fork download
  1. #include<stdio.h>
  2.  
  3. void swap(int *a, int *b){
  4. int temp = *a;
  5. *a=*b;
  6. *b=temp;
  7. }
  8.  
  9. int partition(int arr[], int l, int r){
  10. int pivot = arr[l];
  11. int left,right;
  12. for(left=l+1,right=r;left<=right;){
  13. if(arr[left]>pivot && arr[right]<=pivot){
  14. swap(&arr[left],&arr[right]);
  15. }
  16. if(arr[left]<=pivot) {
  17. left++;
  18. }
  19. if(arr[right]>pivot) {
  20. right--;
  21. }
  22. }
  23. swap(&arr[l],&arr[right]);
  24. // printf("%d\n",right);
  25. return right;
  26. }
  27.  
  28. void quicksort(int arr[], int l, int r){
  29. int p;
  30. if (l < r){
  31. p = partition(arr,l,r);
  32. quicksort(arr,l,p-1);
  33. quicksort(arr,p+1,r);
  34. }
  35. }
  36.  
  37. int main(){
  38. int i,size;
  39. // int arr[] = { 10, 20, 7 , 5, 24 , 17, 13, 56, 38, 12 , 29, 46};
  40. int arr[] = {0,2,2};
  41. // int arr[] = {2,2,1,0};
  42. size = sizeof(arr)/sizeof(arr[0]);
  43. printf("The array before sorting is --->");
  44. for(i=0;i<size;i++){
  45. printf("--->%d",arr[i]);
  46. }
  47. printf("--->END\n\n");
  48. quicksort(arr,0,size-1);
  49. printf("The array after sorting is --->");
  50. for(i=0;i<size;i++){
  51. printf("--->%d",arr[i]);
  52. }
  53. printf("--->END");
  54.  
  55. return 0;
  56. }
Success #stdin #stdout 0s 2292KB
stdin
Standard input is empty
stdout
The array before sorting is --->--->0--->2--->2--->END

The array after sorting is --->--->0--->2--->2--->END