Bubble sort algorithm implementation in C
C program for bubble sort: C programming code for bubble sort to sort numbers or arrange them in ascending order. You can modify it to print numbers in descending order.
/* Bubble sort code
By atnyla developer
int array, n, c, d, swap;
printf("Enter number of elements\n");
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
for (c = 0 ; c < ( n - 1 ); c++)
for (d = 0 ; d < n - c - 1; d++)
if (array[d] > array[d+1]) /* For decreasing order use < */
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
printf("Sorted list in ascending order:\n");
for ( c = 0 ; c < n ; c++ )
Output :Enter number of elements
Enter 6 integers
Sorted list in ascending order:
Press any key to continue . . .
You can also sort strings using Bubble sort, it is less efficient as its average and worst case complexity is high, there are many other fast sorting algorithms like quicksort, heapsort etc. Sorting simplifies problem-solving in computer programming.