# Thread: Losing number in quick sort

1. ## Losing number in quick sort

I'm tinkering with a serial quick sort algorithm. Normally I use qsort() but I wanted to test this.

Somehow I loose the highest number. I can't see where I drop it. If someone here could point it out...
Code:
```// Help function to quicksort, leave them in front of qsort.
void swap(float_t *left, float_t *right){
float_t temp;
temp = *left;
*left = *right;
*right = temp;
}

// Help function to quicksort, this works like a charm on random dist numbers.
int pivot(int i, int j){
return ((i+j)/2);
}

// Quick sort..
void quick_sort(float_t * array, int low, int high){
int start, end, piv;
float_t k;

if (low < high){
piv = pivot(low,high);
swap(&array[low],&array[piv]);
k = array[low];
start = (low+1);
end = high;

while (start <= end)
{
while ((start <= high) && (array[start] <= k)){ start++; }
while ((end >= low) && (array[end] > k)){ end--; }
if (start < end){ swap(&array[start],&array[end]);}
}
swap(&array[low],&array[end]);
quick_sort(array,low,(end-1));
quick_sort(array,(end+1),high);
}
}```

2. I've tested it and I can't see anything wrong with it either.

I suspect the bug is in your test code. It's very easy to mix up high, the maximum index, with N, the count, or high + 1.

3. I think I know what I did. I allocated a N size array. When I call the function I did it like this q_sort(array, 0 , N); I guess it should be N-1.

Thanks Malcolm for the input, it made me think twice about mixing up indexes.