// Author: Keith Shomper // Date: 10/10/03 // Purpose: To demonstrate the classic bubble sorting #include #include using namespace std; int main() { int n; // read in the size of the array cout << "How big is the array you want to sort? "; cin >> n; const int SIZE = n; // The size of the array int insertElem; // the element in the array we're attempting to place int i, j; // loop control variables int k; // page control int t; // temporary variable for swapping two numbers // declare other array objects, of various initializations int a[SIZE]; // Read in the contents of array 'a' for (i = 0; i < SIZE; ++i) { cin >> a[i]; } // Print out the contents of array 'a' cout << "Unsorted array 'a' has the values: " << endl; for (i = 0, k = 1; i < SIZE; ++i, ++k) { cout << setw(7) << a[i] << " "; if (k % 10 == 0 || i == SIZE - 1) { cout << endl; } } // sort the array with bubble sort. for (i = SIZE; i > 0; --i) { for (j = 0; j < i - 1; ++j) { if (a[j] > a[j+1]) { t = a[j]; a[j] = a[j+1]; a[j+1] = t; } } } // Print out the contents of array 'a' cout << "Sorted array 'a' has the values: " << endl; for (i = 0, k = 1; i < SIZE; ++i, ++k) { cout << setw(7) << a[i] << " "; if (k % 10 == 0 || i == SIZE - 1) { cout << endl; } } return 0; }