Advertisement
Shailrshah

Explanation of QuickSort

Nov 18th, 2013
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int *a, n;
  4. void swap(int *a ,int *b){
  5.     int temp = *a;
  6.     *a = *b;
  7.     *b = temp;
  8. }
  9. void quickSort(int left,int right){
  10.     if(left < right){
  11.         int pivot = left, i = left, j = right;
  12.         while(i < j){
  13.             while(a[i] <= a[pivot] && i < right) i++;
  14.             while(a[j] > a[pivot]) j--;
  15.             if(i < j) swap(&a[i], &a[j]);
  16.         }
  17.         swap(&a[j], &a[pivot]);
  18.         quickSort(left, j-1);
  19.         quickSort(j+1, right);
  20.     }
  21. }
  22. int main(){
  23.     printf("Enter the number of elements: ");
  24.     scanf("%d", &n);
  25.     a = (int *) malloc(sizeof(int) * n);
  26.     printf("\nNow enter %d elements:-\n", n);
  27.     for(int i = 0; i < n; i++) scanf("%d", &a[i]);
  28.     quickSort(0, n-1);
  29.     for(int i = 0; i < n; i++) printf("%d  ", a[i]);
  30. }
  31. // The array is:-
  32. // 19  27  5  9  86  45 (n = 6)
  33. // quickSort(0, n-1)(0, 5) will be called from main().
  34.  
  35. // Start of quickSort(0, 5)
  36. // Since left < right, pivot and i = left = 0 and j = right = 5
  37. // At the end of a pass in the while loop, i = 1 and j = 3
  38. // Since i < j, a[i](9) and a[j](27) are swapped.
  39. // The array is:-
  40. // 19  9  5  27  86  45
  41. // At the end of a pass in the while loop, i = 3 and j = 2
  42. // Since i >= j, no swapping takes place.
  43. // Since i>=j, control has come out of the while loop.
  44. // a[j](5) and a[pivot](19) are swapped.
  45. // The array is:-
  46. // 5  9  19  27  86  45
  47. // quickSort(left, j-1)(0, 1) and quickSort(j+1, right)(3, 5) will be called.
  48.  
  49. // Start of quickSort(0, 1)
  50. // Since left < right, pivot and i = left = 0 and j = right = 1
  51. // At the end of a pass in the while loop, i = 1 and j = 0
  52. // Since i >= j, no swapping takes place.
  53. // Since i>=j, control has come out of the while loop.
  54. // a[j](5) and a[pivot](5) are swapped.
  55. // The array is:-
  56. // 5  9  19  27  86  45
  57. // quickSort(left, j-1)(0, -1) and quickSort(j+1, right)(1, 1) will be called.
  58.  
  59. // Start of quickSort(0, -1)
  60. // Since left >= right, end of quickSort(a, 0, -1)
  61.  
  62. // Start of quickSort(1, 1)
  63. // Since left >= right, end of quickSort(a, 1, 1)
  64.  
  65. // After completing quickSort(0, -1) and quickSort(1, 1), quickSort(0, 1) ends
  66.  
  67. // Start of quickSort(3, 5)
  68. // Since left < right, pivot and i = left = 3 and j = right = 5
  69. // At the end of a pass in the while loop, i = 4 and j = 3
  70. // Since i >= j, no swapping takes place.
  71. // Since i>=j, control has come out of the while loop.
  72. // a[j](27) and a[pivot](27) are swapped.
  73. // The array is:-
  74. // 5  9  19  27  86  45
  75. // quickSort(left, j-1)(3, 2) and quickSort(j+1, right)(4, 5) will be called.
  76.  
  77. // Start of quickSort(3, 2)
  78. // Since left >= right, end of quickSort(a, 3, 2)
  79.  
  80. // Start of quickSort(4, 5)
  81. // Since left < right, pivot and i = left = 4 and j = right = 5
  82. // At the end of a pass in the while loop, i = 5 and j = 5
  83. // Since i >= j, no swapping takes place.
  84. // Since i>=j, control has come out of the while loop.
  85. // a[j](45) and a[pivot](86) are swapped.
  86. // The array is:-
  87. // 5  9  19  27  45  86
  88. // quickSort(left, j-1)(4, 4) and quickSort(j+1, right)(6, 5) will be called.
  89.  
  90. // Start of quickSort(a, 4, 4)
  91. // Since left >= right, end of quickSort(a, 4, 4)
  92.  
  93. // Start of quickSort(6, 5)
  94. // Since left >= right, end of quickSort(a, 6, 5)
  95.  
  96. // After completing quickSort(4, 4) and quickSort(6, 5), quickSort(4, 5) ends
  97.  
  98. // After completing quickSort(3, 2) and quickSort(4, 5), quickSort(3, 5) ends
  99.  
  100. // After completing quickSort(0, 1) and quickSort(3, 5), quickSort(0, 5) ends
  101. // The sorted array is:-
  102. // 5  9  19  27  45  86
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement