Lecture 15: Asymptotics II

9/30/2020

Loops

Loops Example 1: Based on Exact Count

int N = A.length; for (int i = 0; i < N; i += 1) for (int j = i + 1; j < N; j += 1) if (A[i] == A[j]) return true; return false;

Loops Example 2

int N = A.length; for (int i = 1; i < N; i = i * 2) for (int j = 0; j < i; j += 1) System.out.println("hello"); int ZUG = 1 + 1; return false;

No Magic Shortcut

Repeat After Me...

Recursion

Recursion (Intuitive)

public static int f3(int n) { if (n <= 1) return 1; return f3(n-1) + f3(n-1); }

Recursion and Exact Counting

Recursion and Recurrence Relations

Binary Search Intuitive

Binary Search Exact Count

Binary Search (using Recurrence Relations)

Log Time is Really Terribly Fast

Merge Sort

Selection Sort: A Prelude to Mergesort

The Merge Operation

Using Merge to Speed Up the Sorting Process

Two Merge Layers

Example 5: Mergesort

Mergesort Order of Growth

Linear vs. Linearithmic (N log N) vs Quadratic

Summary