c code for quick sort.

  • csorting
  • write a c code for quick sort.

    WAP to sort an array of n doubles in a descending order using quick sort.

    Code

    #include <stdio.h> void printArray(double *A, int n) { for (int i = 0; i < n; i++) { printf("%lf ", A[i]); } printf("\n"); } int partition(double A[], int low, int high) { double pivot = A[low]; int i = low + 1; int j = high; double temp; do { while (A[i] >= pivot) { i++; } while (A[j] < pivot) { j--; } if (i < j) { temp = A[i]; A[i] = A[j]; A[j] = temp; } } while (i < j); // Swap A[low] and A[j] temp = A[low]; A[low] = A[j]; A[j] = temp; return j; } void quickSort(double A[], int low, int high) { int partitionIndex; if (low < high) { partitionIndex = partition(A, low, high); quickSort(A, low, partitionIndex - 1); quickSort(A, partitionIndex + 1, high); } } int main() { int n = 4; double A[n]; for (int i = 0; i < n; i++) { scanf("%lf", &A[i]); } printArray(A, n); quickSort(A, 0, n - 1); printArray(A, n); return 0; }