Code:
int left = idarray[0],
right = idarray[number]; // << going out of bounds
quicksort(idarray, left, right);
Passing the value at ith location won't help.
Instead your low and high of
Code:
void quicksort(int idarr[], int low, int high)
expect a valid array index.
The call should have read :
Code:
quicksort(idarray, 0, number - 1);
In your quicksort function, you assume the pivot to be actually located at its right place, won't be true most of the times.The while loop in
Code:
while (i <= j) //While there is more than 1 account
{
while (idarr[i] < pivot)
i++;
while (idarr[j] > pivot)
j--;
if (i <= j) {
tmp = idarr[i];
idarr[i] = idarr[j];
idarr[j] = tmp;
i++;
j--;
}
};
actually locates your pivot.
At the beginning of your quicksort function , let's say you move your idarr[(low + high) / 2] to the end of the array.
then your while loop determines where to place the pivot
Code:
while (i < j) //While there is more than 1 account
{
while (idarr[i] <= pivot && i < j)
i++;
while (idarr[j] >= pivot && i < j)
j--;
if (i < j)
{
tmp = idarr[i];
idarr[i] = idarr[j];
idarr[j] = tmp;
}
}
'i' determines the position of the element which is greater than the pivot.
So it should be placed on the right hand side of the pivot so, you make a swap.
Code:
idarr[high] = idarr[i];
idarr[i] = pivot;
Now you recursively call quicksort to sort the left hand side and then the right hand side of the array
Code:
quicksort(idarr, low ,i - 1 );
quicksort(idarr, i + 1, high);
The final quicksort function looks something like this:
Code:
void quicksort(int idarr[], int low, int high)
{
int i = low,
j = high,
tmp,
pivot ;
// Where is the terminating case ? Array sorted
if (low >= high)
return;
//Move the pivot to the end of the aray
pivot = idarr[(low+high)/2];
idarr[(low+high)/2] = idarr[high];
idarr[high] = pivot;
while (i < j) //While there is more than 1 account
{
while (idarr[i] <= pivot && i < j)
i++;
while (idarr[j] >= pivot && i < j)
j--;
if (i < j)
{
tmp = idarr[i];
idarr[i] = idarr[j];
idarr[j] = tmp;
}
}
//Returned the pivot to its rightful place
idarr[high] = idarr[i];
idarr[i] = pivot;
// recursion
quicksort(idarr, low ,i - 1 );
quicksort(idarr, i + 1, high);
}
This sorts just the ids, you also need to swap the balance of each customer ?