0% found this document useful (0 votes)
2 views35 pages

Algorithm Quicksort

Quick Sort is a Divide and Conquer algorithm that partitions an array around a pivot and recursively sorts the subarrays. The performance of Quick Sort varies based on the pivot selection strategy, with different cases for worst, best, and average scenarios. The worst case occurs when the pivot is consistently the smallest or largest element, while the best case happens when the pivot is the median.

Uploaded by

syeda
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)
2 views35 pages

Algorithm Quicksort

Quick Sort is a Divide and Conquer algorithm that partitions an array around a pivot and recursively sorts the subarrays. The performance of Quick Sort varies based on the pivot selection strategy, with different cases for worst, best, and average scenarios. The worst case occurs when the pivot is consistently the smallest or largest element, while the best case happens when the pivot is the median.

Uploaded by

syeda
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

Quick Sort

SWE2016-44
Quick Sort

• Divide and Conquer algorithm


1. Divide: Partition the array into two subarrays around a pivot 𝑥 such that
𝑒𝑙𝑒𝑚𝑒𝑛𝑡𝑠 𝑖𝑛 𝑙𝑜𝑤𝑒𝑟 𝑠𝑢𝑏𝑎𝑟𝑟𝑎𝑦 ≤ 𝑥 ≤ 𝑒𝑙𝑒𝑚𝑒𝑛𝑡𝑠 𝑖𝑛 𝑢𝑝𝑝𝑒𝑟 𝑠𝑢𝑏𝑎𝑟𝑟𝑎𝑦.
2. Conquer: Recursively sort the two subarrays.
3. Combine: Trivial.

• It picks an element as pivot and partitions the given array around the picked pivot. There
are many different versions of Quick Sort that pick pivot in different ways:
- Always pick first element as pivot
- Always pick last element as pivot
- Pick a random element as pivot (Randomized Quick Sort)
- Pick median as pivot

• The key process is partition()

2
Pseudo Code for recursive quickSort function

Quick Sort 1
• Always pick last element as pivot

Pseudo code for partition()

3
Pseudo Code for recursive quickSort function

Quick Sort 1
• Always pick last element as pivot

Pseudo code for partition()

arr[] = {10, 80, 30, 90, 40, 50, 70}


Indexes: 0 1 2 3 4 5 6

low = 0, high = 6, pivot = arr[h] = 70


Initialize index of smaller element, i = -1
Traverse elements from j = low to high-1

4
Pseudo Code for recursive quickSort function

Quick Sort 1
• Always pick last element as pivot

Pseudo code for partition()

arr[] = {10, 80, 30, 90, 40, 50, 70}


Indexes: 0 1 2 3 4 5 6

low = 0, high = 6, pivot = arr[h] = 70


Initialize index of smaller element, i = -1
Traverse elements from j = low to high-1

j = 0: Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])


i=0
arr[] = {10, 80, 30, 90, 40, 50, 70} // No change as i and j are same

5
Pseudo Code for recursive quickSort function

Quick Sort 1
• Always pick last element as pivot

Pseudo code for partition()

arr[] = {10, 80, 30, 90, 40, 50, 70}


Indexes: 0 1 2 3 4 5 6

low = 0, high = 6, pivot = arr[h] = 70


Initialize index of smaller element, i = -1
Traverse elements from j = low to high-1

j = 0: Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])


i=0
arr[] = {10, 80, 30, 90, 40, 50, 70} // No change as i and j are same

j = 1: Since arr[j] > pivot, do nothing // No change in i and arr[]

6
Pseudo Code for recursive quickSort function

Quick Sort 1
• Always pick last element as pivot

Pseudo code for partition()

arr[] = {10, 80, 30, 90, 40, 50, 70}


Indexes: 0 1 2 3 4 5 6

low = 0, high = 6, pivot = arr[h] = 70


Initialize index of smaller element, i = -1
Traverse elements from j = low to high-1

j = 0: Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])


i=0
arr[] = {10, 80, 30, 90, 40, 50, 70} // No change as i and j are same

j = 1: Since arr[j] > pivot, do nothing // No change in i and arr[]

j = 2: Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])


i=1
arr[] = {10, 30, 80, 90, 40, 50, 70} // We swap 80 and 30

7
Pseudo Code for recursive quickSort function

Quick Sort 1
• Always pick last element as pivot

Pseudo code for partition()

arr[] = {10, 80, 30, 90, 40, 50, 70} j = 3: Since arr[j] > pivot, do nothing // No change in i and arr[]
Indexes: 0 1 2 3 4 5 6

low = 0, high = 6, pivot = arr[h] = 70


Initialize index of smaller element, i = -1
Traverse elements from j = low to high-1

j = 0: Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])


i=0
arr[] = {10, 80, 30, 90, 40, 50, 70} // No change as i and j are same

j = 1: Since arr[j] > pivot, do nothing // No change in i and arr[]

j = 2: Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])


i=1
arr[] = {10, 30, 80, 90, 40, 50, 70} // We swap 80 and 30

8
Pseudo Code for recursive quickSort function

Quick Sort 1
• Always pick last element as pivot

Pseudo code for partition()

arr[] = {10, 80, 30, 90, 40, 50, 70} j = 3: Since arr[j] > pivot, do nothing // No change in i and arr[]
Indexes: 0 1 2 3 4 5 6
j = 4: Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])
low = 0, high = 6, pivot = arr[h] = 70 i=2
Initialize index of smaller element, i = -1 arr[] = {10, 30, 40, 90, 80, 50, 70} // 80 and 40 Swapped
Traverse elements from j = low to high-1

j = 0: Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])


i=0
arr[] = {10, 80, 30, 90, 40, 50, 70} // No change as i and j are same

j = 1: Since arr[j] > pivot, do nothing // No change in i and arr[]

j = 2: Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])


i=1
arr[] = {10, 30, 80, 90, 40, 50, 70} // We swap 80 and 30

9
Pseudo Code for recursive quickSort function

Quick Sort 1
• Always pick last element as pivot

Pseudo code for partition()

arr[] = {10, 80, 30, 90, 40, 50, 70} j = 3: Since arr[j] > pivot, do nothing // No change in i and arr[]
Indexes: 0 1 2 3 4 5 6
j = 4: Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])
low = 0, high = 6, pivot = arr[h] = 70 i=2
Initialize index of smaller element, i = -1 arr[] = {10, 30, 40, 90, 80, 50, 70} // 80 and 40 Swapped
Traverse elements from j = low to high-1
j = 5: Since arr[j] <= pivot, do i++ and swap arr[i] with arr[j]
j = 0: Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])
i=3
i=0
arr[] = {10, 30, 40, 50, 80, 90, 70} // 90 and 50 Swapped
arr[] = {10, 80, 30, 90, 40, 50, 70} // No change as i and j are same

j = 1: Since arr[j] > pivot, do nothing // No change in i and arr[]

j = 2: Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])


i=1
arr[] = {10, 30, 80, 90, 40, 50, 70} // We swap 80 and 30

10
Pseudo Code for recursive quickSort function

Quick Sort 1
• Always pick last element as pivot

Pseudo code for partition()

arr[] = {10, 80, 30, 90, 40, 50, 70} j = 3: Since arr[j] > pivot, do nothing // No change in i and arr[]
Indexes: 0 1 2 3 4 5 6
j = 4: Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])
low = 0, high = 6, pivot = arr[h] = 70 i=2
Initialize index of smaller element, i = -1 arr[] = {10, 30, 40, 90, 80, 50, 70} // 80 and 40 Swapped
Traverse elements from j = low to high-1
j = 5: Since arr[j] <= pivot, do i++ and swap arr[i] with arr[j]
j = 0: Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])
i=3
i=0
arr[] = {10, 30, 40, 50, 80, 90, 70} // 90 and 50 Swapped
arr[] = {10, 80, 30, 90, 40, 50, 70} // No change as i and j are same

