Chapter 1. Data Structures and Complexity
Chapter 1. Data Structures and Complexity
Recursive Algorithms
Complexity Analysis 2
Complexity Analysis with Recursions
Given the input size , let be the total number of basic
operations executed in BinarySearch
Algorithm 1: BinarySearch(arr, searchnum, left, right)
1 if left = right We can still count the number of
2 if arr[left]= searchnum basic operations for this part
3 return left
4 else The total number of basic
5 return -1 operations executed is a constant
6 middle (left + right)/2 intendent of the input size n, we
7 if arr[middle] = searchnum can use to denote this
8 return middle
9 elseif arr[middle] < searchnum
10 return BinarySearch(arr, searchnum, middle+1, right)
11 else
12 return BinarySearch(arr, searchnum, left, middle -1)
We either run line 10 or 12, but not both. What is the number of basic
operations that are executed by Line 10 or 12?
Complexity Analysis 3
Analysis for Recursive Binary Search (i)
excluded
𝑛 𝑛/2
𝑔 ( 𝑛 ) =𝑎 +𝑔 ( )
𝑛
2
Complexity Analysis 4
Analysis for Recursive Binary Search (ii)
Given ,
What is by using and to represent?
of them
Complexity Analysis 5
Analysis for Recursive Binary Search (iii)
Complexity Analysis 6
Sorting with Recursion
Input: a set S of n integers
Problem: store S in an array such that the elements
are arranged in ascending order
4 2 3 6 9 5 2 3 4 5 6 9
Complexity Analysis 7
Selection Sort
Step 1: Scan all the n elements in the array to find
the position of the largest element
,
4 2 3 6 9 5
4 2 3 6 5 9
4 2 3 6 5 9
sorted
Complexity Analysis 8
Selection Sort
Algorithm 2: selectionSort(arr, n)
1 if n 1 maxElement =9
2 return arr maxIndex = 4
3 maxElement arr[0] maxElement =4 maxElement =6
4 maxIndex 0 maxIndex = 0 maxIndex = i
5 for i 1 to n -1
6 if maxElement >
7 arr[i] 4 2 3 6 9 5
8 maxElement =
9 arr[i]
i=1 i=2 i=3 i=4
10 maxIndex = I
11 arr[maxIndex] = arr[n-1]
arr[n-1] = maxElement
selectionSort(arr, n-1) 4 2 3 6 5 sorted
9
Complexity Analysis 9
Selection Sort: Complexity Analysis
Algorithm 2: selectionSort(arr, n) # of basic operations
1 if n 1 𝑂 (1)
2 return arr 𝑂 (1)
3 maxElement arr[0] 𝑂 (1)
4 maxIndex 0 𝑂 (1)
5 for i 1 to n -1 𝑂 (𝑛)
6 if maxElement > 𝑂 (𝑛)
7 arr[i] 𝑂 (𝑛)
8 maxElement = 𝑂 (𝑛)
9 arr[i] 𝑂 (1)
10 maxIndex = i 𝑂 (1)
11 arr[maxIndex] = arr[n-1] 𝑂 (?)
arr[n-1] = maxElement
What is the total #n-1)
selectionSort(arr, of basic operations from Lines 1-10?
Depends linearly to n, we use to denote the cost. Both and are
constant, we do not care their specific values
Complexity Analysis 10
Analysis for Selection Sort (i)
Let be the total number of basic
operations in the worst case
Complexity Analysis 11
Analysis for Selection Sort (ii)
Given and
What is by using and to represent?
Complexity Analysis 13
Summary: Complexity Analysis with Recursion
Step 3: calculate
Complexity Analysis 14