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

Quicksort: - Sort An Array A (P R) - Divide

Quicksort is a sorting algorithm that works in three steps: 1. Divide - Partition the array around a pivot value so that elements less than the pivot are to the left and greater elements to the right. 2. Conquer - Recursively apply the quicksort algorithm to the subarrays to the left and right of the pivot. 3. Combine - No additional steps are needed, the array is sorted in place during the recursive calls.

Uploaded by

kjhofboa
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

Quicksort: - Sort An Array A (P R) - Divide

Quicksort is a sorting algorithm that works in three steps: 1. Divide - Partition the array around a pivot value so that elements less than the pivot are to the left and greater elements to the right. 2. Conquer - Recursively apply the quicksort algorithm to the subarrays to the left and right of the pivot. 3. Combine - No additional steps are needed, the array is sorted in place during the recursive calls.

Uploaded by

kjhofboa
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 50

Quicksort A[p…q] ≤ A[q+1…r]

• Sort an array A[p…r]


• Divide
• Partition the array A into 2 subarrays A[p..q] and A[q+1..r], such that each element of A[p..q]
is smaller than or equal to each element in A[q+1..r]
• Need to find index q to partition the array

1
Quicksort A[p…q] ≤ A[q+1…r]

• Conquer
• Recursively sort A[p..q] and A[q+1..r] using Quicksort

• Combine
• Trivial: the arrays are sorted in place
• No additional work is required to combine them
• The entire array is now sorted
2
Apply Quick Sort Algorithm

32 99 29 98 67 45 25 76 33 22 47
Find Smaller Value from Right Side so that Item < Pivot

44 33 11 55 77 90 40 60 99 22 88 66
Find Smaller Value from Right Side so that Item < Pivot

44 33 11 55 77 90 40 60 99 22 88 66
Find Smaller Value from Right Side so that Item < Pivot

44 33 11 55 77 90 40 60 99 22 88 66
Interchange 44 and 22 to obtain the list

22 33 11 55 77 90 40 60 99 44 88 66
Find Greater Value from Left Side so that Item> Pivot

22 33 11 55 77 90 40 60 99 44 88 66
Find Greater Value from Left Side so that Item> Pivot

22 33 11 55 77 90 40 60 99 44 88 66
Find Greater Value from Left Side so that Item> Pivot

22 33 11 55 77 90 40 60 99 44 88 66
Interchange 44 and 55 to obtain the list

22 33 11 44 77 90 40 60 99 55 88 66
Find Smaller Value from Right Side so that Item < Pivot

22 33 11 44 77 90 40 60 99 55 88 66
Find Smaller Value from Right Side so that Item < Pivot

22 33 11 44 77 90 40 60 99 55 88 66
Find Smaller Value from Right Side so that Item < Pivot

22 33 11 44 77 90 40 60 99 55 88 66
Interchange 44 and 40 to obtain the list

22 33 11 40 77 90 44 60 99 55 88 66
Find Greater Value from Left Side so that Item> Pivot

22 33 11 40 77 90 44 60 99 55 88 66
Interchange 44 and 77 to obtain the list

22 33 11 40 44 90 77 60 99 55 88 66
44

22 33 11 40 90 77 60 99 55 88 66

1st Sublist 2nd Sublist

Now 44 is correctly placed in its final position


1st Sublist

22 33 11 40
Apply Quick Sort Algorithm

32 99 29 98 67 45 25 76 33 22 47
32 99 29 98 67 45 25 76 33 22 47
45

32 99 29 98 67 25 76 33 22 47
Find Greater Value from Left Side so that Item>
Pivot

32 45

99 29 98 67 25 76 33 22 47
Find Greater Value from Left Side so that Item>
Pivot

45

32 99 29 98 67 25 76 33 22 47
Find Greater Value from Left Side so that Item>
Pivot

99 45

32 29 98 67 25 76 33 22 47
Find Smaller Value from Right Side so that Item
< Pivot

99 45

32 29 98 67 25 76 33 22 47
Find Smaller Value from Right Side so that Item
< Pivot

99 45 47

32 29 98 67 25 76 33 22
Find Smaller Value from Right Side so that Item
< Pivot

99 45

32 29 98 67 25 76 33 22 47
Find Smaller Value from Right Side so that Item
< Pivot

99 45 22

32 29 98 67 25 76 33 47
Exchange
Occurred

45
22 99
32 29 98 67 25 76 33 47
Exchange
Occurred
And
Index Changed

45

32 22 29 98 67 25 76 33 99 47
Find Greater Value from Left Side so that Item>
Pivot

29 45

32 22 98 67 25 76 33 99 47
Find Greater Value from Left Side so that Item>
Pivot

45

32 22 29 98 67 25 76 33 99 47
Find Greater Value from Left Side so that Item>
Pivot

98 45

32 22 29 67 25 76 33 99 47
Find Smaller Value from Right Side so that Item
< Pivot

98 45 33

32 22 29 67 25 76 99 47
Exchange
Occurred

98 45 33

32 22 29 67 25 76 99 47
Exchange
Occurred

45
33 98
32 22 29 67 25 76 99 47
Exchange
Occurred

45

32 22 29 33 67 25 76 98 99 47
Exchange
Occurred
And
Index Changed

45

32 22 29 33 67 25 76 98 99 47
Find Greater Value from Left Side so that Item>
Pivot

45

32 22 29 33 67 25 76 98 99 47
Find Greater Value from Left Side so that Item>
Pivot

67 45

32 22 29 33 25 76 98 99 47
Find Smaller Value from Right Side so that Item
< Pivot

67 45 76

32 22 29 33 25 98 99 47
Find Smaller Value from Right Side so that Item
< Pivot

67 45

32 22 29 33 25 76 98 99 47
Find Smaller Value from Right Side so that Item
< Pivot

67 45 25

32 22 29 33 76 98 99 47
Exchange
Occurred

25 45 67

32 22 29 33 76 98 99 47
Exchange
Occurred

45
25 67
32 22 29 33 76 98 99 47
Left Side< 45 < Right
Side

45 67 76 98 99 47

32 22 29 33 25
Left Side< 45 < Right
Side

67 76 98 99 47

45

32 22 29 33 25
Apply Recursive Partitioning on
Left Side

32 22 29 33 25
Apply Recursive Partitioning on Right
Side
67 76 98 99 47

You might also like