How computers find and order data: Binary Search, Linear Search, and Bubble Sort — with step-by-step animated walkthroughs and a 10-question quiz.
The essentials for each algorithm, in your own words.
Binary search finds an item in a sorted list by repeatedly checking the middle value. If the target is smaller, search the left half; if larger, search the right half — and repeat until it's found or the list runs out.
Linear search checks each item in a list one by one, from the start, until it finds the target or reaches the end of the list. It's simple, but slower than binary search on large lists.
Bubble sort arranges items into order by repeatedly comparing adjacent pairs and swapping them if they're in the wrong order. Each full pass "bubbles" the largest remaining item to the end, until no swaps are needed.
Watch how the search space is halved each time as it looks for 23 in a sorted list.
SET low TO 0
SET high TO length of list - 1
WHILE low <= high DO
SET mid TO (low + high) / 2
IF list[mid] == target THEN
PRINT "Item found at position", mid
EXIT loop
ELSE IF list[mid] < target THEN
SET low TO mid + 1
ELSE
SET high TO mid - 1
END IF
END WHILE
IF low > high THEN
PRINT "Item not found"
END IF
Watch how each item is checked in turn while looking for 20 in an unsorted list.
SET found TO false
FOR each item IN list
IF item == target THEN
PRINT "Item found"
SET found TO true
EXIT loop
END IF
END FOR
IF found == false THEN
PRINT "Item not found"
END IF
Watch adjacent pairs get compared and swapped, pass by pass, until the list is sorted.
FOR i FROM 0 TO length of list - 1
FOR j FROM 0 TO length of list - i - 2
IF list[j] > list[j + 1] THEN
SWAP list[j] AND list[j + 1]
END IF
END FOR
END FOR
OCR-style multiple choice questions on binary search, linear search and bubble sort.