j = 1: Since arr[j] > pivot, do nothing // No change in i and arr[] We come out of loop because j is now equal to high-1.
Finally we place pivot at correct position by swapping arr[i+1] and
j = 2: Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j]) arr[high] (or pivot)
i=1 arr[] = {10, 30, 40, 50, 70, 90, 80} // 80 and 70 Swapped
arr[] = {10, 30, 80, 90, 40, 50, 70} // We swap 80 and 30

11
Pseudo Code for recursive quickSort function

Quick Sort 1
• Always pick last element as pivot

Pseudo code for partition()

arr[] = {10, 80, 30, 90, 40, 50, 70} j = 3: Since arr[j] > pivot, do nothing // No change in i and arr[]
Indexes: 0 1 2 3 4 5 6
j = 4: Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])
low = 0, high = 6, pivot = arr[h] = 70 i=2
Initialize index of smaller element, i = -1 arr[] = {10, 30, 40, 90, 80, 50, 70} // 80 and 40 Swapped
Traverse elements from j = low to high-1
j = 5: Since arr[j] <= pivot, do i++ and swap arr[i] with arr[j]
j = 0: Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])
i=3
i=0
arr[] = {10, 30, 40, 50, 80, 90, 70} // 90 and 50 Swapped
arr[] = {10, 80, 30, 90, 40, 50, 70} // No change as i and j are same

j = 1: Since arr[j] > pivot, do nothing // No change in i and arr[] We come out of loop because j is now equal to high-1.
Finally we place pivot at correct position by swapping arr[i+1] and
j = 2: Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j]) arr[high] (or pivot)
i=1 arr[] = {10, 30, 40, 50, 70, 90, 80} // 80 and 70 Swapped
arr[] = {10, 30, 80, 90, 40, 50, 70} // We swap 80 and 30
Now 70 is at its correct place. All elements smaller than 70
are before it and all elements greater than 70 are after it.
12
Analysis of Quick Sort 1

• Time taken by QuickSort in general can be written as following:

𝑇 𝑛 =𝑇 𝑘 +𝑇 𝑛−𝑘−1 +Θ 𝑛

- The first two terms are for two recursive calls, the last term is for the partition process. 𝑘 is the number of
elements which are smaller than pivot.

• The time taken by QuickSort depends upon the input array and partition strategy.
Following are three cases:
1. Worst Case
2. Best Case
3. Average Case

13
Analysis of Quick Sort 1 𝑇 𝑛 =𝑇 𝑘 +𝑇 𝑛−𝑘−1 +Θ 𝑛

• Worst Case
- The worst case occurs when the partition process always picks greatest or smallest element as pivot. If we
consider above partition strategy where last element is always picked as pivot, the worst case would occur when
the array is already sorted in increasing or decreasing order

𝑇 𝑛 =𝑇 0 +𝑇 𝑛−1 +Θ 𝑛 =𝑇 𝑛−1 +Θ 𝑛

• Best Case
- The best case occurs when the partition process always picks the middle element as pivot.

𝑛
𝑇 𝑛 = 2𝑇 +Θ 𝑛
2

• Average Case
- Consider all possible permutation of array and calculate time taken by every permutation which doesn’t look easy.
- We can get an idea of average case by considering the case when partition puts 𝑂(𝑛/9) elements in one set and
𝑂(9𝑛/10) elements in other set.

𝑛 9𝑛
𝑇 𝑛 =𝑇 +𝑇 +Θ 𝑛
10 10
14
Analysis of Quick Sort 1 𝑇 𝑛 =𝑇 𝑘 +𝑇 𝑛−𝑘−1 +Θ 𝑛

• Worst Case 𝚯 𝒏𝟐
- The worst case occurs when the partition process always picks greatest or smallest element as pivot. If we
consider above partition strategy where last element is always picked as pivot, the worst case would occur when
the array is already sorted in increasing or decreasing order

𝑇 𝑛 =𝑇 0 +𝑇 𝑛−1 +Θ 𝑛 =𝑇 𝑛−1 +Θ 𝑛

