Intro to
Algorithms:
Sorting
[CREDITS HERE]
[preface stuff]
- Agenda?
- Class format?
- FAQs?
Evaluating Different Sorting Algorithms
Evaluating Sorting Algorithms:
Complexity
Time Complexity
Space Complexity
How much memory is used?
How many comparisons are made?
How many swaps are made?
...with respect to input size
...and assuming worst case scenarios
Evaluating Sorting Algorithms:
Stability
A sorting algorithm is "stable"
if it preserves the order of
equal items.
Evaluating Sorting Algorithms:
Stability (example)
Prompt: I want bikes sorted by price (ascending). Given equal prices, I want lighter option to be first.
The list is already sorted by weight (ascending). I just need to sort it by price. But an unstable sort based on price could "unsort" weights.
Bike A | $600 | 20 lbs. |
Bike B | $500 | 30 lbs. |
Bike C | $500 | 35 lbs. |
Evaluating Sorting Algorithms:
Adaptability
A sorting algorithm is "adaptive"
if it becomes more efficient
(i.e., if its complexity is reduced)
when the input is
already nearly sorted.
Insertion Sort
Visualization here:
Exercise Time!
Space
Comparisons
Swaps
Insertion Sort
Complexity
Characteristics
Stable:
Adaptive:
Insertion Sort
Use Cases
Insertion Sort
Text
Selection Sort
Visualization here:
Exercise Time!
Space
Comparisons
Swaps
Selection Sort
Complexity
Characteristics
Stable:
Adaptive:
Selection Sort
Use Cases
Selection Sort
Text
Bubble Sort
Visualization here:
Exercise Time!
Space
Comparisons
Swaps
Bubble Sort
Complexity
Characteristics
Stable:
Adaptive:
Bubble Sort
Use Cases
Bubble Sort
Text
Merge Sort
Visualization here:
Exercise Time!
Space
Comparisons
Swaps
Merge Sort
Complexity
arrays
linked lists
Characteristics
Stable:
Adaptive:
Merge Sort
Use Cases
Merge Sort
Text
Quick Sort
Visualization here:
Exercise Time!
Space
Comparisons
Swaps*
Quick Sort
Complexity
*Note: Complexity of swapping is implementation-specific
(there are multiple solutions)
Characteristics
Stable:
Adaptive:
Quick Sort
Use Cases
Quick Sort
Text
Recap
- Complexity
- Stability
- Adaptability
- Divide & Conquer
Recap
Concepts
-
Khan Academy
algorithms lessons -
Sorting-Algorithms.com
reference animations & info -
Comparison of Sorting
animations w/controls
Recap
Resources & References
deck
By rebootjeff
deck
- 971