I have an assignment to create a quicksort algorithm which chooses the last element as the pivot element. Further inside the partition() function when an Element n is found which is greater or equal to the pivot the function should perform a "ring swap". It should swap n with pivot-1 and after that pivot-1 with pivot.
So i wrote this program:
int partition(uint8_t *arr, int left, int right){
int pivot_position = right;
if (right-left >1)
{
for (int i = right; i >= left; i--)
{
for (int j = left; j < i; j++)
{
if (arr[j] >= arr[i])
{
pivot_position = i;
int tmp = arr[j];
arr[j] = arr[i-1];
arr[i-1] = arr[i];
arr[i] = tmp;
break;
}
}
}
}else{
if (right-left == 1 && arr[left] > arr[right])
{
int tmp = arr[left];
arr[left] = arr[right];
arr[right] = tmp;
}
return 0;
}
return pivot_position;
}
void quicksort( uint8_t *arr, int left, int right){
int pivot = partition(arr, left, right);
if (pivot != 0)
{
quicksort( arr, left, pivot-1);
quicksort( arr, pivot+1, right);
}
}
If given the Array: [160, 32, 96, 128, 224, 64, 192, 0, 255] The expected Output: [0, 32 , 64 ,96, 128, 160, 192, 224, 256]
What have I done wrong?
EDIT: As requested:
void main() {
uint8_t *arr = malloc(sizeof(uint8_t)*9);
arr[0] = 160;
arr[1] = 32;
arr[2] = 96;
arr[3] = 128;
arr[4] = 224;
arr[5] = 64;
arr[6] = 192;
arr[7] = 0;
arr[8] = 255;
for(int i = 0; i < 9, i++){
printf(arr[i]);
}
quicksort(arr, 0, 8);
for(int i = 0; i < 9; i++){
printf(arr[i]);
}
}
Sorry if this does not work coudnt test it I am currently not infront of my machine.
The main issue is that you are not always comparing with the pivot value. Your code assumes it is at
i, but that will not be true once thejloop doesn't find anything to swap. In that case your outer loop will still decreasei, which at that moment no longer points to the pivot.Also when you update
pivot_position, you should take into account that you are about to move that pivot to indexi-1, so it should take that value.Then there is also a type issue. You've declared
arras a pointer tou_int8. But then yourtempshould not be defined as anintbut asuint8_t.If we just want to fix the minimum necessary, you could replace this:
with this:
But the double loop is inefficient. The inner loop starts from
leftover and over again. This is not needed. You only need to compare a value once with the pivot. Also, when there are only two values to partition, you don't really need a special case (theelseblock).Also,
Here is how I would write it -- using that 3-cycle constraint: