I'm trying to make quicksort program. I just couldn't understand why and how this error is coming. I've tried every method online but I'm unable to catch the issue. If I'm doing this with array in place of vector, I'm getting correct output but with vector error is coming.
My errors are:
.\quicksort.cpp:7:28: error: cannot convert 'std::vector<double>' to 'double' in initialization
double pivot = values[end];
.\quicksort.cpp:10:19: error: no match for 'operator<=' (operand types are 'std::vector<double>' and 'double')
if (values[i] <= pivot) {
How to convert the vector<double>
to double then?
Here's my code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int partition(vector<double> *values, int start, int end) {
double pivot = values[end];
int pIndex = start;
for (int i=start; i<end; i++) {
if (values[i] <= pivot) {
swap(values[i], values[pIndex]);
pIndex++;
}
}
swap(values[pIndex], values[end]);
return pIndex;
}
void quicksort(vector<double> *values, int start, int end) {
if (start<end) {
int pIndex;
pIndex = partition(values, start, end);
quicksort(values, start, pIndex-1);
quicksort(values, pIndex+1, end);
}
}
int main() {
int n;
cin >> n;
vector<double> values(n);
for (int i = 0; i < n; i++) {
cin >> values[i];
}
quicksort(values, 0, n-1);
for (int j=0; j<n; j++) {
cout<<values[j]<<" ";
}
return 0;
}
How to rectify these errors?
References not pointers
int partition(vector<double>& values, int start, int end) {
void quicksort(vector<double>& values, int start, int end) {
If you used a pointer (but you shouldn't) the correct code would be
double pivot = (*values)[end];
Pointers are not the same as the things they are pointing to, you have to use *
or ->
to get at the object that a pointer points to. You don't have to do this with references.
void quicksort(vector<double> &values, const int start, const int end);
int partition(vector<double> &values, const int start, const int end)
And your compile time error gone, with pointer to the object you need to call your function like this:
quicksort(&values, 0, n - 1);
Because you need it's address (actually your pointer need it).
And you need to use operator[]
of std::vector
which is first need to get std::vector
object from pointer to it:
double pivot = (*values)[end];
As you see it's ugly and could be complex, So it's better to use References instead of pointer in those situations.
Your partition and quicksort functions take pointers as parameters not reference values.
Your functions should be declared as such:
int partition(vector<double> &values, int start, int end)
void quicksort(vector<double> &values, int start, int end)
The reason you are getting the error is that since the values parameter is a pointer when you use the array accessor ( [i]
) it assumes that values is an array of vectors and returns a vector not a double.
you should pass vector<double>&
to your function instead of vector<double>*
, ie pass by reference instead of pass by pointer. If passing by pointer, then you need to access the elements of the vector with ->[index]
.
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.