Standard C++ Library Class Reference
Complexity
partial_sort_copy does approximately (last-first) * log(min(last-first, result_last-result_first)) comparisons.
Example
//
// partsort.cpp
// #include <vector>
 #include <algorithm>
 #include <iostream.h>
 int main()
 {
 int d1[20] = {17, 3, 5, -4, 1, 12, -10, -1, 14, 7,
 -6, 8, 15, -11, 2, -2, 18, 4, -3, 0};
 //
 // Set up a vector.
 //
 vector<int> v1(d1+0, d1+20);
 //
 // Output original vector.
 //
 cout << "For the vector: ";
 copy(v1.begin(), v1.end(), ostream_iterator<int>(cout," "));
 //
 // Partial sort the first seven elements.
 //
 partial_sort(v1.begin(), v1.begin()+7, v1.end());
 //
 // Output result.
 //
 cout << endl << endl << "A partial_sort of 7 elements gives: " 
 << endl << " ";
 copy(v1.begin(), v1.end(), ostream_iterator<int>(cout," "));
 cout << endl;
 //
 // A vector of ten elements.
 //
 vector<int> v2(10, 0);
 //
 // Sort the last ten elements in v1 into v2.
 //
 partial_sort_copy(v1.begin()+10, v1.end(), v2.begin(), 
v2.end());
 //
 // Output result.
 //
 cout << endl << "A partial_sort_copy of the last ten elements 
gives: " << endl << " ";
 copy(v2.begin(), v2.end(), ostream_iterator<int>(cout," "));
 cout << endl;
 return 0;
 }
Output :
For the vector: 17 3 5 -4 1 12 -10 -1 14 7 -6 8 15 -11 2 -2 18 4 -3 0
A partial_sort of seven elements gives:
 -11 -10 -6 -4 -3 -2 -1 17 14 12 7 8 15 5 3 2 18 4 1 0










