Reputation: 965
I'm getting this error:
prog.cpp:1:5: error: expected unqualified-id before ‘[’ token
Form this code:
int [] quick_srt(int array[], int low, int n){
That's the problem, please?
EDIT
int [] quick_srt(int array[], int low, int n){
int lo = low;
int hi = n;
comp++;
if (lo >= n){ // lo is greater then or equal to n
return array;
}
int mid = array[(lo + hi) / 2]; //Using mid as pivot
comp++;
while (lo < hi){ //lo is less then hi
comp++;
while (lo < hi && array[lo] < mid){ //lo less than hi AND array[lo] less than mid
lo++;
comp++;
}
comp++;
while (lo < hi && array[hi] > mid) {//lo less than hi AND array[lo] greater than mid
hi--;
comp++;
}
comp++; //for if
comp++; //for else
if(array[lo] == array[hi]){
break; //for duplicate items
}
else if (lo < hi) { // less than
int T = array[lo];
array[lo] = array[hi];
array[hi] = T;
swaps++;
}
comp++;
}
comp++;
if (hi < lo) { //hi is less than lo
int T = hi;
hi = lo;
lo = T;
}
quick_srt(array, low, lo); //recrusie call
quick_srt(array, lo == low ? lo+1 : lo, n); //re-call, if lo = low, increment lo else pass lo and n
return array;
}
Upvotes: 0
Views: 99
Reputation: 25
One more way is use void quick_srt(int * array, int low)
I would suggest go with vector its lot more easy
Upvotes: 0
Reputation: 51930
int []
is only valid for function arguments. I'd suggest you do this instead:
void quick_srt(std::vector<int>& array, int low)
You don't need the n
argument. USe `array.size() instead.
Upvotes: 2
Reputation: 225282
You can't return an array from a function. the int array[]
in the parameter list is just syntactic sugar for int *array
, and there's no analogue for the return value. If you want to return a pointer, you'll need to do so explicitly:
int *quick_srt(int array[], int low, int n);
or, equivalently:
int *quick_srt(int *array, int low, int n);
Upvotes: 2