Proving Correctness: Using induction
Runtime Analysis: Asymptotic analysis
Specific Problems Discussed: Comparison-sorting problems
Pseudo-code to introduce bubble sort logic:
for i = 1 to length(A) {
for j = i+1 to length(A) {
if( A[i] > A[j] ) swap A[i] and A[j]
}
}
Euclidean algorithm for calculating GCD:
int GCD (int a, int b){
if b is equal to zero return a;
return GCD(b, a % b);
}
Purpose: Order sequences of values with applications in organizing data (libraries, databases).
Applications Include: Finding medians and data compression.
Concept: Insert elements one at a time into their correct position in a growing sorted list.
Implementation:
insertionSort( A : array of items ) {
int holePosition;
int valueToInsert;
for i = 1 to length(A) do {
valueToInsert = A[i];
holePosition = i;
while holePosition > 0 and A[holePosition-1] > valueToInsert do {
A[holePosition] = A[holePosition-1];
holePosition = holePosition - 1;
}
A[holePosition] = valueToInsert;
}
}
Initial Array: [4, 3, 1, 5, 2]
Move elements left until the right position is found.
Final Sorted Array: [1, 2, 3, 4, 5]
Loop Invariant: The subarray A[0] to A[i] is sorted.
Components of Proof:
Base Case: A[0] is trivially sorted.
Inductive Hypothesis: Assume true up to index i.
Inductive Step: Inserting A[i+1] into the sorted subarray shows it remains sorted.
The worst-case scenario occurs with a reverse-ordered list leading to O(n²) complexity.
For an already sorted list, the complexity is Ω(n).
Generally not a concern unless algorithms are randomized.
Big-O Notation: Represents upper bounds of function growth ignoring constants.
Example:
O(n + log(n)) = O(n)
Big-O provides an upper-bound expectation for algorithm performance.
Big-Ω gives a lower-bound metric similarly defined as T(n) is Ω(g(n)).
Correctness: Confirm correct implementation via induction.
Runtime: Analyze performance characteristics using asymptotic notation.