Library Sort
#include <iostream> #include <algorithm> using namespace std; /* * This program reads in integers and sorts them using the library sort * function. The algorithm header provides the sorting function. */ const int MAX_NUM_INTS = 100; int main() { int ints[MAX_NUM_INTS]; // Where the numbers go. // Read them in. Make sure not to read out of bounds. cout << "Please enter some integers to sort: " << endl; int *scan, *end = ints + MAX_NUM_INTS; for(scan = ints; scan < end && cin >> *scan; ++scan); // Sort them. Note that scan points one past the last valid // data item, which is what is needed. sort(ints, scan); // Print them. cout << "==================" << endl; for(int *p = ints; p < scan; ++p) cout << *p << " "; cout << "\n==================" << endl; }

Here we use the library sort instead of rolling our own. Note the parameters are again passed by a pointer to the first data item, and a pointer one past the end. This convention is common in the C++ libraries.