1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

void quicksort(int a[], int left, int right)
{
int SplitPoint;
if(compare(right, left) <= 0) //checking if there is only 1 element in array so no need to do anything
{
return;
}
else if(compare(left+1,right) == 0) //checking if there is 2 elements in array, can just swap with if statement.
{
if(compare(a[left],a[right]) > 0)
{
swap(a[left],a[right]);
}
return;
}
SplitPoint = partition(a,left,right); //splitpoint is the index of the pivot's correct positions
quicksort(a,left,SplitPoint1); //recursively calling quicksort on the 2 smaller subarrays
quicksort(a, SplitPoint+1, right); //as split by the pivot
}
int partition(int a[], int left, int right)
{
int i, j, pivot;
j = right + 1;
i = left  1;
pivot = a[left+2]; //just testing using a different pivot
while(true)
{
while(compare(pivot, a[++i]) > 0) // keep scanning from left until an element greater than the pivot is found
{
if(i == right) //if the rightmost index is reached then break out of this while loop
{ //i.e there is nothing greater than the pivot
break;
}
}
while(compare(pivot, a[j]) < 0) // keep scanning from right until an element less than the pivot is found
{
if(j == left) //if the leftmost index is reached then break out of this while loop
{ //i.e theres nothing less than the pivot
break;
}
}
if(compare(i, j) >= 0) //if i and j meet or crossover then break out the infinite while(true) loop
{ //as everything to the left is less than the pivot and everything to the right is greater
break;
}
swap(a[i],a[j]); //swap elements in wrong side
print_array(a, 10);
}
swap(a[left], a[j]); //put the pivot in the right place
print_array(a, 10);
return j; //returns the correct position of the pivot
}
 