CHAPTER 9: SORTING ALGORITHMS
9.1 BUBBLE SORT
9.1.1 How It Works
- Compare adjacent elements
- Swap if in wrong order
- Repeat for entire list
- Each pass places largest unsorted element at end
- Continue until no swaps needed
9.1.2 Pseudocode
<PSEUDOCODE>
9.1.3 Python Code
<PYTHON>
9.1.4 Complexity
- Best Case: O(n) - already sorted
- Worst Case: O(n²) - reverse sorted
- Space: O(1)
9.2 INSERTION SORT
9.2.1 How It Works
- Assume first element is sorted
- Take next element
- Insert into correct position in sorted portion
- Repeat until all elements sorted
9.2.2 Pseudocode
<PSEUDOCODE>
9.2.3 Python Code
<PYTHON>
9.2.4 Complexity
- Best Case: O(n) - already sorted
- Worst Case: O(n²) - reverse sorted
- Space: O(1)