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

KU05 AA DivideAndConquer 02

Divide and conquer section 2

Uploaded by

bobjan6900
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)
6 views

KU05 AA DivideAndConquer 02

Divide and conquer section 2

Uploaded by

bobjan6900
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/ 18

Divide and

Conquer

Analysing
Divide and
Conquer
Algorithm
Computer Science Faculty - Software General Equation
Master Theorem

Engineering Quick Sort


Algorithm
Analysis of Algorithms Introduction
Analysing Quick
Sort

Divide and
Conquer
Analysis of Algorithms‘ Course Lectures Algorithm
(without
( Divide and Conquer ) Combination)
Binary Search

Ali Shah Saber


[email protected] | telegram: @alishahsaber
Fall, 2024
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Divide and
Session Overview Conquer

Analysing
Divide and
Conquer
Algorithm
1 Analysing Divide and Conquer Algorithm General Equation
Master Theorem
General Equation
Quick Sort
Master Theorem Algorithm
Introduction
Analysing Quick
Sort

2 Quick Sort Algorithm Divide and


Conquer
Introduction Algorithm
(without
Analysing Quick Sort Combination)
Binary Search

3 Divide and Conquer Algorithm (without Combination)


Binary Search

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Divide and
Analysing Divide and Conquer Algorithm Conquer

Analysing
In general we have the following recurrence equation: Divide and
Conquer
{ Algorithm

θ(n) if n <= c, General Equation

T(n) = Master Theorem

aT(n/b) + D(n) + C(n) Otherwise. Quick Sort


Algorithm
Introduction

Where Analysing Quick


Sort

1 T(n): is the time requred for an input of size n Divide and


Conquer
2 n: is the size of problem Algorithm
(without
Combination)
3 c: is a constant number Binary Search

4 a: is the number of subproblems


5 n/b: is the size of each subproblem
6 D(n): is the time needed for Divide
7 C(n): is the time needed for Combine

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Divide and
Solving the recurrence equations Conquer

Analysing
Divide and
Conquer
Algorithm
General Equation
Master Theorem

There are different approaches to do this Quick Sort


Algorithm

1 Constructing Recursion Tree Introduction


Analysing Quick
Sort

2 Performing Substitution Divide and


Conquer
3 Using Induction Algorithm
(without
4 Master Theorem Combination)
Binary Search

5 Generating Functions

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Master Thearem

. . . .... .... .... . . . . .


Divide and
Conquer

Analysing
Divide and
Conquer
Algorithm
General Equation
Master Theorem

What are the implicit formula of T(n)? Quick Sort


Algorithm

1 T(n) = 9T(n/3) + n Introduction


Analysing Quick
Sort

2 T(n) = T(2n/3) + 1 Divide and


Conquer
3 T(n) = 3T(n/4) + n log n Algorithm
(without
Combination)
Binary Search

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Divide and
QuickSort Conquer

Analysing
Divide Divide and
Conquer
for sorting A[p..r], Partition A[p..r], into two subarrays Algorithm
General Equation
A[p..q1] and A[q + 1..r], such that each element in the Master Theorem

first subarray A[p..q1] is <= A[q] and A[q] is <= each Quick Sort
Algorithm
element in the second subarray A[q + 1..r]. Introduction
Analysing Quick
Sort

Conquer Divide and


Conquer
Sort the two subarrays by recursive calls to QUICKSORT. Algorithm
(without
Combination)
Combine Binary Search

No work is needed, because they are sorted in place.


Perform the divide step by a procedure PARTITION,
which returns the index q that marks the position
separating the subarrays.

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Divide and
QuickSort Algorithm Conquer

Analysing
Divide and
Conquer
Algorithm
General Equation
Master Theorem

Quick Sort
Algorithm
Introduction
Analysing Quick
Sort

Divide and
Conquer
Algorithm
(without
Combination)
Binary Search

To sort an entire array A, the initial call is


QUICKSORT(A, 1, A.length).

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Divide and
QuickSort Algorithm Conquer

Analysing
Divide and
Conquer
Algorithm
General Equation
Master Theorem

Quick Sort
Algorithm
Introduction
Analysing Quick
Sort

Divide and
Conquer
Algorithm
(without
Combination)
Binary Search

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Divide and
QuickSort Algorithm Conquer

Analysing
Divide and
Conquer
Algorithm
General Equation
Master Theorem

Quick Sort
Trace QuickSort with these samples: Algorithm
Introduction
First Example: Analysing Quick
Sort

A = [15, 22, 13, 27, 12, 10, 20, 25] Divide and
Second Example: Conquer
Algorithm
A = [2, 8, 7, 1, 3, 5, 6, 4] (without
Combination)
Trace overall work procedure and partition procedure Binary Search

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Divide and
Conquer

Analysing
Divide and
Conquer
Algorithm
General Equation
Master Theorem

Quick Sort
Algorithm
Introduction
Analysing Quick
Sort

Divide and
Conquer
Algorithm
(without
Combination)
Binary Search

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Divide and
Analysing Quick Sort Algorithm Conquer

Worse Case Analysing


Divide and
Conquer
Algorithm
General Equation

T(n) = T(n − 1) + θ(n) + T(0)


Master Theorem

Quick Sort
= T(n − 1) + Θ(n) Algorithm
Introduction
Analysing Quick
Sort

from arithmetic series we have: Divide and


Conquer
Algorithm

n
1 (without
k = n (n − 1) Combination)
2 Binary Search

k=1
= Θ(n2 )

So

T(n) = θ(n2 )
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Divide and
Analysing Quick Sort Algorithm Conquer

Analysing
Divide and
Conquer
Algorithm
Best Case General Equation
Master Theorem

• Occurs when the subarrays are completely balanced Quick Sort


Algorithm
every time. Introduction

• Each subarray has <= n/2 elements.


Analysing Quick
Sort

Divide and
Conquer
T(n) = 2T(n/2) + θ(n) Algorithm
(without
Combination)
Binary Search
So

T(n) = θ(n log n)

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Divide and
Analysing Quick Sort Algorithm Conquer

Analysing
Divide and
Conquer
Average Case Algorithm
General Equation

• Quicksort’s average running time is much closer to


Master Theorem

Quick Sort
the best case than to the worst case. Algorithm
Introduction

• Imagine that PARTITION always produces a 9-to-1 Analysing Quick


Sort

split. Divide and


Conquer
Algorithm
(without
T(n) = T(9n/10) + T(n/10) + θ(n) Combination)
Binary Search

So

T(n) = O(n log n)

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Analysing Quick Sort Algorithm - Average
Case

. . . .... .... .... . . . . .


Divide and
Binary Search Conquer

Analysing
Divide and
Conquer
An example of Divide and Conquer with not Containing Algorithm
Combination General Equation
Master Theorem

Looking for number 13 in this example Quick Sort


Algorithm
Introduction
Analysing Quick
Sort

Divide and
Conquer
Algorithm
(without
Combination)
Binary Search

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Divide and
Binary Search Conquer

Analysing
Divide and
Conquer
Algorithm
Divide and Conquer Approach without Combination Step General Equation
Master Theorem

Quick Sort
Binary-Search(x,k){ Algorithm

1. if x == NIL or k == x.key Introduction


Analysing Quick
Sort
2. return x
Divide and
3. if k<x.key Conquer
Algorithm
4. return Binary-Search(x.left, k) (without
Combination)
5. else Binary Search

6. return Binary-Search(x.right, k)
7. }

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
The End
Questions? Comments?

. . . .... .... .... . . . . .

You might also like