// Author: Keith Shomper // Date: 10/10/03 // Purpose: To demonstrate sorting #include #include #include using namespace std; int compare (const void *elem1, const void *elem2) { return *((int *)elem1) - *((int *)elem2); } 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 quicksort. qsort(a, SIZE, sizeof(int), compare); // 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; }