Linear search
Checks each item in turn, from the start, until it finds the one it wants or reaches the end. It works on any list, sorted or not, but it is slow on large lists because it may have to check every item.
Step through the searching and sorting algorithms you need to know. Watch the pointers move, read what happens at each step and see the line of pseudocode that does it.
Click an item or type one. Searches are case sensitive. Try something that isn't in the list too!
Pick a searching or sorting algorithm above. You'll get a fresh list of 12 items each time.
These are the searching and sorting algorithms in the OCR GCSE Computer Science (J277) specification. Choose one above to step through it, or use this summary to revise.
Checks each item in turn, from the start, until it finds the one it wants or reaches the end. It works on any list, sorted or not, but it is slow on large lists because it may have to check every item.
Only works on a sorted list. It checks the middle item, then throws away the half that can't contain the target, and repeats. It is much faster on large lists: about 20 checks are enough for a million items.
Compares each pair of neighbouring items and swaps them if they are in the wrong order. It repeats these passes until a pass makes no swaps. It is simple to write but slow for large lists.
Takes each item in turn and inserts it into the right place in the sorted part of the list to its left. It is efficient for small lists and lists that are nearly sorted.
Splits the list in half again and again until every list has one item, then merges the lists back together in order. It is much faster than bubble sort on large lists, but uses more memory.
| Algorithm | Needs sorted data? | Strength | Weakness |
|---|---|---|---|
| Linear search | No | Works on any list | Slow on large lists |
| Binary search | Yes | Very fast on large lists | List must be sorted first |
| Bubble sort | – | Simple to understand and code | Slow on large lists |
| Insertion sort | – | Good for small or nearly sorted lists | Slow on large unsorted lists |
| Merge sort | – | Fast on large lists | Uses more memory |
It decides which half to throw away by comparing the target with the middle item. That only works if every item to the left is smaller and every item to the right is larger.
Of these three, merge sort is usually fastest for large lists. Insertion sort can be quicker for small or nearly sorted lists.
It gives a practice question using the same list, so you can write out the steps yourself and then check them against the walkthrough.
Choose an algorithm to show an exam question.