fork download
  1. #include<stdio.h>
  2. void swap(int* a, int* b)
  3. {
  4. int t = *a;
  5. *a = *b;
  6. *b = t;
  7. }
  8. int partition (int arr[], int low, int high)
  9. {
  10. int pivot = arr[high];
  11. int i = (low - 1);
  12. for (int j = low; j <= high- 1; j++)
  13. {
  14. if (arr[j] <= pivot)
  15. {
  16. i++;
  17. swap(&arr[i], &arr[j]);
  18. }
  19. }
  20. swap(&arr[i + 1], &arr[high]);
  21. return (i + 1);
  22. }
  23. void quickSort(int arr[], int low, int high)
  24. {
  25. if (low < high)
  26. {
  27. int pivot = partition(arr, low, high);
  28. quickSort(arr, low, pivot - 1);
  29. quickSort(arr, pivot + 1, high);
  30. }
  31. }
  32.  
  33. void displayArray(int arr[], int size)
  34. {
  35. int i;
  36. for (i=0; i < size; i++)
  37. printf("%d\t ",arr[i]);
  38. }
  39. int main()
  40. {
  41. int n;
  42. printf("Aman Sharma\n");
  43. printf("Enter the range of Elements : ");
  44. scanf("%d",&n);
  45. int arr[n];
  46. printf("Enter the Elements : \n");
  47. for(int i=0;i<n;i++)
  48. scanf("%d",&arr[i]);
  49. quickSort(arr, 0, n-1);
  50. printf("Array sorted with quick sort \n");
  51. displayArray(arr,n);
  52. return 0;
  53. }
Success #stdin #stdout 0s 4320KB
stdin
5
7
8
9
4
3
stdout
Aman Sharma
Enter the range of Elements : Enter the Elements : 
Array sorted with quick sort 
3	 4	 7	 8	 9