0% found this document useful (0 votes)
7 views75 pages

Lecture 7

1. The document discusses the divide and conquer algorithm design paradigm. It explains that divide and conquer algorithms work by dividing a problem into smaller subproblems, solving the subproblems recursively, and combining the solutions to solve the original problem. 2. Binary search and merge sort are provided as examples of divide and conquer algorithms. Binary search divides the search space in half at each step. Merge sort divides an array in half and recursively sorts the subarrays before merging the sorted halves. 3. The agenda outlines that the document will cover divide and conquer design, binary search, merge sort, quick sort, and compare analysis of algorithms.

Uploaded by

SA AN
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)
7 views75 pages

Lecture 7

1. The document discusses the divide and conquer algorithm design paradigm. It explains that divide and conquer algorithms work by dividing a problem into smaller subproblems, solving the subproblems recursively, and combining the solutions to solve the original problem. 2. Binary search and merge sort are provided as examples of divide and conquer algorithms. Binary search divides the search space in half at each step. Merge sort divides an array in half and recursively sorts the subarrays before merging the sorted halves. 3. The agenda outlines that the document will cover divide and conquer design, binary search, merge sort, quick sort, and compare analysis of algorithms.

Uploaded by

SA AN
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/ 75

Analysis, Design of Algorithms

CS3610

Dr. Islam Hegazy


Associate Professor

F2023 Analysis, Design of Algorithms 1


Objectives

By the end of this lecture, you will:

Use a divide-and-conquer algorithm to solve an appropriate problem.

Identify a practical example to which it would apply.

Be able to implement common O(NlogN) sorting algorithms

F2023 Analysis, Design of Algorithms 2


Agenda

01 Divide and conquer 02 Binary search

03 Merge sort 04 Quick sort

05 Comparison

F2023 Analysis, Design of Algorithms 3


Divide and conquer

What?
Divide the problem into small distinct sub-problem(s)
Conquer the sub-problems by solving them recursively in same manner
1. Divide and conquer

Combine the solution of sub-problems to get the final solution

1) Divide

2) Conquer . . .

. . . . . . 3) Combine
. . .
. . .
. . .
. . . . . . . . .

F2023 Analysis, Design of Algorithms 4


Divide and conquer

Why?
Often leads to efficient solutions.
1. Divide and conquer

It’s RECURSIVE by nature!... WHY?


Fun(…)
….
1) Divide
….
2) Conquer
MATCH?
Fun(…), Fun(…),
MATCH?

3) Combine …etc
….
….
T(N) = Time of Normal Code + T(Recursive Code) + …
(Divide & Combine) etc (Conquer)
F2023 Analysis, Design of Algorithms 5
Divide and conquer

Typical case
Problem of size n
Subdivided into b instances, b>1
1. Divide and conquer

a instances need to be solved, a>=1


General divide and conquer recurrence
T(n) = aT(n/b) + f(n)
where f (n) is time spent on dividing
an instance of size n into instances of
size n/b and combining their solutions.

F2023 Analysis, Design of Algorithms 6


Divide and conquer
1. Divide and conquer

F2023 Analysis, Design of Algorithms 7


Agenda

01 Divide and conquer 02 Binary search

03 Merge sort 04 Quick sort

05 Comparison

F2023 Analysis, Design of Algorithms 8


Binary search

Ex.1: Binary Search


Divide: check the middle element
Conquer: Recursively search in ONE half
2. Binary search

Combine Trivial
• Analysis: T(N) = O(Divide & Combine) + T(Conquer)
1. T(N) = 1 T(N/2) + Θ(1)
2. Master Case#2: Θ(log(N))
vs

Item < = >


vs
< = >
. DEMO
.
F2023 .
Analysis, Design of Algorithms 9
Agenda

01 Divide and conquer 02 Binary search

03 Merge sort 04 Quick sort

05 Comparison

F2023 Analysis, Design of Algorithms 10


Merge sort

Ex.2: Merge Sort


Divide: Trivial (split into two halves)
Conquer: Recursively sort 2 subarrays (2 halves)
Combine: Merge two sorted subarrays
3. Merge sort

• Analysis: T(N) = O(Divide & Combine) + T(Conquer)


1. T(N) = 2 T(N/2) + Θ(N)
2. Master Case#2: Θ(N log(N))
Sorted N

Sorted N/2 N/2 Sorted


Merge .
.
Sorted N/4 N/4 Sorted .
F2023 Merge Analysis, Design of Algorithms 11
Merge sort

Ex.2: Merge Sort


Trace: 7, 2, 9, 4, 3, 8, 6, 1
Partition
7 2 9 4⏐3 8 6 1
3. Merge sort

7 2 9 4 3 8 6 1 → 1 3 8 6

9 4 → 4 3 8 → 3
7 2 6 1 → 1 6
9 8

7→ 2→ 9→ 4→ 3→ 8→ 6→ 1→
7 2 9 4 3 . 8 6 1
.
.
F2023 Analysis, Design of Algorithms 12
Merge sort

Ex.2: Merge Sort


Trace: 7, 2, 9, 4, 3, 8, 6, 1
Recursive call, partition
7 2 9 4⏐3 8 6 1
3. Merge sort

7 2⏐9 4 3 8 6 1 → 1 3 8 6

7 2 → 2 9 4 → 4 3 8 → 3
6 1 → 1 6
7 9 8

7→ 2→ 9→ 4→ 3→ 8→ 6→ 1→
7 2 9 4 3 . 8 6 1
.
.
F2023 Analysis, Design of Algorithms 13
Merge sort

Ex.2: Merge Sort


Trace: 7, 2, 9, 4, 3, 8, 6, 1
Recursive call, partition
7 2 9 4⏐3 8 6 1
3. Merge sort

7 2⏐9 4 3 8 6 1 → 1 3 8 6

9 4 → 4 3 8 → 3
7⏐2 6 1 → 1 6
9 8

7→ 2→ 9→ 4→ 3→ 8→ 6→ 1→
7 2 9 4 3 . 8 6 1
.
.
F2023 Analysis, Design of Algorithms 14
Merge sort

Ex.2: Merge Sort


Trace: 7, 2, 9, 4, 3, 8, 6, 1
Recursive call, base case
7 2 9 4⏐3 8 6 1
3. Merge sort

7 2⏐9 4 3 8 6 1 → 1 3 8 6

9 4 → 4 3 8 → 3
7⏐2 6 1 → 1 6
9 8

7→ 2→ 9→ 4→ 3→ 8→ 6→ 1→
7 2 9 4 3 . 8 6 1
.
.
F2023 Analysis, Design of Algorithms 15
Merge sort

Ex.2: Merge Sort


Trace: 7, 2, 9, 4, 3, 8, 6, 1
Recursive call, base case
7 2 9 4⏐3 8 6 1
3. Merge sort

7 2⏐9 4 3 8 6 1 → 1 3 8 6

9 4 → 4 3 8 → 3
7⏐2 6 1 → 1 6
9 8

7→ 9→ 4→ 3→ 8→ 6→ 1→
2→2
7 9 4 3 . 8 6 1
.
.
F2023 Analysis, Design of Algorithms 16
Merge sort

Ex.2: Merge Sort


Trace: 7, 2, 9, 4, 3, 8, 6, 1
Merge
7 2 9 4⏐3 8 6 1
3. Merge sort

7 2⏐9 4 3 8 6 1 → 1 3 8 6

9 4 → 4 3 8 → 3
7⏐2→2 7 6 1 → 1 6
9 8

7→ 9→ 4→ 3→ 8→ 6→ 1→
2→2
7 9 4 3 . 8 6 1
.
.
F2023 Analysis, Design of Algorithms 17
Merge sort

Ex.2: Merge Sort


Trace: 7, 2, 9, 4, 3, 8, 6, 1
Recursive call, partition, base case, merge
7 2 9 4⏐3 8 6 1
3. Merge sort

7 2⏐9 4 3 8 6 1 → 1 3 8 6

3 8 → 3
7⏐2→2 7 9 4 → 4 9 6 1 → 1 6
8

7→ 4→ 3→ 8→ 6→ 1→
2→2 9→9
7 4 3 . 8 6 1
.
.
F2023 Analysis, Design of Algorithms 18
Merge sort

Ex.2: Merge Sort


Trace: 7, 2, 9, 4, 3, 8, 6, 1
Merge
7 2 9 4⏐3 8 6 1
3. Merge sort

7 2⏐9 4→ 2 4 7 9 3 8 6 1 → 1 3 8 6

3 8 → 3
7⏐2→2 7 9 4 → 4 9 6 1 → 1 6
8

7→ 4→ 3→ 8→ 6→ 1→
2→2 9→9
7 4 3 . 8 6 1
.
.
F2023 Analysis, Design of Algorithms 19
Merge sort

Ex.2: Merge Sort


Trace: 7, 2, 9, 4, 3, 8, 6, 1
Recursive call, partition, base case, merge
7 2 9 4⏐3 8 6 1
3. Merge sort

7 2⏐9 4→ 2 4 7 9 3 8 6 1 → 1 3 6 8

3 8 → 3
7⏐2→2 7 9 4 → 4 9 6 1 → 1 6
8

7→ 4→ 3→
2→2 9→9 8→8 6→6 1→1
7 4 3 .
.
.
F2023 Analysis, Design of Algorithms 20
Merge sort

Ex.2: Merge Sort


Trace: 7, 2, 9, 4, 3, 8, 6, 1
Merge
7 2 9 4⏐3 8 6 1 → 1 2 3 4 6 7 8 9
3. Merge sort

7 2⏐9 4→ 2 4 7 9 3 8 6 1 → 1 3 6 8

3 8 → 3
7⏐2→2 7 9 4 → 4 9 6 1 → 1 6
8

7→ 4→ 3→
2→2 9→9 8→8 6→6 1→1
7 4 3 .
.
.
F2023 Analysis, Design of Algorithms 21
Merge sort

Idea for merging


Two piles of sorted cards s m e
1 2 3 4 5 6 7 8
Choose the smaller of the two top cards
2 4 5 7 1 2 3 6
Remove it and place it in the output pile
3. Merge sort

Repeat the process until one pile is empty


Take the remaining input pile and place it face-down onto the output pile

.
.
.
F2023 Analysis, Design of Algorithms 22
Merge sort

Merge example
3. Merge sort

14 23 45 98 6 33 42 67

.
.
.
F2023 Analysis, Design of Algorithms 23
Merge sort

Merge example
3. Merge sort

14 23 45 98 6 33 42 67

Merge .
.
.
F2023 Analysis, Design of Algorithms 24
Merge sort

Merge example
3. Merge sort

14 23 45 98 6 33 42 67

Merge .
.
.
F2023 Analysis, Design of Algorithms 25
Merge sort

Merge example
3. Merge sort

14 23 45 98 6 33 42 67

6 14

Merge .
.
.
F2023 Analysis, Design of Algorithms 26
Merge sort

Merge example
3. Merge sort

14 23 45 98 6 33 42 67

6 14 23

Merge .
.
.
F2023 Analysis, Design of Algorithms 27
Merge sort

Merge example
3. Merge sort

14 23 45 98 6 33 42 67

6 14 23 33

Merge .
.
.
F2023 Analysis, Design of Algorithms 28
Merge sort

Merge example
3. Merge sort

14 23 45 98 6 33 42 67

6 14 23 33 42

Merge
F2023 Analysis, Design of Algorithms 29
Merge sort

Merge example
3. Merge sort

14 23 45 98 6 33 42 67

6 14 23 33 42 45

Merge
F2023 Analysis, Design of Algorithms 30
Merge sort

Merge example
3. Merge sort

14 23 45 98 6 33 42 67

6 14 23 33 42 45 67

Merge
F2023 Analysis, Design of Algorithms 31
Merge sort

Merge example
3. Merge sort

14 23 45 98 6 33 42 67

6 14 23 33 42 45 67 98

Merge
F2023 Analysis, Design of Algorithms 32
Merge sort

Analysis of Merge sort


Divide: The divide step just computes the middle of the sub-array, which
takes constant time. Thus, D(n)=O(1)
Conquer: sort two problems of size (n/2), 🡺 running time = 2 T(n/2)
3. Merge sort

Combine: MERGE n-element 🡺takes time Θ(n)

Θ(1) if n = 1
T (n) =
2T (n/2)+ Θ(n) if n > 1

Master Case#2 🡺 Θ(N log(N))


F2023 Analysis, Design of Algorithms 33
Merge sort

Pseudocode s m e
Alg.: MERGE-SORT(A, s, e) 1 2 3 4 5 6 7 8

5 2 4 7 1 3 2 6
if s < e

then m ← ⎣(s + e)/2⎦


3. Merge sort

Divide
MERGE-SORT(A, s, m) Where’s
Conquer each step
MERGE-SORT(A, m + 1, e) ???
MERGE(A, s, m, e) Combine

• Initial call: MERGE-SORT(A, 1, n)


F2023 Analysis, Design of Algorithms 34
Merge sort

s m e
Alg.: MERGE(A, s, m, e) 1 2 3 4 5 6 7 8

1. Compute n1 and n2 2 4 5 7 1 2 3 6

2. Copy the first n1 elements into L[1 . . n1 + 1]


n1 n2
3. Copy the next n2 elements into R[1 . . n2 + 1]
3. Merge sort

4. L[n1 + 1] ← ∞; R[n2 + 1] ← ∞ s m

5. i ← 1; j ← 1 L 2 4 5 7 ∞
6. for k ← s to e m+1 e

7. do if L[ i ] ≤ R[ j ] R 1 2 3 6 ∞
8. then A[k] ← L[ i ]
9. i ←i + 1
10. else A[k] ← R[ j ]
11. j←j+1
F2023 Analysis, Design of Algorithms 35
Merge sort

Discussion
Running time insensitive of the input
Advantage:
Guaranteed to run in Θ(nlgn)
3. Merge sort

Can be easily parallelized


Disadvantage:
Out-place: requires extra space ≈N
Can be reduced to N/2… do you know HOW?!
By moving the FIRST half into a new array of size N/2 while keeping the SECOND
half in the original array. Then merge both into the original array.
F2023 Analysis, Design of Algorithms 36
Agenda

01 Divide and conquer 02 Binary search

03 Merge sort 04 Quick sort

05 Comparison

F2023 Analysis, Design of Algorithms 37


Quick sort

Ex.3: Quick Sort


Divide:
Select a pivot
Place it in its correct position (Move everything smaller to left and everything
4. Quick sort

else to the right)


Conquer: Recursively sort 2 subarrays
Combine: Trivial
N
Analysis:
• Sort P <T(N) = O(Divide & Combine)
> + T(Conquer)
• T(N) = T(L) + T(N – L) + Θ(N)
Sort <P• L: length
> of one of the
P two subarrays
< Sort
• Depend on the input array and the pivot selection
F2023 Analysis, Design of Algorithms 38
Quick sort

Given an array of n elements (e.g., integers):


If array only contains one element, return
Else
pick one element to use as pivot.
4. Quick sort

Partition elements into two sub-arrays:


Elements less than or equal to pivot Divide

Elements greater than pivot


Quicksort two sub-arrays Conquer
Return results

F2023 Analysis, Design of Algorithms 39


Quick sort

Ex.3: Quick Sort


Trace: 40, 20, 10, 80, 60, 50, 7, 30, 100
There are a number of ways to pick the pivot element.
In this example, we will use the first element in the array:
4. Quick sort

10
40 20 10 80 60 50 7 30
0

F2023 Analysis, Design of Algorithms 40


Quick sort

Place pivot in its position


4. Quick sort

pivot_index = 0
10
40 20 10 80 60 50 7 30
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]

i j

F2023 Analysis, Design of Algorithms 41


Quick sort

1. While data[i] <= data[pivot]


++i
4. Quick sort

pivot_index = 0
10
40 20 10 80 60 50 7 30
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]

i j

F2023 Analysis, Design of Algorithms 42


Quick sort

1. While data[i] <= data[pivot]


++i
4. Quick sort

pivot_index = 0
10
40 20 10 80 60 50 7 30
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]

i j

F2023 Analysis, Design of Algorithms 43


Quick sort

1. While data[i] <= data[pivot]


++i
4. Quick sort

pivot_index = 0
10
40 20 10 80 60 50 7 30
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]

i j

F2023 Analysis, Design of Algorithms 44


Quick sort

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
4. Quick sort

pivot_index = 0
10
40 20 10 80 60 50 7 30
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]

i j

F2023 Analysis, Design of Algorithms 45


Quick sort

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
4. Quick sort

pivot_index = 0
10
40 20 10 80 60 50 7 30
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]

i j

F2023 Analysis, Design of Algorithms 46


Quick sort

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. Quick sort

pivot_index = 0
10
40 20 10 80 60 50 7 30
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]

i j

F2023 Analysis, Design of Algorithms 47


Quick sort

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. Quick sort

pivot_index = 0
10
40 20 10 30 60 50 7 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]

i j

F2023 Analysis, Design of Algorithms 48


Quick sort

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. Quick sort

4. While j > i, go to 1

pivot_index = 0
10
40 20 10 30 60 50 7 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]

i j

F2023 Analysis, Design of Algorithms 49


Quick sort

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. Quick sort

4. While j > i, go to 1

pivot_index = 0
10
40 20 10 30 60 50 7 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]

i j

F2023 Analysis, Design of Algorithms 50


Quick sort

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. Quick sort

4. While j > i, go to 1

pivot_index = 0
10
40 20 10 30 60 50 7 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]

i j

F2023 Analysis, Design of Algorithms 51


Quick sort

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. Quick sort

4. While j > i, go to 1

pivot_index = 0
10
40 20 10 30 60 50 7 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]

i j

F2023 Analysis, Design of Algorithms 52


Quick sort

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. Quick sort

4. While j > i, go to 1

pivot_index = 0
10
40 20 10 30 7 50 60 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]

i j

F2023 Analysis, Design of Algorithms 53


Quick sort

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. Quick sort

4. While j > i, go to 1

pivot_index = 0
10
40 20 10 30 7 50 60 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]

i j

F2023 Analysis, Design of Algorithms 54


Quick sort

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. Quick sort

4. While j > i, go to 1

pivot_index = 0
10
40 20 10 30 7 50 60 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]

i j

F2023 Analysis, Design of Algorithms 55


Quick sort

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. Quick sort

4. While j > i, go to 1

pivot_index = 0
10
40 20 10 30 7 50 60 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]

i j

F2023 Analysis, Design of Algorithms 56


Quick sort

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. Quick sort

4. While j > i, go to 1

pivot_index = 0
10
40 20 10 30 7 50 60 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]

i j

F2023 Analysis, Design of Algorithms 57


Quick sort

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. Quick sort

4. While j > i, go to 1

pivot_index = 0
10
40 20 10 30 7 50 60 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]

i j

F2023 Analysis, Design of Algorithms 58


Quick sort

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. Quick sort

4. While j > i, go to 1

pivot_index = 0
10
40 20 10 30 7 50 60 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]

i j

F2023 Analysis, Design of Algorithms 59


Quick sort

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. Quick sort

4. While j > i, go to 1

pivot_index = 0
10
40 20 10 30 7 50 60 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]

i j

F2023 Analysis, Design of Algorithms 60


Quick sort

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. Quick sort

4. While j > i, go to 1

pivot_index = 0
10
40 20 10 30 7 50 60 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]

i j

F2023 Analysis, Design of Algorithms 61


Quick sort

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. Quick sort

4. While j > i, go to 1
5. Swap data[j] and data[pivot_index]

pivot_index = 0
10
40 20 10 30 7 50 60 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]

i j

F2023 Analysis, Design of Algorithms 62


Quick sort

1. While data[i] <= data[pivot]


++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. Quick sort

4. While j > i, go to 1
5. Swap data[j] and data[pivot_index]
10
pivot_index = 4 7 20 10 30 40 50 60 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]

i j

F2023 Analysis, Design of Algorithms 63


Quick sort

Partition result

10
7 20 10 30 40 50 60 80
0
4. Quick sort

[0] [1] [2] [3] [4] [5] [6] [7] [8]

<= data[pivot] > data[pivot]

F2023 Analysis, Design of Algorithms 64


Quick sort

Recursion: Quicksort Sub-arrays

10
7 20 10 30 40 50 60 80
0
4. Quick sort

[0] [1] [2] [3] [4] [5] [6] [7] [8]

<= data[pivot] > data[pivot]

F2023 Analysis, Design of Algorithms 65


Quick sort

What would the BEST case be?


Each call to Partition splits the array into two equal parts
4. Quick sort

1. T(N) = 2 T(N/2) + Θ(N)


2. Master Case#2: Θ(N log(N))
F2023 Analysis, Design of Algorithms 66
Quick sort

What would the WORST case be?


Each call to Partition splits the array into ONE part only

Partition n - 1
4. Quick sort

▪ When does this happen?


✔ sorted
❑ 1. T(N) = T(N – 1) + Θ(N)
✔ reverse sorted
❑ 2. Iteration/tree method 🡺 Θ(N2)
❑ near sorted
❑ random
F2023 Analysis, Design of Algorithms 67
Quick sort

How to Avoid N2 Worst Case?


Randomly shuffle the input array in Θ(N) before sorting
Good choice for the pivot, may be:
The element in the middle position
4. Quick sort

Median of first, last, and middle


Median of k sampled elements
A random element
Avoids the worst case on a completely sorted list.
Expected time is O(NLog(N)) for ALL input arrays.

F2023 Analysis, Design of Algorithms 68


Agenda

01 Divide and conquer 02 Binary search

03 Merge sort 04 Quick sort

05 Comparison

F2023 Analysis, Design of Algorithms 69


Sorting comparison

Selection sort:
Easy idea (Bruteforce)
Min number of swaps (O(N) swaps)
O(n2) in ALL cases
5. Comparison

Bubble sort:
Easy idea (Bruteforce)
O(n) sorted case
O(n2) in other cases

F2023 Analysis, Design of Algorithms 70


Sorting comparison

Insertion sort:
Easy to code
Fast on small inputs (less than ~50 elements)
Fast on nearly-sorted inputs
5. Comparison

O(n) sorted case


O(n2) worst case
O(n2) average case
O(n2) reverse-sorted case

F2023 Analysis, Design of Algorithms 71


Sorting comparison

Merge sort:
Divide-and-conquer:
Split array in half
Recursively sort subarrays
5. Comparison

Merge step complexity is O(n)


O(n lg n) worst case
Can be easily parallelized
Needs auxiliary memory

F2023 Analysis, Design of Algorithms 72


Sorting comparison

Quick sort:
Divide-and-conquer:
Recursive sort
Partition array into two subarrays (1st subarray < 2nd subarray)
5. Comparison

No combine step needed


O(n lg n) average case
Fast in practice
O(n2) worst case
If Pivot is 1st element: Worst case on reverse/sorted input

F2023 Analysis, Design of Algorithms 73


Sorting comparison

Quick sort:
Can be enhanced by
Randomly shuffle the input array
Randomly select the pivot
5. Comparison

F2023 Analysis, Design of Algorithms 74


F2023 Analysis, Design of Algorithms 75

You might also like