0% found this document useful (0 votes)
27 views

Experiment 3 Quick Sort

Uploaded by

sarthakverma2503
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Experiment 3 Quick Sort

Uploaded by

sarthakverma2503
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Experiment-3

Objective: Write a program to implement Quicksort.


Quicksort is the widely used sorting algorithm that makes n log n comparisons in average case
for sorting an array of n elements. It is a faster and highly efficient sorting algorithm. This
algorithm follows the divide and conquer approach. Divide and conquer is a technique of breaking
down the algorithms into sub-problems, then solving the sub-problems, and combining the results
back together to solve the original problem.
Divide: In Divide, first pick a pivot element. After that, partition or rearrange the array into two
sub-arrays such that each element in the left sub-array is less than or equal to the pivot element
and each element in the right sub-array is larger than the pivot element.
Conquer: Recursively, sort two subarrays with Quicksort.
Combine: Combine the already sorted array.
Quicksort picks an element as pivot, and then it partitions the given array around the picked pivot
element. In quick sort, a large array is divided into two arrays in which one holds values that are
smaller than the specified value (Pivot), and another array holds the values that are greater than
the pivot.
After that, left and right sub-arrays are also partitioned using the same approach. It will continue
until the single element remains in the sub-array.
Quick Sort Algorithm
Choosing the pivot
Picking a good pivot is necessary for the fast implementation of quicksort. However, it is typical
to determine a good pivot. Some of the ways of choosing a pivot are as follows -
Pivot can be random, i.e. select the random pivot from the given array.
Pivot can either be the rightmost element of the leftmost element of the given array.
Select median as the pivot element.
Algorithm

1. QUICKSORT (array A, start, end)


2. {
3. 1 if (start < end)
4. 2{
5. 3 p = partition (A, start, end)
6. 4 QUICKSORT (A, start, p - 1)
7. 5 QUICKSORT (A, p + 1, end)
8. 6 }
9. }

Partition Algorithm:
The partition algorithm rearranges the sub-arrays in a place.

1. PARTITION (array A, start, end)


2. {
3. 1 pivot ? A[end]
4. 2 i ? start-1
5. 3 for j ? start to end -1 {
6. 4 do if (A[j] < pivot) {
7. 5 then i ? i + 1
8. 6 swap A[i] with A[j]
9. 7 }}
10. 8 swap A[i+1] with A[end]
11. 9 return i+1
12. }

Working of Quick Sort Algorithm


Now, let's see the working of the Quicksort Algorithm.

To understand the working of quick sort, let's take an unsorted array. It will make the concept
clearer and understandable.

Let the elements of array are -

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.

As a[pivot] > a[left], so algorithm moves one position to right as -

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

Best Case O(n*logn)

Average Case O(n*logn)

Worst Case O(n2)


Space Complexity: O(n*logn)

C language Code of Quick sort

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. }

C++ code for Quick sort


1. #include <iostream>
2.
3. using namespace std;
4.
5. /* function that consider last element as pivot,
6. place the pivot at its exact position, and place
7. smaller elements to left of pivot and greater
8. elements to right of pivot. */
9. int partition (int a[], int start, int end)
10. {
11. int pivot = a[end]; // pivot element
12. int i = (start - 1);
13.
14. for (int j = start; j <= end - 1; j++)
15. {
16. // If current element is smaller than the pivot
17. if (a[j] < pivot)
18. {
19. i++; // increment index of smaller element
20. int t = a[i];
21. a[i] = a[j];
22. a[j] = t;
23. }
24. }
25. int t = a[i+1];
26. a[i+1] = a[end];
27. a[end] = t;
28. return (i + 1);
29. }
30.
31. /* function to implement quick sort */
32. void quick(int a[], int start, int end) /* a[] = array to be sorted, start = Starting index, end = Endi
ng index */
33. {
34. if (start < end)
35. {
36. int p = partition(a, start, end); //p is the partitioning index
37. quick(a, start, p - 1);
38. quick(a, p + 1, end);
39. }
40. }
41.
42. /* function to print an array */
43. void printArr(int a[], int n)
44. {
45. int i;
46. for (i = 0; i < n; i++)
47. cout<<a[i]<< " ";
48. }
49. int main()
50. {
51. int a[] = { 23, 8, 28, 13, 18, 26 };
52. int n = sizeof(a) / sizeof(a[0]);
53. cout<<"Before sorting array elements are - \n";
54. printArr(a, n);
55. quick(a, 0, n - 1);
56. cout<<"\nAfter sorting array elements are - \n";
57. printArr(a, n);
58.
59. return 0;
60. }

Python code for Quicksort

1. #function that consider last element as pivot,


2. #place the pivot at its exact position, and place
3. #smaller elements to left of pivot and greater
4. #elements to right of pivot.
5.
6. def partition (a, start, end):
7. i = (start - 1)
8. pivot = a[end] # pivot element
9.
10. for j in range(start, end):
11. # If current element is smaller than or equal to the pivot
12. if (a[j] <= pivot):
13. i=i+1
14. a[i], a[j] = a[j], a[i]
15.
16. a[i+1], a[end] = a[end], a[i+1]
17.
18. return (i + 1)
19.
20. # function to implement quick sort
21. def quick(a, start, end): # a[] = array to be sorted, start = Starting index, end = Ending index
22. if (start < end):
23. p = partition(a, start, end) # p is partitioning index
24. quick(a, start, p - 1)
25. quick(a, p + 1, end)
26. def printArr(a): # function to print the array
27. for i in range(len(a)):
28. print (a[i], end = " ")
29.
30.
31. a = [68, 13, 1, 49, 58]
32. print("Before sorting array elements are - ")
33. printArr(a)
34. quick(a, 0, len(a)-1)
35. print("\nAfter sorting array elements are - ")
36. printArr(a)

Java Code for Quicksort

1. public class Quick


2. {
3. /* function that consider last element as pivot,
4. place the pivot at its exact position, and place
5. smaller elements to left of pivot and greater
6. elements to right of pivot. */
7. int partition (int a[], int start, int end)
8. {
9. int pivot = a[end]; // pivot element
10. int i = (start - 1);
11. for (int j = start; j <= end - 1; j++)
12. {
13. // If current element is smaller than the pivot
14. if (a[j] < pivot)
15. {
16. i++; // increment index of smaller element
17. int t = a[i];
18. a[i] = a[j];
19. a[j] = t;
20. }
21. }
22. int t = a[i+1];
23. a[i+1] = a[end];
24. a[end] = t;
25. return (i + 1);
26. }
27. /* function to implement quick sort */
28. void quick(int a[], int start, int end) /* a[] = array to be sorted, start = Starting index, end = Endi
ng index */
29. {
30. if (start < end)
31. {
32. int p = partition(a, start, end); //p is partitioning index
33. quick(a, start, p - 1);
34. quick(a, p + 1, end);
35. }
36. }
37. /* function to print an array */
38. void printArr(int a[], int n)
39. {
40. int i;
41. for (i = 0; i < n; i++)
42. System.out.print(a[i] + " ");
43. }
44. public static void main(String[] args) {
45. int a[] = { 13, 18, 27, 2, 19, 25 };
46. int n = a.length;
47. System.out.println("\nBefore sorting array elements are - ");
48. Quick q1 = new Quick();
49. q1.printArr(a, n);
50. q1.quick(a, 0, n - 1);
51. System.out.println("\nAfter sorting array elements are - ");
52. q1.printArr(a, n);
53. System.out.println();
54. }
55. }

You might also like