DSA I Week 3 Lecture 1
DSA I Week 3 Lecture 1
Sherajul Arifin
Lecturer, Department of Computer Science and Engineering,
United International University
Time Complexity Analysis
Let T(n) denote its worst-case running time on input instances of size
n. Thus the running time,T(n) satisfies the following recurrence
relation.
T(2) ≤ c.
2
Solving Recurrence Relations
3
Solving Recurrence Relations
Method-1: Unrolling the Recursive Tree
Step -1: Analyze the first few steps:
o At the first level of recursion, we have a single problem of
size n, which takes time at most cn plus the time spent in
all subsequent recursive calls.
o At the next level, we have two problems each of size n/2.
Each of these takes time at most cn/2, for a total of at
most cn, again plus the time in subsequent recursive calls.
o At the third level, we have four problems each of size n/4,
each taking time at most cn/4, for a total of at most cn.
4
Solving Recurrence Relations
5
Solving Recurrence Relations
Step -3: Summing over all levels of recursion:
We know, the recursion will ‘bottom out’ when the value of input n
reaches n = 1. We assume the recursion will level down x times. Each
time the input size is halved, after x times it will be n/2x
→x = log2 n = log2 n * cn
a = 2, b = 2, k = 1, so, a = bk
= O(nlog2 2 log2 n)
= O(n1 log n)
= O(n log n)
8
Divide & Conquer (revisited)
9
Divide-and-Conquer Technique
• Divide-and-Conquer is a general algorithm design
paradigm:
o Divide the problem into a number of subproblems that
are smaller
o instances of the same problem
o Conquer the subproblems by solving them recursively
o Combine the solutions to the subproblems into the
solution for the original problem
• The base case for the recursion are subproblems of
constant size
• Analysis can be done using recurrence equations
10
Quick Sort
11
Divide-and-Conquer
12
Merge Sort and Quick Sort
•Two well-known sorting algorithms adopt this divide-and-conquer
strategy
•Merge sort
o Divide step is trivial – just split the list into two equal parts.
o Work is carried out in the conquer step by merging two sorted
lists.
•Quick sort
o Work is carried out in the divide step using a pivot element.
o Conquer step is trivial.
13
Quick Sort
Another divide-and-conquer algorithm
■The array A[p..r] is partitioned into two non-empty subarrays A[p..q] and
A[q+1..r]
p q q+1 r
8 15 4 30 25 7 18 12 8 4 7 12 15 30 25 18
Invariant: All elements in A[p..q] are less than all elements in A[q+1..r]
■The subarrays are recursively sorted by calls to quicksort
■Unlike merge sort, no combining step: two subarrays form an already-sorted
array
14
Quick Sort
15
16
Quicksort -Simulation
17
Quicksort -Simulation
18
Quicksort -Simulation
19
Quicksort -Simulation
20
Quicksort -Simulation
21
Quicksort -Simulation
22
Quicksort -Simulation
23
Quicksort -Simulation
24
Quicksort -Simulation
25
Quicksort -Simulation
26
Quick Sort (Partition)
Clearly, all the actions take place in the partition()function
■Rearranges the subarrays in place
■End result:
Two subarrays
All values in first subarray | pivot | all values in the second
subarray
■Returns the index of the “pivot” element separating the two
subarrays
27
Quick Sort (Analysis)
What will be the worst case for the algorithm?
■Partition is always unbalanced
What will be the best case for the algorithm?
■Partition is perfectly balanced
28
Quick Sort: Worst case running time
The recurrence for the worst-case running time T(n) is [Partition is
always unbalanced]
29
Quick Sort: Best case running time
The recurrence for the best-case running time T(n) is [Partition is
always balanced]
30
Quick Sort: Running Time
31
Quick Sort: Analysis
The real liability of quicksort is that it runs in O(n2) on already-
sorted input Two solutions:
■Randomize the input array, OR
■Pick a random pivot element
32
Thank you!