Big-O, Sorting Algorithms, Data Structures, and Analysis Techniques

0.0(0)
studied byStudied by 1 person
full-widthCall with Kai
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/29

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

30 Terms

1
New cards

Big-O definition

A function f(n) is in O(g(n)) if ∃ c,n0>0 such that f(n) ≤ c·g(n) for all n ≥ n0.

2
New cards

Formal Big-O proof template

For a polynomial f(n)=a_k n^k+...+a_0, note n^i ≤ n^k for n≥1; choose c=Σa_i, n0=1 ⇒ f(n) ≤ c·n^k.

3
New cards

Example Big-O proof

For f(n)=2n^2+3n+3, when n≥3 we have f(n) ≤ 4n^2 ⇒ c=4, n0=3 ⇒ O(n^2).

4
New cards

Amortized analysis definition

Analyzes average cost per operation over any sequence of n operations (no probability assumptions).

5
New cards

Vector push_back doubling

Resizes at 1,2,4,...,n; copies <2n; plus n writes ⇒ total <3n ⇒ amortized O(1).

6
New cards

Vector push_back +100 growth

Resizes about n/100 times; copy cost Θ(n^2); divide by n ⇒ amortized Θ(n) per push.

7
New cards

Binary counter increment

Total bit flips ≤ 2n for n increments ⇒ amortized O(1).

8
New cards

Lower bound theorem

Any comparison-based sort needs at least ⌈log₂(n!)⌉ comparisons = Θ(n log n).

9
New cards

Sorts exempt from lower bound

Counting, Radix, Bucket (use key structure, not just comparisons).

10
New cards

Why radix requires stable sort

LSD radix sort preserves earlier digit order only if the inner sort (counting) is stable.

11
New cards

Insertion sort stability

Stable: equal elements keep their relative order.

12
New cards

Selection sort stability

Not stable: swapping can break relative order of equals.

13
New cards

Best sort for values {0,1,2}

Counting sort: O(n+k)=O(n) since k=3.

14
New cards

Least efficient sort for {0,1,2}

Selection sort: Θ(n^2) even if nearly sorted.

15
New cards

Stack vs Queue input reversal

Stack reverses input (LIFO); Queue models lines/shelf restocking (FIFO).

16
New cards

Stack operations runtimes

Push = amortized O(1); Pop, Top, is_empty = O(1).

17
New cards

Queue operations runtimes

Enqueue = amortized O(1); Dequeue = O(1) with ring buffer or linked list.

18
New cards

Binary search cost when n doubles

log₂(2n) = log₂ n + 1 ⇒ only +1 extra comparison.

19
New cards

Insertion sort best case

Already sorted array; runtime Θ(n).

20
New cards

Pointer storage

Pointer variable lives on the stack; pointee allocated with new lives on the heap.

21
New cards

Pointer operators

(p++) uses then increments; (--p) decrements then uses; (p)++ prints then increments element; ++(p) increments element then prints.

22
New cards

Shallow copy problem

Copies pointer value only ⇒ double delete, aliasing bugs, dangling pointer if one object frees.

23
New cards

Rule of Three/Five

If a class manages resources (e.g., raw pointers), define destructor, copy constructor, copy assignment (and move versions in C++11+).

24
New cards

Insertion sort runtimes

Best: O(n) (already sorted). Worst: O(n^2) (reverse sorted). Average: O(n^2).

25
New cards

Selection sort runtimes

Best: O(n^2). Worst: O(n^2). Average: O(n^2).

26
New cards

Bubble sort runtimes

Best: O(n) (if early exit check). Worst: O(n^2). Average: O(n^2).

27
New cards

Counting sort runtimes

Best/Worst/Average: O(n+k). Linear when k = O(n).

28
New cards

Radix sort runtimes

Best/Worst/Average: O(d·(n+k)). Linear when digit count d and base k are constants.

29
New cards

Bucket sort runtimes

Best: O(n+k) (uniform distribution). Worst: O(n^2) (all elements fall in one bucket). Average: O(n+k).

30
New cards

Binary search runtimes

Best: O(1) (target at middle). Worst: O(log n). Average: O(log n).