fork download
  1. #include <stdio.h>
  2. void swap(int a,int b)
  3. { int temp=a;
  4. a=b;
  5. b=temp;
  6. }
  7.  
  8. int Partition(int arr[], int low,int high)
  9. { int pivot,i,j;
  10. pivot=arr[low];
  11. i=low+1;
  12. j=high;
  13. while(i<=j)
  14. { while(i<=j && arr[i]<=pivot)
  15. { i++;
  16. }
  17. while(j>=i && arr[j]>pivot)
  18. { j--;
  19. }
  20. if(i<j)
  21. { swap(arr[i],arr[j]);
  22. i++;
  23. j--;
  24. }
  25. }
  26. swap(arr[low],arr[j]);
  27. return j;
  28. }
  29.  
  30. void quickSort(int arr[], int low, int high)
  31. { if(low<high)
  32. { int loc;
  33. loc=Partition(arr,low,high);
  34. quickSort(arr,low,loc-1);
  35. quickSort(arr,loc+1,high);
  36. }
  37. }
  38.  
  39. void main()
  40. { int n,i,arr[50];
  41. printf("Enter size:");
  42. scanf("%d",&n);
  43. printf("\nEnter elements:");
  44. for(i=0;i<n;i++)
  45. { scanf("%d",&arr[i]);
  46. }
  47.  
  48. quickSort(arr,0,n-1);
  49. printf("\nQuick Sorted array is\n");
  50. for(i=0;i<n;i++)
  51. { scanf("%d",&arr[i]);
  52. }
  53. }
Success #stdin #stdout 0s 5324KB
stdin
4
stdout
Enter size:
Enter elements:
Quick Sorted array is