void quickSort(vector<double> unsortedData, int leftBound, int rightBound) {
if (leftBound < rightBound) {
double i = partitionStep(unsortedData, leftBound, rightBound);
quickSort(unsortedData, leftBound, i-1);
quickSort(unsortedData, i + 1, rightBound);
}
}
double partitionStep(vector<double> unsortedData, int leftBound, int rightBound) {
double pivot = unsortedData[rightBound];
while (leftBound <= rightBound) {
while ((unsortedData[leftBound] < pivot)) {
leftBound++;
}
while ((unsortedData[rightBound] > pivot)) {
rightBound--;
}
if (unsortedData[leftBound] == unsortedData[rightBound]) {
leftBound++;
} else if (leftBound < rightBound) {
double temp = unsortedData[leftBound];
unsortedData[leftBound] = unsortedData[rightBound];
unsortedData[rightBound] = temp;
}
}
return rightBound;
}
I need to sort a vector of doubles. This code runs but the vector is not sorted at the end. It's probably something that I am overlooking. Thoughts?
A high level description of your quickSort
routine is:
which isn't particularly useful. Change the input argument to vector<double>&
so that you're doing stuff with a reference to the original vector rather than a copy.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.