Step through bubble, insertion and merge sort and linear and binary search, one comparison at a time.
Step through sorting and searching algorithms, with pass-by-pass tables.
This tool needs JavaScript.
Exam questions often ask you to show a list part-way through a sort, or the items a search checks. AQA GCSE names linear search, binary search, bubble sort and merge sort. OCR GCSE names those four and insertion sort. SQA Higher lists linear search among its standard algorithms.
Linear search checks items from the start until it finds the target or reaches the end. It works on any list. Binary search needs a sorted list. It compares the middle item with the target, then keeps only the half that could hold it. Here positions start at 0 and mid = (low + high) DIV 2, so a half is rounded down. A question may count from 1 or round another way, so follow its rule.
Bubble sort the list 5, 2, 4, 1 into ascending order.
Open this bubble sort
Now search the sorted list 3, 9, 10, 23, 27, 38, 43, 82 for 43.
Open this binary search
One trip through the unsorted part of the list from start to end, making every neighbouring comparison and any swaps. A single comparison or swap is not a pass.
No. It rules out half the list by assuming the items are in order, so on an unsorted list it can miss a value that is there. Sort the list first, or use linear search.
No. Merge sort grows much more slowly as lists get longer, but on a short or already sorted list a bubble sort that stops early can need less work, and it uses less memory.