• Best Case 𝚯 𝒏 𝐥𝐨𝐠 𝒏  case 2 of Master Theorem


- The best case occurs when the partition process always picks the middle element as pivot.

𝑛
𝑇 𝑛 = 2𝑇 +Θ 𝑛
2

• Average Case 𝚯 𝒏 𝒍𝒐𝒈 𝒏


- Consider all possible permutation of array and calculate time taken by every permutation which doesn’t look easy.
- We can get an idea of average case by considering the case when partition puts 𝑂(𝑛/9) elements in one set and
𝑂(9𝑛/10) elements in other set.

𝑛 9𝑛
𝑇 𝑛 =𝑇 +𝑇 +Θ 𝑛
10 10
15
Quick Sort 2
• Random Pivoting
- Using a randomly generated pivot we can further improve the time complexity of QuickSort.
- An example:

16
Quick Sort 2
• Random Pivoting
- Using a randomly generated pivot we can further improve the time complexity of QuickSort.
- An example:

17
Analysis of Quick Sort 2
• Random Pivoting
- In Randomized Quick Sort, we use random number to pick the next pivot (or we randomly shuffle the array)

- Time complexity of some randomized algorithms is dependent on value of random variable. Such
Randomized algorithms are called Las Vegas Algorithms. These algorithms are typically analyzed for
expected worst case.

- To compute expected time taken in worst case, all possible values of the used random variable needs to be
considered in worst case and time taken by every possible value needs to be evaluated. Average of all
evaluated times is the expected worst case time complexity.

18
Analysis of Quick Sort 2
• Random Pivoting
- Let 𝑇 𝑛 = the random variable for the running time of randomized quicksort on an input of size 𝑛,
assuming random numbers are independent.
- For 𝑘 = 0, 1, … , 𝑛– 1, define the indicator random variable

1 if PARTITION generates a 𝑘: 𝑛 − 𝑘 − 1 split,


𝑋𝑘 = ቊ
0 otherwise

1
- 𝐸 𝑋𝑘 = Pr 𝑋𝑘 = 1 = , since all splits are equally likely, assuming elements are distinct.
𝑛

𝑇 0 + 𝑇 𝑛– 1 + Θ 𝑛 if 0: 𝑛– 1 split,
𝑇 1 + 𝑇 𝑛– 2 + Θ 𝑛 if 1: 𝑛– 2 split,
𝑇 𝑛 =

𝑇 𝑛– 1 + 𝑇 0 + Θ 𝑛 if 𝑛– 1: 0 split,
𝑛−1

= ෍ 𝑋𝑘 𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ 𝑛 .
𝑘=0
19
Analysis of Quick Sort 2
• Random Pivoting
𝑛−1

𝑇 𝑛 = ෍ 𝑋𝑘 𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ 𝑛
𝑘=0

20
Analysis of Quick Sort 2
• Random Pivoting
𝑛−1

𝑇 𝑛 = ෍ 𝑋𝑘 𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ 𝑛
𝑘=0
𝑛−1 Take expectations of both sides.
𝐸𝑇 𝑛 = 𝐸 ෍ 𝑋𝑘 𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ 𝑛
𝑘=0

21
Analysis of Quick Sort 2
• Random Pivoting
𝑛−1

𝑇 𝑛 = ෍ 𝑋𝑘 𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ 𝑛
𝑘=0
𝑛−1

𝐸𝑇 𝑛 = 𝐸 ෍ 𝑋𝑘 𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ 𝑛
𝑘=0
𝑛−1
Linearity of expectation.
= ෍ 𝐸 𝑋𝑘 𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ 𝑛
𝑘=0

22
Analysis of Quick Sort 2
• Random Pivoting
𝑛−1

𝑇 𝑛 = ෍ 𝑋𝑘 𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ 𝑛
𝑘=0
𝑛−1

𝐸𝑇 𝑛 = 𝐸 ෍ 𝑋𝑘 𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ 𝑛
𝑘=0
𝑛−1

= ෍ 𝐸 𝑋𝑘 𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ 𝑛
𝑘=0
𝑛−1
Independence of 𝑋𝑘 from other random choices.
= ෍ 𝐸 𝑋𝑘 ∙ 𝐸 𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ 𝑛
𝑘=0

23
Analysis of Quick Sort 2
• Random Pivoting
𝑛−1

𝑇 𝑛 = ෍ 𝑋𝑘 𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ 𝑛
𝑘=0
𝑛−1

𝐸𝑇 𝑛 = 𝐸 ෍ 𝑋𝑘 𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ 𝑛
𝑘=0
𝑛−1

= ෍ 𝐸 𝑋𝑘 𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ 𝑛
𝑘=0
𝑛−1

= ෍ 𝐸 𝑋𝑘 ∙ 𝐸 𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ 𝑛
𝑘=0
1
𝑛−1 𝑛−1 𝑛−1 Linearity of expectation; 𝐸 𝑋𝑘 = .
1 1 1 n
= ෍𝐸 𝑇 𝑘 + ෍𝐸 𝑇 𝑛−𝑘−1 + ෍Θ 𝑛
𝑛 𝑛 𝑛
𝑘=0 𝑘=0 𝑘=0

24
Analysis of Quick Sort 2
• Random Pivoting
𝑛−1

𝑇 𝑛 = ෍ 𝑋𝑘 𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ 𝑛
𝑘=0
𝑛−1

𝐸𝑇 𝑛 = 𝐸 ෍ 𝑋𝑘 𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ 𝑛
𝑘=0
𝑛−1

= ෍ 𝐸 𝑋𝑘 𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ 𝑛
𝑘=0
𝑛−1

= ෍ 𝐸 𝑋𝑘 ∙ 𝐸 𝑇 𝑘 + 𝑇 𝑛 − 𝑘 − 1 + Θ 𝑛
𝑘=0
𝑛−1 𝑛−1 𝑛−1
1 1 1
= ෍𝐸 𝑇 𝑘 + ෍𝐸 𝑇 𝑛−𝑘−1 + ෍Θ 𝑛
𝑛 𝑛 𝑛
𝑘=0 𝑘=0 𝑘=0
𝑛−1 Summations have identical terms.
2
= ෍𝐸 𝑇 𝑘 +Θ 𝑛
𝑛
𝑘=0

25
Analysis of Quick Sort 2
• Random Pivoting
𝑛−1
2
𝐸𝑇 𝑛 = ෍𝐸 𝑇 𝑘 +Θ 𝑛
𝑛
𝑘=2
(The 𝑘 = 0, 1 terms can be absorbed in the Θ 𝑛 .)

Prove: 𝐸 𝑇 𝑛 ≤ 𝑎𝑛 log 𝑛 for constant 𝑎 > 0.


• Choose 𝑎 large enough so that 𝑎𝑛 log 𝑛 dominates 𝐸 𝑇 𝑛 for sufficiently small 𝑛 ≥ 2.
• Use 𝑛−1
1 1
෍ 𝑘 log 𝑘 ≤ 𝑛2 log 𝑛 − 𝑛2
2 8
𝑘=2

26
Analysis of Quick Sort 2
• Random Pivoting
𝑛−1
2
𝐸𝑇 𝑛 = ෍𝐸 𝑇 𝑘 +Θ 𝑛
𝑛
𝑘=2

27
Analysis of Quick Sort 2
• Random Pivoting
𝑛−1
2
𝐸𝑇 𝑛 = ෍𝐸 𝑇 𝑘 +Θ 𝑛
𝑛
𝑘=2
Substitute inductive hypothesis.
𝑛−1
2
≤ ෍ 𝑎𝑘 log 𝑘 + Θ 𝑛
𝑛
𝑘=2

