// Author: Keith Shomper // Date: 10/10/03 // Purpose: To demonstrate redirecting imput and 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 // 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 insertion sort. for (i = 1; i < SIZE; ++i) { insertElem = a[i]; j = i-1; while (j >= 0 && a[j] > insertElem) { a[j+1] = a[j]; --j; } a[++j] = insertElem; } // 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; }