fork download
  1. /* Bubble sort code */
  2.  
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5.  
  6. //bubble sort using function
  7.  
  8. void bubble_sort(int [], int); //function definition
  9.  
  10. int main() {
  11. int array[100], n, c, d, swap;
  12.  
  13. printf("Enter number of elements\n");
  14. scanf("%d", &n);
  15.  
  16. printf("Enter %d integers\n", n);
  17.  
  18. for (c = 0; c < n; c++)
  19. scanf("%d", &array);
  20.  
  21. bubble_sort(array, n);
  22.  
  23. printf("Sorted list in ascending order:\n");
  24.  
  25. for (c = 0; c < n; c++)
  26. printf("%d\n", array);
  27.  
  28. return 0;
  29. }
  30.  
  31. void bubble_sort(int list[], int n) //function body
  32. {
  33. int c, d, t;
  34.  
  35. for (c = 0; c < (n - 1); c++) //outer loop
  36. {
  37. for (d = 0; d < n - c - 1; d++) //inner loop
  38. {
  39. if (list[d] > list[d + 1]) {
  40. /* Swapping */
  41.  
  42. t = list[d]; //using temporary variable
  43. list[d] = list[d + 1];
  44. list[d + 1] = t;
  45. }
  46. }
  47. }
  48. }
Success #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
Enter number of elements
Enter 0 integers
Sorted list in ascending order: