CSC 282 · Database Programming
Our notes
1 min readLinear search
Look at each record in turn until found. Works on any file. Average n/2 comparisons, worst n.
Binary search
Needs records sorted on the key. Compare with the middle record; discard the half that cannot contain the key; repeat.
At most about log₂ n comparisons: for 1,000,000 records, about 20.
Worked example
Sorted keys: 12, 19, 25, 31, 44, 52, 60, 73, 88 (positions 1 to 9). Find 60.
- Low 1, high 9, middle 5 → 44 < 60, so low = 6.
- Low 6, high 9, middle 7 → 60. Found in 2 comparisons. (A linear search needs 7.)
In code (positions from 0):
def binary_search(keys, target):
low, high = 0, len(keys) - 1
while low <= high:
mid = (low + high) // 2
if keys[mid] == target:
return mid
if keys[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
Indexes
An index is a separate sorted list of key values with the location of each record, like the index of a textbook. Searching the index is fast, then one read fetches the record. Databases build indexes on primary keys automatically; you can add them on fields often searched (Surname, Department). Indexes speed reading but slow inserts and updates slightly, and take space.
Sorting
Arranging records by one or more keys: by Department, then Surname within department. Common in-memory methods are insertion sort, merge sort and quicksort; very large files are sorted in pieces and merged (external sort). In SQL you simply write ORDER BY.
Retrieval and reports
A report pulls the records you need (selection), the fields you need (projection), in the order you need, with totals.
Control break report: records sorted by department; when the department changes, print the sub-total and start a new one; at the end print the grand total.
| Department | Students | Fees owed (₦) |
|---|---|---|
| Computer Science | 3 | 120,000 |
| Physics | 2 | 45,000 |
| Total | 5 | 165,000 |
Exams usually ask
- Trace a binary search and count comparisons.
- Explain an index and when to add one.
- Design a control-break report.
Common mistake
Running a binary search on unsorted data. It can miss a record that is there; sort first or use a linear search.
Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.
Read
-
Binary search
(opens en.wikipedia.org in a new tab)
Wikipedia, Binary search, CC BY-SA 4.0 · licence
-
Sorting algorithm
(opens en.wikipedia.org in a new tab)
Wikipedia, Sorting algorithm, CC BY-SA 4.0 · licence
-
Linear search
(opens en.wikipedia.org in a new tab)
Wikipedia, Linear search, CC BY-SA 4.0 · licence
-
NOUN CIT 304: Data Management
NOUN course book (PDF, about 6.9 MB: best on Wi-Fi). Files, databases, the relational model and SQL.
(opens nou.edu.ng in a new tab)
-
NOUN CIT 213: Elementary Data Processing
NOUN course book (PDF, about 0.9 MB). Fields, records, files and processing data.
(opens nou.edu.ng in a new tab)
Watch
-
Best pick Searching And Sorting Algorithms In Python | Searching Algorithms | Sorting Algorithms | Intellipaat
17 min · ≈ 77 MB · about ₦43
(opens youtube.com in a new tab)
-
10 Sorting Algorithms Easily Explained
11 min · ≈ 50 MB · about ₦28
(opens youtube.com in a new tab)
-
All the Sorting Algorithms
12 min · ≈ 54 MB · about ₦30
(opens youtube.com in a new tab)
-
5 Sorting Algorithms Every Programmer Should Know
20 min · ≈ 90 MB · about ₦51
(opens youtube.com in a new tab)
-
12. Searching and Sorting
49 min · ≈ 221 MB · about ₦124
(opens youtube.com in a new tab)
We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.