Bubble Sort In C

Introduction
  • In this blog, I am going to explain about Bubble sort to sort the numbers or arrange them in an ascending order.
Software Requirements:
  • Turbo C++ or C. 
Programming
  1. #include < stdio.h >   
  2. int main()  
  3. {  
  4.     int array[100], n, c, d, swap;  
  5.     printf("Enter number of elements\n");  
  6.     scanf("%d", & n);  
  7.     printf("Enter %d integers\n", n);  
  8.     for (c = 0; c < n; c++) scanf("%d", & array[c]);  
  9.     for (c = 0; c < (n - 1); c++) {  
  10.         for (d = 0; d < n - c - 1; d++) {  
  11.             if (array[d] > array[d + 1]) {  
  12.                 swap = array[d];  
  13.                 array[d] = array[d + 1];  
  14.                 array[d + 1] = swap;  
  15.             }  
  16.         }  
  17.     }  
  18.     printf("Sorted list in ascending order:\n");  
  19.     for (c = 0; c < n; c++) printf("%d\n", array[c]);  
  20.     return 0;  
  21. }   
Explanation
  •  In the programming, given above, it is clearly understood how to do Bubble sort in a simple manner by using for loop and if condition.

    programming

    programming
Output
 
Output:
 
Conclusion
  • Thus, the program can be executed and printed successfully. 
Ebook Download
View all
Learn
View all