fork download
  1. #include<stdio.h>
  2.  
  3. void swap(int *i, int *j)
  4. {
  5. int temp = *i;
  6. *i = *j;
  7. *j = temp;
  8. }
  9.  
  10. int partition(int arr[], int start, int end)
  11. {
  12. int pivot = arr[end];
  13. int i = start;
  14. int j = end-1;
  15. while(i<=j)
  16. {
  17. while(arr[i]<pivot)
  18. i++;
  19. while(arr[j]>pivot)
  20. j--;
  21. if(i<=j)
  22. {
  23. swap(&arr[i],&arr[j]);
  24. i++;
  25. j--;
  26. }
  27. }
  28.  
  29. swap(&arr[i],&arr[end]);
  30. return i;
  31. }
  32.  
  33. void performQuickSort(int arr[], int start, int end)
  34. {
  35. if(start<end)
  36. {
  37. int p = partition(arr, start, end);
  38. performQuickSort(arr, start, p-1);
  39. performQuickSort(arr, p+1, end);
  40. }
  41. }
  42.  
  43. void quickSort(int arr[], int size)
  44. {
  45. performQuickSort(arr, 0, size-1);
  46. }
  47.  
  48. // driver program
  49. int main(void)
  50. {
  51. int i;
  52. int arr[10] = {2, 6, 4, 10, 8, 1, 9, 5, 3, 7};
  53.  
  54. quickSort(arr,10);
  55.  
  56. printf("The 5th largest number in array is:- %d", arr[10 - 5]);
  57. return 0;
  58. }
  59.  
Success #stdin #stdout 0s 2168KB
stdin
Standard input is empty
stdout
The 5th largest number in array is:-  6