-
Notifications
You must be signed in to change notification settings - Fork 138
/
Bubble_sort.c
52 lines (46 loc) · 980 Bytes
/
Bubble_sort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// BUBBLE SORT USING C PROGRAMME
#include <stdio.h>
void swap(int* xp, int* yp)// function to swap elements
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
//Implementation of bubble sort
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n - 1; i++){
for (j = 0; j < n - i - 1; j++){
if (arr[j] > arr[j + 1])
swap(&arr[j], &arr[j + 1]);
}
}
}
/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
// Driver program to test above functions
int main()
{
int i, n;
printf("Enter the number of elements in the array : ");
scanf("%d",&n);
int arr[n];
printf("Enter the elements in the array : ");
for(i = 0;i<n;i++){
scanf("%d",&arr[i]);
}
printf("Entered array is : ");
printArray(arr, n);
printf("\n");
bubbleSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}