Guest User

Bubble sort (infinite loop)

a guest
Apr 30th, 2011
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     int array[10] = {4,3,11,9,7,54,12,35,31,1};
  7.    
  8.     cout << "The array's original order: ";
  9.     for(int ac = 0;ac<9;ac++)
  10.     {
  11.             cout << array[ac] << ", ";
  12.     }
  13.    
  14.     cout << endl;
  15.    
  16.     //##Bubble sort##
  17.     int swapper;
  18.     int swapped = 1;
  19.    
  20.     //do
  21.     while (swapped = 1)
  22.     {
  23.          for (int sc=0;sc<8;sc++)
  24.          {
  25.              swapped = 0;
  26.              if(array[sc] > array[sc+1])
  27.              {
  28.                           swapper = array[sc];
  29.                           array[sc] = array[sc+1];
  30.                           array[sc+1] = swapper;
  31.                           swapped = 1;
  32.              }
  33.               cout << "here"; //continuously outputs this
  34.          }  
  35.     }
  36.     //while (swapped = 1);
  37.     //##Sorted##
  38.    
  39.     cout << "The array sorted          : ";
  40.     for(int ac = 0;ac<9;ac++)
  41.     {
  42.             cout << array[ac] << ", ";
  43.     }
  44.     cout << endl;
  45.    
  46.  system("pause");
  47.  return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment