



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
This document from wellesley college's cs231 algorithms course explores various issues in analyzing algorithms, including choosing a barometer for measuring time, model of computation, measuring input size, and worst-case, best-case, and average-case analysis. It also provides examples of insertion sort and its analysis.
Typology: Quizzes
1 / 7
This page cannot be seen from the preview
Don't miss anything!
Wellesley College ◊ CS231 Algorithms ◊ September 4, 1996 Handout #
ISSUES IN ANALYZING ALGORITHMS
Many Ways to Skin a Cat**
SQ1(x) return x * x
SQ2( x ) ans ← 0 {Add x to ans x times} for i ← 1 to x do do ans ← ans + x return ans
SQ3(x) ans ← 0 {Add 1 to ans x^2 times} for i ← 1 to x do for j ← 1 to x do ans ← ans + 1 return ans **---------------------------------------------------------------------------------------------
Choosing a Barometer**
What should we count to measure time?
Input SQ1 SQ2 SQ 1 2 3
n
Details:
Model of Computation
Running times depend on model of computation!
Sequential vs. Parallel (see Takis's Parallel Algorithms Course)
Typically assume that numerical operations take constant time.
Measuring Input Size**
Standard assumptions:
Not always obvious:
Can have more than one size:
Running Time for a Particular Input Size**
Running time may differ greatly for different inputs of the same size. How to characterize?
- Worst-case analysis: consider maximum time for every input size. - Best-case analysis: consider minimum time for every input size (not a good idea!). - Average-case analysis : expected running time based on probability
Example: Insertion Sort
Example of good ol' divide-and-conquer :
InsertionSort(A, k) Sort A[1..k] via insertion sort method Initially call Merge-Sort(A,1,length[A]) if n > 0 A[1..0] = empty array is trivially sorted; do nothing then InsertionSort(A, k - 1) Insert(A, k)
Insert(A, i) Assume A[1..i-1] is sorted. Make A[1..i] sorted if i > 1 A[1..1] is trivially sorted; do nothing then if LessThan(A, i - 1, i) Is A[i-1] < A[i]? then Swap(A, i - 1, i) Swap contents of A[i-1] and A[i] Insert(A, i - 1)
Analysis of Insert**
Worst-Case i #Calls to Insert #LessThans #Swaps 1 2 3
n
Best-Case i #Calls to Insert #LessThans #Swaps 1 2 3
n
Average-Case i #Calls to Insert #LessThans #Swaps 1 2 3
n
Analysis of InsertionSort**
Worst-Case k #Calls to Insertion-Sort
#Calls to Insert
#LessThans #Swaps
1 2 3
n
Best-Case k #Calls to Insertion-Sort
#Calls to Insert
#LessThans #Swaps
1 2 3
n
AverageCase k #Calls to Insertion-Sort
#Calls to Insert
#LessThans #Swaps
1 2 3
n
Another Version of Insertion Sort
CLR version of insertion sort:
Insertion-Sort(A) for j ← 2 to length[A] do key ← A[j] Insert A[j] into the sorted sequence A[1..j-1]. i ← j- while i > 0 and A[i] > key do A[i + 1] ← A[i]
Merge Sort
Idea : Divide array into two equal-sized subproblems, recursively sort, then merge results.
Merge-Sort(A, p, r) Sort A[p..r] by insertion sort method. Initially call Merge-Sort(A,1,length[A]). if p < r then q ← (p + r) / 2 Merge-Sort(A, p, q) Merge-Sort(A, q + 1, r) Merge(A, p, q, r)
Merge left as an exercise. **---------------------------------------------------------------------------------------------
The Next Three Lectures**
Asymptotic Notation: coarse-grained comparisons of algorithms.
Recurrences: coarse-grained analysis of algorithms.
Probability: tools for average-case analysis.
--------------------------------------------------------------------------------------------- ------------------------
Pop Quiz
What's is the main topic of this course?