際際滷

際際滷Share a Scribd company logo
Design and Analysis of Algorithms
COT 5405
CLASS NOTES
14th
October 2003
Overview:
 Divide and Conquer
 Master theorem
 Master theorem based analysis for
 Binary Search
 Merge Sort
 Quick Sort
Divide and Conquer
Basic Idea:
1. Decompose problems into sub instances.
2. Solve sub instances successively and independently.
3. Combine the sub solutions to obtain the solution to the original
problem.
In order to look into the efficiency of the Divide and Conquer lets look into the
Multiplication of two n-digit Numbers
Traditional Multiplication:
Say we are multiplying 382 with 695(n=3)
382
* 695
- - - - -
Essentially, we are multiplying 1 digit with n other digits and then adding the n numbers,
which can give us a solution of at most 2n digits.
There are n additions, each of O(n) time at most, which gives us the running time of the
algorithm as O(n2
)
!!Using Divide and Conquer to multiply n-digit numbers
We will write the two n-digit numbers as follows:
(10n/2
X + Y) (10n/2
W+Z) =10n
XW + (XZ + YW) 10n/2
+YZ ---(1)
That is we are converting the multiplication of two n-digit numbers into multiplication of
four n/2 digit numbers, plus some extra work involved in additions. We are recursively
calling multiplication and performing some additions in every recursion.
Let T (n) be the running time of multiplying two n-digit numbers.
Then in our case,
T (n) = 4T (n/2) +O (n)
 Four multiplications of n/2 digit numbers
 Addition is going to be between numbers that have atmost 2n digits. Thus
addition can be O (n).
Recursively substituting the value of T (n):
T (n) = 4 [4T (n/4) + O (n/2)] +O (n)
=16 T (n/4) + 4O (n/2) + O (n)
-
-
-
=C T (1) + - - - -
Masters Theorem
Let T(n) be the running time of an algorithm with an input size of n;
Suppose we can run the algorithm in such a way that we make a recursive calls every
time with an input size of n/b and do some extra work in every recursion (additions and
subtractions).
Such that T (n) can be represented as:
T (n) = a T (n/b) + O (nk
),
Then,
If log ba>k, T (n) =O (nlog
b
a
) (recursive calls dominates)
If log ba=k, T (n) =O (nk
log n) (almost equal work in rec. calls and in extra work)
If log ba<k, T (n) =O (nk
) (Extra work dominates)
In our multiplication problem:
T (n) = 4T (n/2) +O (n)
A=4, b=2
Log24=2, k=1
Since algorithm is dominated by recursive calls and the running time is O (n2
).
But this is as good as our traditional multiplication algorithm. Since we now know that
multiplications dominate the running time, if we can reduce the number of
multiplications to three, which can bring down our T(n) by 25%.
To calculate (1), we just need the following 3 multiplications separately:
1. (X+Y)(W+Z) 2 additions and one multiplication
2.XW
3.YZ
Then we can calculate
XZ+YW=(X+Y)(W+Z)-XW-YZ
Thus we use three multiplications at the expense of some extra additions and
subtractions, which run in constant time( each of O(n) time)
Thus,
T(n)=3T(n/2) + O(n)
Applying Masters theorem,
A=3,b=2,k=1
Thus, T(n)=O(nlog
2
3
)
Since log
2
3 ~ 1.5,
We have reduced the total number of recursive calls in our program. For very large n, it will work well but in actual
implementation, we hardly code to gain advantage out if this feature.
Binary Search
Goal: Searching for nth value in a sorted list of length n.
(Divide the list into two and recursively search in the individual lists half the size)
Again,
Let T(n) be the running time of the algorithm. Then,
T(n)=T(n/2) + O(1)
In O(1) time we divide the list into two halves(n/2) that run in T(n/2) time.
Using Masters theorem,
A=1,b=2
Log21=0
K=0;
So,
T(n)=O(log n)
Merge Sort
Goal: Sp litting the element list into 2 lists, sort them and merge them.
T(n)=2T(n/2) + O(n)
Here, the hidden constant is greater than the hiddent constant in the merge sort because
while dividing the lists into two different arrays and then sorting them, we are allocating
extra space and subsequently, copying into the original array.
Using Masters theorem,
A=2,b=2,k=1
Log22=1
So, T(n)=O(n log n)
Quicksort
Goal: Pick one element as the partition element. Put all the elements smaller than it, to
the left of it and all elements greater than it, to the right of it. On the lists left and right of
the partition element recursively call Qsort.
Say the list is: 8,3,6,9,2,4,7,5
Partition element:5
8, 3, 6, 9, 2, 4, 7, 5
8 in the worng place, 7 fine.
8, 3, 6, 9, 2, 4, 7, 5
4 ,3 ,6 ,9 , 2 ,8 ,7 ,5
4, ,3, 2, 9, 6, 8, 7, 5
4, 3 ,2 ,9 ,6 ,8 ,7 ,5
front Last
front Last
front last
front last
last front
Now swap front with 5 and we have 5 in place.
4,3,2,5,6,8,7,9
Thus the only extra space utilized here is the temporary variable used for swapping.
In te worst case, we might end up choosing a partition element which is the first element
in our list.
In that case T(n)=O(n2
)
To make sure this rarely happens:
1. Pick a random partition element.
2. Probablity of picking a good partition element is as low as the probability of
picking a bad one. So, they will even out.
There are n possible partition elements
Element Split Prob(element)
1 0,n-1 1/n
2 1,n-2 1/n
3 1/n
N n-1,0 1/n
Now,
T (n) = 1/n [ T(0) + T(n-1) + O(n) ] +
1/n [ T(1) + T(n-2) + O(n) ] +
1/n [ T(2) + T(n-3) + O(n) ] +



1/n [T (n-1) + T (0) + O (n)]
n * T[n] = {2 k=0裡n-1
T(k)} + O(n2
) A
Substitute n = n-1,
(n-1) * T[(n-1)] = {2 k=0裡n-1
T(k)} + O((n-1)2
)B
Subtract A from B
n T(n) - (n-1)T(n-1) = 2 T(n-1) + O(n)
n T(n) = n+1 T(n-1) + O(n)
T(n) = ((n+1)/n )T(n-1) + O(1)
Divide by (n+1)
T(n)/(n+1) = [T(n-1) ]/ n + O(1/n)  C
Let,
S(n) = T(n)/(n+1)  D
S(n) = S(n-1) + O(1/n)
This can be written as a sum,
= S(n-2) + O(1/n-1) + O(1/n)
= S(n-3) + O(1/n-2) + O(1/n-1) + O(1/n)
S(n) = O(k=1 裡 n
1/k)
= O( Hn)
S(n) = T(n)/(n+1) = O (ln n) E
Substitute D in C
T(n) = S(n) . (n+1)
use E,
T(n) = O (ln n) . (n+1)
T(n) = O (n lg n )
Notes compiled by Shankar Vaithianthan and Harjinder Mandarh
n T(n) - (n-1)T(n-1) = 2 T(n-1) + O(n)
n T(n) = n+1 T(n-1) + O(n)
T(n) = ((n+1)/n )T(n-1) + O(1)
Divide by (n+1)
T(n)/(n+1) = [T(n-1) ]/ n + O(1/n)  C
Let,
S(n) = T(n)/(n+1)  D
S(n) = S(n-1) + O(1/n)
This can be written as a sum,
= S(n-2) + O(1/n-1) + O(1/n)
= S(n-3) + O(1/n-2) + O(1/n-1) + O(1/n)
S(n) = O(k=1 裡 n
1/k)
= O( Hn)
S(n) = T(n)/(n+1) = O (ln n) E
Substitute D in C
T(n) = S(n) . (n+1)
use E,
T(n) = O (ln n) . (n+1)
T(n) = O (n lg n )
Notes compiled by Shankar Vaithianthan and Harjinder Mandarh
Ad

Recommended

lecture 4
lecture 4
sajinsc
Recursion tree method
Recursion tree method
Rajendran
Recurrence theorem
Recurrence theorem
Rajendran
Proof master theorem
Proof master theorem
Rajendran
K10692 control theory
K10692 control theory
saagar264
Master method theorem
Master method theorem
Rajendran
A Note on TopicRNN
A Note on TopicRNN
Tomonari Masada
Analysis of algo
Analysis of algo
Maneesha Srivastav
A Note on Latent LSTM Allocation
A Note on Latent LSTM Allocation
Tomonari Masada
Computational Complexity
Computational Complexity
Kasun Ranga Wijeweera
Recurrences
Recurrences
Dr Sandeep Kumar Poonia
Master method
Master method
arun jacob
Time complexity
Time complexity
Katang Isip
Master theorem
Master theorem
fika sweety
Time complexity (linear search vs binary search)
Time complexity (linear search vs binary search)
Kumar
Time complexity
Time complexity
Kartik Chandra Mandal
Time andspacecomplexity
Time andspacecomplexity
LAKSHMITHARUN PONNAM
Asymptotic notations
Asymptotic notations
Nikhil Sharma
002 ray modeling dynamic systems
002 ray modeling dynamic systems
Institute of Technology Telkom
Asymptotic Notations
Asymptotic Notations
Rishabh Soni
Asymptotic notation
Asymptotic notation
sajinis3
Asymptotic notations
Asymptotic notations
Ehtisham Ali
Big o
Big o
Thanhvinh Vo
Algorithm: Quick-Sort
Algorithm: Quick-Sort
Tareq Hasan
Asymptotic notation
Asymptotic notation
mustafa sarac
Analysis of algorithn class 3
Analysis of algorithn class 3
Kumar
CS330-Lectures Statistics And Probability
CS330-Lectures Statistics And Probability
bryan111472
time_complexity_list_02_04_2024_22_pages.pdf
time_complexity_list_02_04_2024_22_pages.pdf
SrinivasaReddyPolamR
Divide and Conquer in DAA concept. For B Tech CSE
Divide and Conquer in DAA concept. For B Tech CSE
RUHULAMINHAZARIKA
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
AmayJaiswal4

More Related Content

What's hot (18)

A Note on Latent LSTM Allocation
A Note on Latent LSTM Allocation
Tomonari Masada
Computational Complexity
Computational Complexity
Kasun Ranga Wijeweera
Recurrences
Recurrences
Dr Sandeep Kumar Poonia
Master method
Master method
arun jacob
Time complexity
Time complexity
Katang Isip
Master theorem
Master theorem
fika sweety
Time complexity (linear search vs binary search)
Time complexity (linear search vs binary search)
Kumar
Time complexity
Time complexity
Kartik Chandra Mandal
Time andspacecomplexity
Time andspacecomplexity
LAKSHMITHARUN PONNAM
Asymptotic notations
Asymptotic notations
Nikhil Sharma
002 ray modeling dynamic systems
002 ray modeling dynamic systems
Institute of Technology Telkom
Asymptotic Notations
Asymptotic Notations
Rishabh Soni
Asymptotic notation
Asymptotic notation
sajinis3
Asymptotic notations
Asymptotic notations
Ehtisham Ali
Big o
Big o
Thanhvinh Vo
Algorithm: Quick-Sort
Algorithm: Quick-Sort
Tareq Hasan
Asymptotic notation
Asymptotic notation
mustafa sarac
Analysis of algorithn class 3
Analysis of algorithn class 3
Kumar
A Note on Latent LSTM Allocation
A Note on Latent LSTM Allocation
Tomonari Masada
Master method
Master method
arun jacob
Time complexity
Time complexity
Katang Isip
Master theorem
Master theorem
fika sweety
Time complexity (linear search vs binary search)
Time complexity (linear search vs binary search)
Kumar
Asymptotic notations
Asymptotic notations
Nikhil Sharma
Asymptotic Notations
Asymptotic Notations
Rishabh Soni
Asymptotic notation
Asymptotic notation
sajinis3
Asymptotic notations
Asymptotic notations
Ehtisham Ali
Algorithm: Quick-Sort
Algorithm: Quick-Sort
Tareq Hasan
Asymptotic notation
Asymptotic notation
mustafa sarac
Analysis of algorithn class 3
Analysis of algorithn class 3
Kumar

Similar to pradeepbishtLecture13 div conq (20)

