I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com
ComputingStlAlgorithmsSorting

sort

Sorts all elements
+ View version details

Key Facts

Gyroscopic Couple: The rate of change of angular momentum (\inline \tau) = \inline I\omega\Omega (In the limit).
  • \inline I = Moment of Inertia.
  • \inline \omega = Angular velocity
  • \inline \Omega = Angular velocity of precession.


Blaise Pascal (1623-1662) was a French mathematician, physicist, inventor, writer and Catholic philosopher.

Leonhard Euler (1707-1783) was a pioneering Swiss mathematician and physicist.

Definition

The sort() algorithm is defined in the standard header <algorithm> and in the nonstandard backward-compatibility header <algo.h>.

Interface

#include <algorithm>
template < class RandomAccessIterator >
   void sort(
      RandomAccessIterator first, 
      RandomAccessIterator last
   );
template < class RandomAccessIterator, class Predicate >
   void sort(
      RandomAccessIterator first, 
      RandomAccessIterator last, 
      Predicate comp
   );

Parameters:
Parameter Description
first A random-access iterator addressing the position of the first element in the range to be sorted
last A random-access iterator addressing the position one past the final element in the range to be sorted
comp User-defined predicate function object that defines the comparison criterion to be satisfied by successive elements in the ordering. A binary predicate takes two arguments and returns true when satisfied and false when not satisfied

Description

Sort function sorts the elements in the range [first, last) in ascending order.

The first version uses operator< to compare the elements, the second version uses the given comparison function comp.

Return Value

None.

Complexity

The average of a sort complexity is O(N log N), where N = last - first.
Example:
Example - sort algorithm
Problem
The following example demonstrates the use of sort().
Workings
#include <vector>
#include <algorithm>
#include <functional>     
#include <iostream>
 
using namespace std;
 
// return whether first element is greater than the second
bool userdefgreater(int elem1, int elem2)
{    return elem1 > elem2;    }
 
int main()
{
  vector <int> vec1;  // container
  vector <int>::iterator Iter1;  // iterator
  int k;
  for (k = 0; k <= 15; k++)
    vec1.push_back(k);
  random_shuffle(vec1.begin(), vec1.end());
  cout <<"Original random shuffle vector vec1 data:\n";
  for (Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
    cout <<*Iter1<<" ";
  cout <<endl;
  sort(vec1.begin(), vec1.end());
  cout <<"\nSorted vector vec1 data:\n";
  for (Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
    cout <<*Iter1<<" ";
  cout <<endl;
  // to sort in descending order, specify binary predicate
  sort(vec1.begin(), vec1.end(), greater<int>());
  cout <<"\nRe sorted (greater) vector vec1 data:\n";
  for (Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
    cout <<*Iter1<<" ";
  cout <<endl;
  // a user-defined binary predicate can also be used
  sort(vec1.begin(), vec1.end(), userdefgreater);
  cout <<"\nUser defined re sorted vector vec1 data:\n";
  for (Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
    cout <<*Iter1<<" ";
  cout <<endl;
 
  return 0;
}
Solution
Output:

Original random shuffle vector vec1 data:
12 1 9 2 0 11 7 3 4 15 8 5 14 13 10 6

Sorted vector vec1 data:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Re sorted (greater) vector vec1 data:
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

User defined re sorted vector vec1 data:
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
References