I am working with a quicksort function. It works fine when I set the pivot to the first element in the array, but when I set it to the last element the array is "semi" sorted with the first element that should be there is replaced with a duplicate of the second, and there are a few other duplicates throughout the array.

Here is the code:

//splitting function for quicksort
void split(int x[], int first, int last, int & pos) {
int left=first;
int right=last;
int pivot = x[right]; <-------------------works as [left]

while(left<right) {
while(x[right] > pivot)
right--;
while(left < right && x[left] <= pivot)
left++;
if(left < right)
swap(x[left], x[right]);
}
pos=right;
x[first] = x[pos];
x[pos] = pivot;
}

//quicksort function
void quicksort2(int x[], int first, int last) {
int pos;
if(first<last) {
split(x,first,last,pos);
quicksort2(x,first,pos-1);
quicksort2(x,pos+1, last);
}
}


Help is appreciated.

Thanks!