CS330-Lectures Statistics And Probability
CS330-Lectures Statistics And Probability
bryan111472
time_complexity_list_02_04_2024_22_pages.pdf
time_complexity_list_02_04_2024_22_pages.pdf
SrinivasaReddyPolamR
Divide and Conquer in DAA concept. For B Tech CSE
Divide and Conquer in DAA concept. For B Tech CSE
RUHULAMINHAZARIKA
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
AmayJaiswal4
T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptx
GadaFarhan
Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sorting
zukun
lec 03wweweweweweweeweweweewewewewee.pdf
lec 03wweweweweweweeweweweewewewewee.pdf
Huma Ayub
Find the compact trigonometric Fourier series for the periodic signal.pdf
Find the compact trigonometric Fourier series for the periodic signal.pdf
arihantelectronics
Algorithm Assignment Help
Algorithm Assignment Help
Programming Homework Help
lecture 15
lecture 15
sajinsc
1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx
pallavidhade2
Algorithms Exam Help
Algorithms Exam Help
Programming Exam Help
lecture 1
lecture 1
sajinsc
Quicksort analysis
Quicksort analysis
Premjeet Roy
Data Structure & Algorithms - Mathematical
Data Structure & Algorithms - Mathematical
babuk110
Quicksort
Quicksort
Gayathri Gaayu
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksort
zukun
DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..
KarthikeyaLanka1
2.pptx
2.pptx
MohAlyasin1
19. algorithms and-complexity
19. algorithms and-complexity
showkat27
CS330-Lectures Statistics And Probability
CS330-Lectures Statistics And Probability
bryan111472
time_complexity_list_02_04_2024_22_pages.pdf
time_complexity_list_02_04_2024_22_pages.pdf
SrinivasaReddyPolamR
Divide and Conquer in DAA concept. For B Tech CSE
Divide and Conquer in DAA concept. For B Tech CSE
RUHULAMINHAZARIKA
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
AmayJaiswal4
T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptx
GadaFarhan
Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sorting
zukun
lec 03wweweweweweweeweweweewewewewee.pdf
lec 03wweweweweweweeweweweewewewewee.pdf
Huma Ayub
Find the compact trigonometric Fourier series for the periodic signal.pdf
Find the compact trigonometric Fourier series for the periodic signal.pdf
arihantelectronics
lecture 15
lecture 15
sajinsc
1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx
pallavidhade2
lecture 1
lecture 1
sajinsc
Quicksort analysis
Quicksort analysis
Premjeet Roy
Data Structure & Algorithms - Mathematical
Data Structure & Algorithms - Mathematical
babuk110
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksort
zukun
DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..
KarthikeyaLanka1
19. algorithms and-complexity
19. algorithms and-complexity
showkat27
Ad

Recently uploaded (20)

