Bubble sort in C.

 

01  #include <stdio.h> 
02 void bubble_sort(long [], long);
03
04 int main()
05 {
06 long array[100], n, c, d, swap;
07 printf("Enter number of elements:");
08 scanf("%ld", &n);
09 printf("Enter %ld longegersn", n);
10
11 for (c = 0; c < n; c++)
12 scanf("%ld", &array[c]);
13 bubble_sort(array, n);
14 printf("Sorted list in ascending order:n");
15
16 for ( c = 0 ; c < n ; c++ )
17 printf("%ldn", array[c]);
18
19 return 0;
20 }
21
22 void bubble_sort(long list[], long n)
23 {
24 long c, d, t;
25 for (c = 0 ; c < ( n - 1 ); c++)
26 {
27 for (d = 0 ; d < n - c - 1; d++)
28 {
29 if (list[d] > list[d+1])
30 {
31 t = list[d];
32 list[d] = list[d+1];
33 list[d+1]= t;
34 }
35 }
36 }
37 }
 
 OUTPUT :

Enter number of elements:6

Enter 6 longegers
12
1
231
444
23
54

Sorted list in ascending order:n
1
12
23
54
231
444

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *