+ 3

Using QuickSort Ascending and Descending in C language

The Ascending worked fine but the Descending doesn't. The outcome of the Sorted Descending isn't right... CODE: #include <stdio.h> #define size 7 void quickAscending(int array[size], int start, int end){ int temp; if(start < end){ int pointer = start; int pivot = array[end]; int count; for(count = start; count < end; count++){ if(array[count] < pivot){ temp = array[count]; array[count] = array[pointer]; array[pointer] = temp; pointer++; } } temp = array[end]; array[end] = array[pointer]; array[pointer] = temp; quickAscending(array, start, pointer-1); quickAscending(array, pointer+1, end); } } void quickDescending(int array[size], int start, int end){ int temp; if(start < end){ int pointer = start; int pivot = array[end]; int count; for(count = start; count < end; count++){ if(array[count] > pivot){ temp = array[count]; array[count] = array[pointer]; array[pointer] = temp; pointer++; } } temp = array[end]; array[end] = array[pointer]; array[pointer] = temp; quickAscending(array, start, pointer-1); quickAscending(array, pointer+1, end); } } void displayArray(int array[size]){ int count; for(count = 0; count < size; count++){ printf("%d ", array[count]); } } int main(){ int array[size] = {20, 10, 5, 27, 0, 3, 15}; printf("Unsorted Array: "); displayArray(array); quickAscending(array, 0, size - 1); printf("\n\nAscending Sorted Array: "); displayArray(array); quickDescending(array, 0, size - 1); printf("\nDescending Sorted Array: "); displayArray(array); }

29th Sep 2021, 4:35 PM
Kaizuke
Kaizuke - avatar
2 Answers
+ 5
In Descending function, you recursively calls Ascending function instead of Descending function.
29th Sep 2021, 5:07 PM
Rusiru Rathmina
Rusiru Rathmina - avatar
+ 1
Oh my gosh thank you Rusiru Rathmina ! I got tired that I didn't even noticed it. Thank you so much ( ꈍᴗꈍ)
29th Sep 2021, 5:10 PM
Kaizuke
Kaizuke - avatar