Searching and Sorting

Computer Science & Algorithms · Advanced Placement

Searching

{{illustration:cs-search}}

- Linear search works on any list. - Binary search needs sorted data and halves the range.

Quadratic sorts

{{illustration:cs-cmp}}

| Algorithm | Worst case | n = 64 | |---|---|---| | insertion sort | n(n − 1)/2 | 2016 | | selection sort | n(n − 1)/2 | 2016 | | merge sort | n log₂ n | 384 | | binary search | log₂ n | 7 |

Divide and conquer

{{illustration:cs-msort}}

- T(n) = 2T(n/2) + n gives n log n. - Merge sort is stable and uses extra memory.

A lower bound

{{illustration:cs-bound}}

- Any comparison sort needs ⌈log₂ n!⌉ comparisons in the worst case. - This is Ω(n log n).