fork download
  1. // Online C compiler to run C program online
  2. #include <stdio.h>
  3.  
  4. //{ Driver Code Starts
  5. //Initial Template for C
  6.  
  7. // C program for implementation of Bubble sort
  8.  
  9. #include <stdio.h>
  10. void printArray(int arr[], int size);
  11.  
  12. void swap(int *xp, int *yp)
  13. {
  14. int temp = *xp;
  15. *xp = *yp;
  16. *yp = temp;
  17. }
  18.  
  19.  
  20. // } Driver Code Ends
  21. //User function Template for C
  22.  
  23.  
  24. void quickSort2(int arr[], int low, int high)
  25. {
  26. static int idx = 0;
  27. int i = low;
  28. int j = high;
  29. int pivot = arr[low + (high-low)/2];
  30. // int idx = low + (high-low)/2;
  31.  
  32. while(i < j)
  33. {
  34. while(arr[i] < pivot)
  35. i++;
  36.  
  37. while(arr[j] > pivot)
  38. j--;
  39.  
  40. if (i<=j)
  41. {
  42. int tmp = arr[i];
  43. arr[i] = arr[j];
  44. arr[j] = tmp;
  45. i++;
  46. j--;
  47. }
  48.  
  49. }
  50. // printArray(arr, 6);
  51.  
  52. if(low < high)
  53. {
  54. // printf(" %d, %d\n", i, j);
  55. printf("[quickSort loop %d] TARGET = %d, LEFT = %d, RIGHT = %d\n", idx++, pivot, i, j);
  56. quickSort2(arr, low, j);
  57. quickSort2(arr, i, high);
  58. }
  59. }
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68. //{ Driver Code Starts.
  69.  
  70. /* Function to print an array */
  71. void printArray(int arr[], int size)
  72. {
  73. int i;
  74. for (i=0; i < size; i++)
  75. printf("%d ", arr[i]);
  76. printf("\n");
  77. }
  78.  
  79.  
  80. // Driver program to test above functions
  81. int main()
  82. {
  83. // int arr[] = {999,3,7,13,11,10,9,5,888,333,555,90,32,65,777,1,3};
  84. int arr[] = {0,3,9,5,1, -1};
  85. // int arr[] = {0,10, 11,9,5,1, -1};
  86. int n = sizeof(arr)/sizeof(int);
  87. printf("size of arrary is %d \n", n);
  88. printArray(arr, n);
  89.  
  90. // bubbleSort(arr, n);
  91. // selectionSort(arr, n);
  92. // insertionSort(arr, n);
  93. quickSort2(arr, 0, n-1);
  94. printArray(arr, n);
  95. return 0;;
  96. }
  97. // } Driver Code Ends
  98.  
  99.  
  100.  
  101.  
Success #stdin #stdout 0s 5288KB
stdin
8
stdout
size of arrary is 6 
0 3 9 5 1 -1 
[quickSort loop 0] TARGET = 9, LEFT = 5, RIGHT = 4
[quickSort loop 1] TARGET = -1, LEFT = 1, RIGHT = 1
[quickSort loop 2] TARGET = -1, LEFT = 1, RIGHT = -1
[quickSort loop 3] TARGET = 0, LEFT = 2, RIGHT = 1
[quickSort loop 4] TARGET = 5, LEFT = 4, RIGHT = 3
[quickSort loop 5] TARGET = 3, LEFT = 3, RIGHT = 2
-1 0 1 3 5 9