13 Quick Sorti
13 Quick Sorti
Associate Professor
2. Else
a) pick one element to use as pivot.
d) Return results
40 20 10 80 60 50 7 30 100
40 20 10 80 60 50 7 30 100
pivot_index = 0 40 20 10 80 60 50 7 30 100
too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
pivot_index = 0 40 20 10 80 60 50 7 30 100
too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
pivot_index = 0 40 20 10 80 60 50 7 30 100
too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
pivot_index = 0 40 20 10 80 60 50 7 30 100
too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
pivot_index = 0 40 20 10 80 60 50 7 30 100
too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
pivot_index = 0 40 20 10 80 60 50 7 30 100
too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
pivot_index = 0 40 20 10 80 60 50 7 30 100
too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
pivot_index = 0 40 20 10 30 60 50 7 80 100
too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.
pivot_index = 0 40 20 10 30 60 50 7 80 100
too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.
pivot_index = 0 40 20 10 30 60 50 7 80 100
too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.
pivot_index = 0 40 20 10 30 60 50 7 80 100
too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.
pivot_index = 0 40 20 10 30 60 50 7 80 100
too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.
pivot_index = 0 40 20 10 30 60 50 7 80 100
too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.
pivot_index = 0 40 20 10 30 60 50 7 80 100
too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.
pivot_index = 0 40 20 10 30 7 50 60 80 100
too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.
pivot_index = 0 40 20 10 30 7 50 60 80 100
too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.
pivot_index = 0 40 20 10 30 7 50 60 80 100
too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.
pivot_index = 0 40 20 10 30 7 50 60 80 100
too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.
pivot_index = 0 40 20 10 30 7 50 60 80 100
too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.
pivot_index = 0 40 20 10 30 7 50 60 80 100
too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.
pivot_index = 0 40 20 10 30 7 50 60 80 100
too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.
pivot_index = 0 40 20 10 30 7 50 60 80 100
too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.
pivot_index = 0 40 20 10 30 7 50 60 80 100
too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.
5. Swap arr[too_small_index] and arr[pivot_index]
pivot_index = 0 40 20 10 30 7 50 60 80 100
too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.
5. Swap arr[too_small_index] and arr[pivot_index]
pivot_index = 4 7 20 10 30 40 50 60 80 100
too_big_index too_small_index
Quick Sort
(Partition Result)
7 20 10 30 40 50 60 80 100
7 20 10 30 40 50 60 80 100
Recursion:
1. Partition splits arr in two sub-arrs of size n/2
2. Quicksort each sub-arr
Depth of Recursion:
Ω(log(n))
QuickSort
Ω(nlog(n))
Quicksort
Assume that keys are random, uniformly distributed.
QuickSort
Ω(nlog(n))
Quicksort
Assume that keys are random, uniformly distributed.
QuickSort
cn+c(n−1)+c(n−2)+⋯+2c
=c(n+(n−1)+(n−2)+⋯+2)
=c((n+1)(n/2)−1) .
O(n^2)
Quicksort
Assume that keys are random, uniformly distributed.
QuickSort
cn+c(n−1)+c(n−2)+⋯+2c
=c(n+(n−1)+(n−2)+⋯+2)
=c((n+1)(n/2)−1) .
O(n^2)
By:Dr. Prakash Singh Tanwar
Thank You