int partition(int array[], int min, int max)
{
// define a pivot as the max item of the (sub)array
int pivot = array[max] ;
int i = min - 1 ;
// loop through the elements of the (sub)array
for (int j = min ; j < max ; j++)
{
// in case the element has a smaller value that the pivot
// bring it in front of it (larger elements will come after it)
if (array[j] <= pivot)
{
i++ ;
int temp = array[i] ;
array[i] = array[j] ;
array[j] = temp ;
}
}
// bring the pivot to its correct position
int temp = array[i+1] ;
array[i+1] = array[max] ;
array[max] = temp ;
return i+1 ;
}
void quickSort(int array[], int min, int max)
{
if (min < max)
{
// partition the array in two parts
int q = partition(array, min, max) ;
// apply QuickSort recursively to both parts
quickSort(array, min, q-1) ;
quickSort(array, q+1, max) ;
}
}