Experiment 3 Quick Sort
Experiment 3 Quick Sort
Partition Algorithm:
The partition algorithm rearranges the sub-arrays in a place.
To understand the working of quick sort, let's take an unsorted array. It will make the concept
clearer and understandable.
In the given array, we consider the leftmost element as pivot. So, in the case a[left]=24, a[right]=27
and a[pivot]= 24.
Since, pivot is a left, so algorithms starts from right and move towards left.
Now, a[pivot]<a[right], So algorithm moves forwarded one position towards left, i.e.
Now, a[left] = 24, a[right] = 19, and a[pivot] = 24. Because, a[pivot] > a[right], so, algorithm will
swap a[pivot] with a[right], and pivot moves to right, as -
Now, a[left] = 19, a[right] = 24, and a[pivot] = 24. Since, pivot is at right, so algorithm starts from
left and moves to right.
Now, a[left] = 9, a[right] = 24, and a[pivot] = 24. As a[pivot] > a[left], so algorithm moves one
position to right as –
Now, a[left] = 29, a[right] = 24, and a[pivot] = 24. As a[pivot] < a[left], so, swap a[pivot] and
a[left], now pivot is at left, i.e. –
Since, pivot is at left, so algorithm starts from right, and move to left. Now, a[left] = 24, a[right] =
29, and a[pivot] = 24. As a[pivot] < a[right], so algorithm moves one position to left, as –
Now, a[pivot] = 24, a[left] = 24, and a[right] = 14. As a[pivot] > a[right], so, swap a[pivot] and
a[right], now pivot is at right, i.e. -
Now, a[pivot] = 24, a[left] = 14, and a[right] = 24. Pivot is at right, so the algorithm starts from
left and move to right.
Now, a[pivot] = 24, a[left] = 24, and a[right] = 24. So, pivot, left and right are pointing the same
element. It represents the termination of procedure.
Element 24, which is the pivot element is placed at its exact position.
Elements that are right side of element 24 are greater than it, and the elements that are left side of
element 24 are smaller than it.
Now, in a similar manner, quick sort algorithm is separately applied to the left and right sub-arrays.
After sorting gets done, the array will be –
Quicksort Complexity
Time Complexity
Case Time Complexity
1. #include <stdio.h>
* function that consider last element as pivot, place the pivot at its exact position, and place smal
ler elements to left of pivot and greater elements to right of pivot. */
1. int partition (int a[], int start, int end)
2. {
3. int pivot = a[end]; // pivot element
4. int i = (start - 1);
5.
6. for (int j = start; j <= end - 1; j++)
7. {
8. // If current element is smaller than the pivot
9. if (a[j] < pivot)
10. {
11. i++; // increment index of smaller element
12. int t = a[i];
13. a[i] = a[j];
14. a[j] = t;
15. }
16. }
17. int t = a[i+1];
18. a[i+1] = a[end];
19. a[end] = t;
20. return (i + 1);
21. }
22. /* function to implement quick sort */
23. void quick(int a[], int start, int end) /* a[] = array to be sorted, start = Starting index, end
= Ending index */
24. {
25. if (start < end)
26. {
27. int p = partition(a, start, end); //p is the partitioning index
28. quick(a, start, p - 1);
29. quick(a, p + 1, end);
30. }
31. }
32.
33. /* function to print an array */
34. void printArr(int a[], int n)
35. {
36. int i;
37. for (i = 0; i < n; i++)
38. printf("%d ", a[i]);
39. }
40. int main()
41. {
42. int a[] = { 24, 9, 29, 14, 19, 27 };
43. int n = sizeof(a) / sizeof(a[0]);
44. printf("Before sorting array elements are - \n");
45. printArr(a, n);
46. quick(a, 0, n - 1);
47. printf("\nAfter sorting array elements are - \n");
48. printArr(a, n);
49.
50. return 0;
51. }