28
Analysis of Quick Sort 2
• Random Pivoting
𝑛−1
2
𝐸𝑇 𝑛 = ෍𝐸 𝑇 𝑘 +Θ 𝑛
𝑛
𝑘=2
𝑛−1
2
≤ ෍ 𝑎𝑘 log 𝑘 + Θ 𝑛
𝑛 𝑛−1
𝑘=2 1 1
Use ෍ 𝑘 log 𝑘 ≤ 𝑛2 log 𝑛 − 𝑛2
2 8
2𝑎 1 2 1 𝑘=2
≤ 𝑛 log 𝑛 − 𝑛2 + Θ 𝑛
𝑛 2 8

29
Analysis of Quick Sort 2
• Random Pivoting
𝑛−1
2
𝐸𝑇 𝑛 = ෍𝐸 𝑇 𝑘 +Θ 𝑛
𝑛
𝑘=2
𝑛−1
2
≤ ෍ 𝑎𝑘 log 𝑘 + Θ 𝑛
𝑛
𝑘=2

2𝑎 1 2 1
≤ 𝑛 log 𝑛 − 𝑛2 + Θ 𝑛
𝑛 2 8

𝑎𝑛
= 𝑎𝑛 log 𝑛 − −Θ 𝑛
4

30
Analysis of Quick Sort 2
• Random Pivoting
𝑛−1
2
𝐸𝑇 𝑛 = ෍𝐸 𝑇 𝑘 +Θ 𝑛
𝑛
𝑘=2
𝑛−1
2
≤ ෍ 𝑎𝑘 log 𝑘 + Θ 𝑛
𝑛
𝑘=2

2𝑎 1 2 1
≤ 𝑛 log 𝑛 − 𝑛2 + Θ 𝑛
𝑛 2 8

𝑎𝑛
= 𝑎𝑛 log 𝑛 − −Θ 𝑛
4 𝑎𝑛
if 𝑎 is chosen large enough so that dominates the Θ 𝑛 .
4
≤ 𝑎𝑛 log 𝑛

31
Analysis of Quick Sort 2
• Random Pivoting
𝑛−1
2
𝐸𝑇 𝑛 = ෍𝐸 𝑇 𝑘 +Θ 𝑛
𝑛
𝑘=2
𝑛−1
2
≤ ෍ 𝑎𝑘 log 𝑘 + Θ 𝑛
𝑛
𝑘=2

2𝑎 1 2 1
≤ 𝑛 log 𝑛 − 𝑛2 + Θ 𝑛
𝑛 2 8

𝑎𝑛
= 𝑎𝑛 log 𝑛 − −Θ 𝑛
4

≤ 𝑎𝑛 log 𝑛

→ Expected worst case time complexity: O(n Log n)

32
Quick Sort 3
• Pick median as pivot
- Find the median first, then partition the array around the median element.
- K’th Smallest/Largest Element in Unsorted Array method for finding the median.

• QuickSelect: worst 𝚯 𝒏𝟐 , average 𝚯 𝒏 • Modified QuickSelect: worst 𝚯 𝒏


- Select a pivot that divides array in a balanced way

→ Worst case time complexity: O(n Log n)


33
Notes
• Although the worst case time complexity of QuickSort is 𝑶 𝒏𝟐 which is more than
many other sorting algorithms like Merge Sort and Heap Sort, QuickSort is faster in
practice, because its inner loop can be efficiently implemented on most architectures,
and in most real-world data.
• Why Quick Sort is preferred over Merge Sort for sorting Arrays?
→ Quick Sort in its general form is an in-place sort (i.e. it doesn’t require any extra storage) whereas
merge sort requires O(N) extra storage, N denoting the array size which may be quite expensive.

• Why MergeSort is preferred over QuickSort for Linked Lists?


→ In case of linked lists the case is different mainly due to difference in memory allocation of arrays and
linked lists. Unlike arrays, linked list nodes may not be adjacent in memory. Unlike array, in linked list,
we can insert items in the middle in O(1) extra space and O(1) time. Therefore merge operation of
merge sort can be implemented without extra space for linked lists.

34
Reference

• Charles Leiserson and Piotr Indyk, “Introduction to Algorithms”, September


29, 2004

• [Link]

You might also like