Created
December 17, 2015 11:02
-
-
Save ercanozkaya/0bf58a976d3ab27c48ff to your computer and use it in GitHub Desktop.
sort.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<class Iterator> | |
void quickSort(Iterator begin, Iterator end) | |
{ | |
Iterator pivot = begin; | |
if (begin == end) { | |
return; | |
} | |
int i = 1, | |
size = end - begin; | |
// Convert the iterator into (pivot,<pivot,>pivot) format | |
for (int j = 1; j < size; j++) { | |
if (*(begin+j) < *pivot) { | |
swap(*(begin+j), *(begin+i)); | |
i++; | |
} | |
} | |
// Put pivot in the middle | |
swap(*pivot, *(begin+i-1)); | |
// Sort the <pivot and >pivot partitions | |
quickSort(begin, begin+i-1); | |
quickSort(begin+i,end); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
vector user2score;
quickSort<vector::iterator>(user2score.begin(), user2score.end());