Advertisement
ptrawt

259201 Lab12

Nov 10th, 2014
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 KB | None | 0 0
  1. #include "iostream"
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     bool did_swap = true;
  7.     int N = 8, index;
  8.     /* A[0] will not be used */
  9.     int A[9] = {0,98,23,45,14,6,67,33,42};
  10.     int to_do = N-1;
  11.  
  12.     while( to_do > 0 && did_swap)
  13.     {
  14.         index = 1;
  15.         did_swap = false;
  16.         while ( index <= to_do )
  17.         {
  18.             if (A[index] > A[index+1])
  19.             {
  20.                 int temp = A[index];
  21.                 A[index] = A[index+1];
  22.                 A[index+1] = temp;
  23.                 did_swap = true;
  24.             }
  25.             index = index+1;
  26.         }
  27.         cout << "to_do=" << to_do <<": ";
  28.         for (int i = 1; i <= N; ++i)
  29.         {
  30.             cout<<A[i]<<" ";
  31.         }
  32.         cout << endl;
  33.         to_do = to_do-1;
  34.     }
  35.  
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement