The idea is repetitively partitioning the data collection, splitting it in two parts, in a way that a randomly chosen pivot would be equal or greater than the values on its left partition, and then call again the quicksorting procedure, until there is nothing more left to sort. As one could easily spot, is a possible bad choice of the pivot that could lead to poor performances.
The resulting code should be something like this:
void quicksort(std::vector<int>& data, int left, int right) // 1 { if(left < right) // 2 { int pivot = partition(data, left, right); // 3 quicksort(data, left, pivot - 1); // 4 quicksort(data, pivot + 1, right); } }1. The function requires in input the collection on which it should operate and the indexes of its leftmost and rightmost elements.
2. Check if the interval is not empty.
3. Split the original interval in two parts. On the left side we have all the values less or equal to the value in the pivot element.
4. Call again quicksort on the left and right partitions. Notice that the pivot element is already in the right place, and don't need to be considered anymore.
We just need to partition a collection as expected:
int partition(std::vector<int>& data, int left, int right) { int pivot = data[right]; // 1 int index = left - 1; // 2 for(int i = left; i < right; ++i) // 3 { if(data[i] <= pivot) // 4 std::swap(data[++index], data[i]); } std::swap(data[++index], data[right]); // 5 return index; }1. OK, this doesn't look smart. As pivot we always select the rightmost element in the interval.
2. Initialize index to the first-before-beginning position in the interval.
3. Loop on all the items in the interval, but the last one (that is, the pivot).
4. If the current element value is less than the pivot, let's swap it with the first not already used element on the left.
5. Finally, we swap the pivot (rightmost value in the interval) with the element next to index.
Full C++ code on github.
No comments:
Post a Comment