Implement quick sort.Â
Source code
#include <stdio.h>
void swap(int *x, int *y) {
 int temp = *x;
 *x = *y;
 *y = temp;
}
int partition(int arr[], int l, int h) {
 int pvt = arr[h];
 int i = (l - 1);
 for (int j = l; j < h; j++) {
  if (arr[j] <= pvt) {
   i++;
   swap(&arr[i], &arr[j]);
  }
 }
 swap(&arr[i + 1], &arr[h]);
 return (i + 1);
}
void qiuck_sort(int arr[], int l, int h) {
 if (l < h) {
  int p = partition(arr, l, h);
  qiuck_sort(arr, l, p - 1);
  qiuck_sort(arr, p + 1, h);
 }
}
void display(int arr[], int n) {
 for (int i = 0; i < n; ++i) {
  printf("%d ", arr[i]);
 }
 printf("\n");
}
int main() {
 int arr[] = {15,12,17,16,11,20,18,12,19,13,14};
 Â
 int size = sizeof(arr) / sizeof(arr[0]);
 Â
 printf("Array elements before sorting: \n");
 display(arr, size);
 qiuck_sort(arr, 0, size - 1);
 Â
 printf("Array elements after sorting in ascending order: \n");
 display(arr, size);
}
Output
Comments
Leave a comment