Courses / Mathematics I
Programming Elements

Searching and Sorting

Mathematics I 324 words Free to read

Finding and Ordering Data

Searching (finding an item) and sorting (putting items in order) are the most fundamental algorithmic tasks, and they beautifully illustrate the complexity ideas of the previous lesson.

Searching:

This reveals a classic trade-off: sorting a list first (once) lets you binary-search it many times cheaply — worthwhile when you search repeatedly.

Sorting arranges elements in order. Simple methods like bubble sort, selection sort, and insertion sort are intuitive but run in O(n2)O(n^2) — fine for small data, slow for large. Efficient methods — merge sort and quicksort — run in O(nlogn)O(n \log n), the best achievable for comparison-based sorting, using divide and conquer: split the data, sort the pieces (recursively), and combine. The gap between O(n2)O(n^2) and O(nlogn)O(n \log n) is the difference between a slow and a usable sort at scale.

Sorting is a workhorse subroutine: once data is sorted, searching, finding duplicates, computing medians, and merging datasets all become easier. Choosing between algorithms is a matter of the data size, whether it is already partly ordered, and how the operation will be repeated — the everyday application of complexity analysis.

Common pitfall: applying binary search to an unsorted list. Binary search's speed comes entirely from the data being sorted — it discards half the list assuming order, so on unsorted data it will skip past the target and report it missing (or find the wrong thing). If the list is not sorted, either sort it first (then binary-search) or use linear search, which needs no ordering.

Practise this lesson

The explanation above is free to read. The graded practice for this lesson lives in the Tryals app.

13practice questions
2interactive scenes
Start Mathematics I free

Programming Elements