vanllabean81

Bubble Sort

Jul 18th, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.         int array[10];  // Declare array
  8.         int end = 9;    // Array holds 10 pieces of data
  9.          
  10.         for (int x = 0; x <= end; x++)
  11.         {
  12.             cin >> array[x];            // Read in array values
  13.         }
  14.        
  15.         for (int times = 0; times <= end; times++)     // Make n - 1 passes
  16.         {
  17.                 for(int i = 0; i < end; i++)        // For each element in array
  18.                 {                              
  19.                      if(array[i+1] < array[i])      // Compare 2 elements
  20.                      {
  21.                         int temp;                      // Temp variable to hold contents of i while we swap
  22.                         temp = array[i+1];             // Put smaller data in temporary holder
  23.                         array[i+1] = array[i];      // Put bigger number second
  24.                         array[i] = temp;               // Put smaller number first
  25.                      }
  26.                 }
  27.         }
  28.         for (int y = 0; y <= end; y++)
  29.                     cout << array[y] << endl;   // Print swapped array
  30. }
Advertisement
Add Comment
Please, Sign In to add comment