OCS Group SG - HPHT Well Design and Operation - SN.pdf
OCS Group SG - HPHT Well Design and Operation - SN.pdf
Muanisa Waras
4th International Conference on Computer Science and Information Technology (...
4th International Conference on Computer Science and Information Technology (...
ijait
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
Jo達o Esperancinha
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
dayananda54
Engineering Mechanics Introduction and its Application
Engineering Mechanics Introduction and its Application
Sakthivel M
NALCO Green Anode Plant,Compositions of CPC,Pitch
NALCO Green Anode Plant,Compositions of CPC,Pitch
arpitprachi123
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Alexandra N. Martinez
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
SharinAbGhani1
Fundamentals of Digital Design_Class_21st May - Copy.pptx
Fundamentals of Digital Design_Class_21st May - Copy.pptx
drdebarshi1993
ElysiumPro Company Profile 2025-2026.pdf
ElysiumPro Company Profile 2025-2026.pdf
info751436
How Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdf
Mina Anis
Microwatt: Open Tiny Core, Big Possibilities
Microwatt: Open Tiny Core, Big Possibilities
IBM
Pavement and its types, Application of rigid and Flexible Pavements
Pavement and its types, Application of rigid and Flexible Pavements
Sakthivel M
Understanding Amplitude Modulation : A Guide
Understanding Amplitude Modulation : A Guide
CircuitDigest
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
resming1
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
djiceramil
Machine Learning - Classification Algorithms
Machine Learning - Classification Algorithms
resming1
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
The basics of hydrogenation of co2 reaction
The basics of hydrogenation of co2 reaction
kumarrahul230759
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
KhadijaKhadijaAouadi
OCS Group SG - HPHT Well Design and Operation - SN.pdf
OCS Group SG - HPHT Well Design and Operation - SN.pdf
Muanisa Waras
4th International Conference on Computer Science and Information Technology (...
4th International Conference on Computer Science and Information Technology (...
ijait
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
Jo達o Esperancinha
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
dayananda54
Engineering Mechanics Introduction and its Application
Engineering Mechanics Introduction and its Application
Sakthivel M
NALCO Green Anode Plant,Compositions of CPC,Pitch
NALCO Green Anode Plant,Compositions of CPC,Pitch
arpitprachi123
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Alexandra N. Martinez
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
SharinAbGhani1
Fundamentals of Digital Design_Class_21st May - Copy.pptx
Fundamentals of Digital Design_Class_21st May - Copy.pptx
drdebarshi1993
ElysiumPro Company Profile 2025-2026.pdf
ElysiumPro Company Profile 2025-2026.pdf
info751436
How Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdf
Mina Anis
Microwatt: Open Tiny Core, Big Possibilities
Microwatt: Open Tiny Core, Big Possibilities
IBM
Pavement and its types, Application of rigid and Flexible Pavements
Pavement and its types, Application of rigid and Flexible Pavements
Sakthivel M
Understanding Amplitude Modulation : A Guide
Understanding Amplitude Modulation : A Guide
CircuitDigest
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
resming1
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
djiceramil
Machine Learning - Classification Algorithms
Machine Learning - Classification Algorithms
resming1
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
The basics of hydrogenation of co2 reaction
The basics of hydrogenation of co2 reaction
kumarrahul230759
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
KhadijaKhadijaAouadi
Ad

pradeepbishtLecture13 div conq

  • 1. Design and Analysis of Algorithms COT 5405 CLASS NOTES 14th October 2003 Overview: Divide and Conquer Master theorem Master theorem based analysis for Binary Search Merge Sort Quick Sort Divide and Conquer Basic Idea: 1. Decompose problems into sub instances. 2. Solve sub instances successively and independently. 3. Combine the sub solutions to obtain the solution to the original problem. In order to look into the efficiency of the Divide and Conquer lets look into the Multiplication of two n-digit Numbers Traditional Multiplication: Say we are multiplying 382 with 695(n=3) 382 * 695 - - - - - Essentially, we are multiplying 1 digit with n other digits and then adding the n numbers, which can give us a solution of at most 2n digits. There are n additions, each of O(n) time at most, which gives us the running time of the algorithm as O(n2 )
  • 2. !!Using Divide and Conquer to multiply n-digit numbers We will write the two n-digit numbers as follows: (10n/2 X + Y) (10n/2 W+Z) =10n XW + (XZ + YW) 10n/2 +YZ ---(1) That is we are converting the multiplication of two n-digit numbers into multiplication of four n/2 digit numbers, plus some extra work involved in additions. We are recursively calling multiplication and performing some additions in every recursion. Let T (n) be the running time of multiplying two n-digit numbers. Then in our case, T (n) = 4T (n/2) +O (n) Four multiplications of n/2 digit numbers Addition is going to be between numbers that have atmost 2n digits. Thus addition can be O (n). Recursively substituting the value of T (n): T (n) = 4 [4T (n/4) + O (n/2)] +O (n) =16 T (n/4) + 4O (n/2) + O (n) - - - =C T (1) + - - - - Masters Theorem Let T(n) be the running time of an algorithm with an input size of n; Suppose we can run the algorithm in such a way that we make a recursive calls every time with an input size of n/b and do some extra work in every recursion (additions and subtractions). Such that T (n) can be represented as: T (n) = a T (n/b) + O (nk ), Then, If log ba>k, T (n) =O (nlog b a ) (recursive calls dominates) If log ba=k, T (n) =O (nk log n) (almost equal work in rec. calls and in extra work) If log ba<k, T (n) =O (nk ) (Extra work dominates) In our multiplication problem: T (n) = 4T (n/2) +O (n) A=4, b=2 Log24=2, k=1 Since algorithm is dominated by recursive calls and the running time is O (n2 ).
  • 3. But this is as good as our traditional multiplication algorithm. Since we now know that multiplications dominate the running time, if we can reduce the number of multiplications to three, which can bring down our T(n) by 25%. To calculate (1), we just need the following 3 multiplications separately: 1. (X+Y)(W+Z) 2 additions and one multiplication 2.XW 3.YZ Then we can calculate XZ+YW=(X+Y)(W+Z)-XW-YZ Thus we use three multiplications at the expense of some extra additions and subtractions, which run in constant time( each of O(n) time) Thus, T(n)=3T(n/2) + O(n) Applying Masters theorem, A=3,b=2,k=1 Thus, T(n)=O(nlog 2 3 ) Since log 2 3 ~ 1.5, We have reduced the total number of recursive calls in our program. For very large n, it will work well but in actual implementation, we hardly code to gain advantage out if this feature. Binary Search Goal: Searching for nth value in a sorted list of length n. (Divide the list into two and recursively search in the individual lists half the size) Again, Let T(n) be the running time of the algorithm. Then, T(n)=T(n/2) + O(1) In O(1) time we divide the list into two halves(n/2) that run in T(n/2) time. Using Masters theorem, A=1,b=2 Log21=0 K=0; So, T(n)=O(log n) Merge Sort Goal: Sp litting the element list into 2 lists, sort them and merge them. T(n)=2T(n/2) + O(n)
  • 4. Here, the hidden constant is greater than the hiddent constant in the merge sort because while dividing the lists into two different arrays and then sorting them, we are allocating extra space and subsequently, copying into the original array. Using Masters theorem, A=2,b=2,k=1 Log22=1 So, T(n)=O(n log n) Quicksort Goal: Pick one element as the partition element. Put all the elements smaller than it, to the left of it and all elements greater than it, to the right of it. On the lists left and right of the partition element recursively call Qsort. Say the list is: 8,3,6,9,2,4,7,5 Partition element:5 8, 3, 6, 9, 2, 4, 7, 5 8 in the worng place, 7 fine. 8, 3, 6, 9, 2, 4, 7, 5 4 ,3 ,6 ,9 , 2 ,8 ,7 ,5 4, ,3, 2, 9, 6, 8, 7, 5 4, 3 ,2 ,9 ,6 ,8 ,7 ,5 front Last front Last front last front last last front
  • 5. Now swap front with 5 and we have 5 in place. 4,3,2,5,6,8,7,9 Thus the only extra space utilized here is the temporary variable used for swapping. In te worst case, we might end up choosing a partition element which is the first element in our list. In that case T(n)=O(n2 ) To make sure this rarely happens: 1. Pick a random partition element. 2. Probablity of picking a good partition element is as low as the probability of picking a bad one. So, they will even out. There are n possible partition elements Element Split Prob(element) 1 0,n-1 1/n 2 1,n-2 1/n 3 1/n N n-1,0 1/n Now, T (n) = 1/n [ T(0) + T(n-1) + O(n) ] + 1/n [ T(1) + T(n-2) + O(n) ] + 1/n [ T(2) + T(n-3) + O(n) ] + 1/n [T (n-1) + T (0) + O (n)] n * T[n] = {2 k=0裡n-1 T(k)} + O(n2 ) A Substitute n = n-1, (n-1) * T[(n-1)] = {2 k=0裡n-1 T(k)} + O((n-1)2 )B Subtract A from B
  • 6. n T(n) - (n-1)T(n-1) = 2 T(n-1) + O(n) n T(n) = n+1 T(n-1) + O(n) T(n) = ((n+1)/n )T(n-1) + O(1) Divide by (n+1) T(n)/(n+1) = [T(n-1) ]/ n + O(1/n) C Let, S(n) = T(n)/(n+1) D S(n) = S(n-1) + O(1/n) This can be written as a sum, = S(n-2) + O(1/n-1) + O(1/n) = S(n-3) + O(1/n-2) + O(1/n-1) + O(1/n) S(n) = O(k=1 裡 n 1/k) = O( Hn) S(n) = T(n)/(n+1) = O (ln n) E Substitute D in C T(n) = S(n) . (n+1) use E, T(n) = O (ln n) . (n+1) T(n) = O (n lg n ) Notes compiled by Shankar Vaithianthan and Harjinder Mandarh
  • 7. n T(n) - (n-1)T(n-1) = 2 T(n-1) + O(n) n T(n) = n+1 T(n-1) + O(n) T(n) = ((n+1)/n )T(n-1) + O(1) Divide by (n+1) T(n)/(n+1) = [T(n-1) ]/ n + O(1/n) C Let, S(n) = T(n)/(n+1) D S(n) = S(n-1) + O(1/n) This can be written as a sum, = S(n-2) + O(1/n-1) + O(1/n) = S(n-3) + O(1/n-2) + O(1/n-1) + O(1/n) S(n) = O(k=1 裡 n 1/k) = O( Hn) S(n) = T(n)/(n+1) = O (ln n) E Substitute D in C T(n) = S(n) . (n+1) use E, T(n) = O (ln n) . (n+1) T(n) = O (n lg n ) Notes compiled by Shankar Vaithianthan and Harjinder Mandarh