CS 411 Fall 2025 > Outline for October 15, 2025
CS 411 Fall 2025
Outline
for October 15, 2025
Outline
Heaps & Heap Sort [L 6.4]
- Priority Queues
  - Associative structure with limited operations.
- Key is called priority.
- Create (insert) as in dictionary; other operations only performed on highest-priority item in container.
 
- Heaps
  - (Binary) Heap: common data structure for implementing a Priority Queue.
- Heap = Binary Tree with Complete & Parental-Dominance properties.
- Array representation: always used for a Binary Heap.
 
- Heap Operations
  - Heap Insert: Add new item as new final leaf. Trickle final leaf up. \(\Theta(\log n)\) time.
- Heap Delete: Swap root & final leaf. Remove final leaf. Trickle root down. \(\Theta(\log n)\) time.
- Fast In-place Heap Make
    - Could do \(n\) Heap Insert operations. \(\Theta(n\log n)\) time.
- Better: Start from end and move toward beginning. Trickle each item down. Result: \(\Theta(n)\) time. [Wow!]
 
 
- Heap Sort
  - Note that Heap Delete puts the biggest item at the end. This is what each pass does in Selection Sort, but there it takes us linear time. Once a Heap is created, we can do this in logarithmic time.
- Procedure:
    - Do in-place Heap Make.
- Do Heap Delete operations until Heap is empty.
 
- Analysis
    - Time: \(\Theta(n\log n)\).
- Additional space required: \(\Theta(1)\). In-place!
- Requirements on data: Must be random-access.
- Stable: No.
- Performance on nearly sorted data: \(\Theta(n\log n)\).
 
- Introsort does Quicksort, keeping track of “recursion” depth [eliminated tail calls count, too!]. If this exceeds some bound [\(2\log_2 n\)?], then switch to Heap Sort.
 
Problem Reduction [L 6.6]
- Idea: Solve other problem to obtain solution to original problem
- Example: Reduction to graph problems
- Example: LCM reduced to GCD
  - Euclid’s Algorithm (a.k.a. Euclidean Algorithm)
- Analysis
- LCM via Euclid’s Algorithm