NOTE -- The example programs described in the following sections have been combined and are included in the file alg7.cpp. As in Chapter13, we generally omit output statements from the descriptions of the programs provided here, although they are included in the complete versions.

The C++ Standard Library provides two fundamental sorting algorithms, described as follows:

namespace std { void sort(RandomAccessIterator first, RandomAccessIterator last [, Compare ] ); void stable_sort(RandomAccessIterator first, RandomAccessIterator last [, Compare ] ); }

The `std::sort()` algorithm is slightly faster, but it does not guarantee that equal elements in the original sequence retain their relative orderings in the final result. If order is important, the `std::stable_sort()` version should be used.

Because these algorithms require random access ** iterator**s, they can be used only with

The comparison operator can be explicitly provided when the default operator `<` is not appropriate. This is used in the example program to sort a ** list** into descending, rather than ascending order. An alternative technique for sorting an entire collection in the inverse direction is to describe the sequence using reverse

NOTE -- Another sorting algorithm is provided by the heap operations described in Section 14.7.

The following example program illustrates the `std::sort()` algorithm being applied to a ** vector**, and the

void sort_example() // illustrates the use of the sort algorithm // see alg7.cpp for complete source code { // fill both a vector and a deque // with random integers std::vector<int> aVec(15); std::deque<int> aDec(15); std::generate(aVec.begin(), aVec.end(), randomValue); std::generate(aDec.begin(), aDec.end(), randomValue); // sort the vector ascending std::sort(aVec.begin(), aVec.end()); // sort the deque descending std::sort(aDec.begin(), aDec.end(), greater<int>() ); // alternative way to sort descending std::sort(aVec.rbegin(), aVec.rend()); }

The generic algorithm `std::partial_sort()` sorts only a portion of a sequence. In the first version of the algorithm, three iterators are used to describe the beginning, middle, and end of a sequence. If `n` represents the number of elements between the start and middle, then the smallest `n` elements are moved into this range in order. The remaining elements are moved into the second region. The order of the elements in this second region is undefined.

namespace std { void partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last [ , Compare ]); }

A second version of the algorithm leaves the input unchanged. The output area is described by a pair of random access ** iterator**s. If

namespace std { RandomAccessIterator partial_sort_copy (InputIterator first, InputIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last [, Compare ] ); }

Because the input to this version of the algorithm is specified only as a pair of input iterators, the `std::partial_sort_copy()` algorithm can be used with any of the containers in the C++ Standard Library. In the example program, it is used with a ** list**:

void partial_sort_example() // illustrates the use of the partial sort algorithm // see alg7.cpp for complete source code { // make a vector of 15 random integers std::vector<int> aVec(15); std::generate(aVec.begin(), aVec.end(), randomValue); // partial sort the first seven positions std::partial_sort(aVec.begin(), aVec.begin() + 7, aVec.end()); // make a list of random integers std::list<int> aList(15, 0); std::generate(aList.begin(), aList.end(), randomValue); // sort only the first seven elements std::vector<int> start(7); std::partial_sort_copy(aList.begin(), aList.end(), start.begin(), start.end(), std::greater<int>()); }