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

Be Quick To Sort Your Problem Out

Quicksort is a divide and conquer algorithm that works by (1) dividing an array into two subarrays based on selecting a pivot element, (2) recursively sorting the subarrays, and (3) combining the sorted subarrays. It has average case performance of O(n log n) but worst case performance of O(n^2) if the array is already sorted. The partitioning algorithm takes linear time O(n) and is the key to quicksort's performance.

Uploaded by

bengali Sen
Copyright
© © All Rights Reserved
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Be Quick To Sort Your Problem Out

Quicksort is a divide and conquer algorithm that works by (1) dividing an array into two subarrays based on selecting a pivot element, (2) recursively sorting the subarrays, and (3) combining the sorted subarrays. It has average case performance of O(n log n) but worst case performance of O(n^2) if the array is already sorted. The partitioning algorithm takes linear time O(n) and is the key to quicksort's performance.

Uploaded by

bengali Sen
Copyright
© © All Rights Reserved
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Quick Sort

BE QUICK TO SORT YOUR PROBLEM OUT


2
The Divide & Conquer Approach

 Divide
 Partition an array A[p..r] into two sub-arrays A[p..q-1] and A[q+1..r]
such that elements in subarray A[p..q-1] ≤ A[q] ≤ elements in subarray
A[q+1..r]
 The element in A[q] is called the pivot element
 Conquer
 Sort the two subarrays recursively using Quick Sort
 Combine
 No work need as in-place sort
Lecture 5 - Quick Sort 11/4/2019
3
Algorithm

Algorithm : QuickSort(A,p,r)
Input : Array, A. Starting index of subarray, p. Ending index of
subarray, q.
Output : A[p..r] in sorted order
Begin
if p < q
then
q ← Partition (A, p, r)
QuickSort(A, p, q-1)
QuickSort(A, q+1, r)
endif
End

Lecture 5 - Quick Sort 11/4/2019


4
Lomuto Partition Algorithm

Algorithm : Partition (A, p, r)


Input : Array A. Starting index p. Ending index r.
Output : Index of pivot element q such that p ≤ q ≤ r & A[p..q-1]
≤ A[q] ≤ A[q+1..r].
Begin
i ← p–1
for j ← p to r-1 i pj r
do
if A[j] ≤ A[r]
then 1 2 3 4 5 6 7 8
i ← i+1 1
8 4
1
8 0
6 8
3
4 6
5
0 3
8 9 5
6
exchange A[i] ↔ A[j]
endif
endfor
exchange A[i+1] ↔ A[r] A[j] ≤ A[r]?
return i+1
End Yes No
Lecture 5 - Quick Sort 11/4/2019
5
Correctness of Partition Algorithm

p i j r

≤x >x ?
 Loop Invariant : At the start of each iteration, for any array index k,
 If p ≤ k ≤ i, then A[k] ≤ x. ( x is the pivot element )
 If (i + 1) ≤ k ≤ (j – 1), then A[k] > x.
 If j ≤ k ≤ (r - 1), A[k] is yet to be compared with x.
 If k = r, then A[k] = x.
Lecture 5 - Quick Sort 11/4/2019
6
Complexity

 Partition Algorithm?
 Θ(n)
 Quick Sort Algorithm?
 Depends on partitioning the array
 If sub-arrays are balanced, it runs as fast as merge sort
 If sub-arrays are unbalanced, it runs as slowly as insertion sort

Lecture 5 - Quick Sort 11/4/2019


7
Worst Case Complexity

 When? 0 1 2 3 4 5 6 7 8 9

 Already Sorted
 What are the sub-array size after partition?
 0 and (n-1) in each iteration
 How the recurrence relation will be?
 T(n) = T(n-1) + T(0) + Θ(n) = T(n-1) + Θ(n)
 Θ(n2)

Lecture 5 - Quick Sort 11/4/2019


8
Best Case Complexity

 When? 0 3 2 1 7 5 6 9 8 4

 Every Partition Call divides equally


 What are the sub-array size after partition?
 𝑛/2 𝑎𝑛𝑑 𝑛/2 𝑖𝑛 𝑒𝑎𝑐ℎ 𝑖𝑡𝑒𝑟𝑎𝑡𝑖𝑜𝑛
 How the recurrence relation will be?
 T(n) = T( 𝑛/2 ) + T( 𝑛/2 ) + Θ(n) = 2T(n/2) + Θ(n)
 Θ(n lg n)

Lecture 5 - Quick Sort 11/4/2019


9
Average Case Complexity

 Partitioning is highly unlikely to happen equally at every


level
 Two intuition for finding out average case
1. Good split and Bad split in alternate recursion
 Consider n = 100
 Compare the tree with the best case scenario
 Approximately twice the best case time
 Θ(2.n lg n) = Θ(n lg n)

Lecture 5 - Quick Sort 11/4/2019


10
Average Case Complexity

2. Partition algorithm always produce a 9-to-1 split

T(n) = T(9n/10) + T(n/10) + c.n


• O(n lg n)

Lecture 5 - Quick Sort 11/4/2019


11
Thank You!

Lecture 5 - Quick Sort 11/4/2019

You might also like