Bubble Sort


Bubble Sort :-

  1. #include "stdafx.h"  // header file for MS VS
  2. #include <conio.h> // header file
  3. #include <iostream> // some compiler used <iostream.h>

  4. using namespace std ;

  5. int main()
  6. {
  7. int a[15] =  {1,6,9,7,8,11,4,3,2,5,14,15,13,10,12} ; // Unsorted list

  8. // Process of sorting the list (Bubble sort)

  9. int index = 0 ;
  10. while ( index < 14 )
  11. {
  12. for (int i = 0 ; i < 14 ; i++ )
  13. {
  14. if (a[i] > a[i + 1])
  15. {
  16. int m = a[i] ;
  17. a[i] = a[i + 1] ;
  18. a[i + 1] = m ;
  19. }
  20. }
  21. index ++ ;
  22. }
  23. // For printing the sorted list

  24. for (int i = 0 ; i < 15 ; i ++)
  25. cout << a[i]<< "," ;

  26. _getch() ; // Some compiler use getch()
  27. return 0;
  28. }
Next PostNewer Post Previous PostOlder Post Home

0 comments:

Post a Comment