-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bubble Sort.c
58 lines (46 loc) · 891 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
53
54
55
56
57
58
#include<stdio.h>
void dispArr(int arr[], int n)
{
int i;
printf("The Array: ");
for(i=0; i<n; i++)
{
printf("%d", arr[i]);
(i != n-1) ? printf(" ") : printf("\n");
}
}
void bubbleSort(int arr[], int n)
{
int i, j, temp;
for(i=0; i<n-1; i++)
{
for(j=0; j<n-i-1; j++)
{
if(arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int main()
{
int n, i, j;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter array data: ");
for(i=0; i<n; i++)
{
scanf("%d", &arr[i]);
}
printf("\n");
dispArr(arr, n);
printf("\n");
bubbleSort(arr, n);
printf("After Bubble Sorting: ");
dispArr(arr, n);
return 0;
}