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.