📘 CodingMarble Learn

Searching and Sorting Algorithms

A searching algorithm finds an item in a list; a sorting algorithm puts a list in order. Linear search checks items one by one and works on any list. Binary search halves a sorted list each time and is much faster. Bubble sort swaps neighbours pass by pass; merge sort splits the list and merges sorted halves, which is faster for big lists.

🎬 Step-by-step story

  1. Here are 12 boxes with numbers. We must find the box with 23. How many looks will it take?
  2. Linear search: check box 1, then box 2, and so on until we find 23. Here it took 7 checks.
  3. Binary search needs a sorted list. Check the middle, then throw away the half that cannot hold 23.
  4. Bubble sort: compare two neighbours and swap if they are in the wrong order. Repeat until no swaps.
  5. Merge sort: split the list into halves, sort the small pieces, then merge them back in order.
  6. Free play: choose a number and a method. Count how many comparisons each one needs.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

🤔 Common doubts, cleared

Why not just look at all items every time?

That works (it is linear search), but on a list of a million items it can take a million checks. Smarter methods need far fewer.

When is linear search the right choice?

When the list is small or not sorted. It needs no preparation, as the 3D shows going box by box.

Why must the list be sorted for binary search?

We throw away a half because we know every item there is too small or too big. That is only true when the list is in order.

How do I know bubble sort is finished?

When a whole pass makes no swaps, every neighbour pair is in order, so the list is sorted.

Why is merge sort faster if it does more steps of splitting?

Splitting is cheap. Merging two sorted lists takes only one comparison per item, and there are only about log₂n levels of merging.

What happens if the number is not in the list?

Linear search reaches the end; binary search ends with no items left. Try finding 50 in free play.

What are searching and sorting algorithms?

An algorithm is a list of clear steps to solve a problem. Two jobs come up again and again in computing:

We compare algorithms by counting comparisons (how many times we check two values). Fewer comparisons means a faster algorithm, especially for big lists.

Linear search

Start at the first item. Compare it with the target. If it matches, stop. If not, move to the next. If you reach the end, the item is not there.

for i from 0 to length − 1
    if list[i] = target then return i
return "not found"

Binary search

Binary search only works on a sorted list.

  1. Look at the middle item.
  2. If it is the target, stop.
  3. If the target is smaller, keep only the left half; if bigger, keep only the right half.
  4. Repeat on the half that is left until found, or nothing is left.
low ← 0, high ← length − 1
while low ≤ high
    mid ← (low + high) DIV 2
    if list[mid] = target then return mid
    else if list[mid] < target then low ← mid + 1
    else high ← mid − 1
return "not found"

Each check halves the list. 1,000 items need at most about 10 checks; 1,000,000 items about 20. Linear search could need 1,000,000.

Bubble sort and merge sort

Bubble sort

Go through the list comparing each pair of neighbours; swap them if they are in the wrong order. After one pass, the biggest item has reached the end. Repeat passes until a pass makes no swaps. Easy to code, uses little extra memory, but slow on big lists (about n² comparisons).

Merge sort

A divide and conquer method. Split the list in half again and again until each piece has one item (one item is already sorted). Then merge pairs of pieces: compare the front items of the two pieces and take the smaller one each time. Much faster on big lists (about n·log₂n comparisons) but needs extra memory for the merged lists.

Insertion sort (also common)

Take items one by one and slide each into its correct place in the sorted part, like arranging playing cards in your hand. Good for small or nearly sorted lists.

Choosing an algorithm

AlgorithmNeeds sorted list?Speed on big listsBest for
Linear searchNoSlow (n)Small or unsorted lists
Binary searchYesVery fast (log₂n)Big sorted lists
Bubble sort—Slow (n²)Tiny or nearly sorted lists, learning
Merge sort—Fast (n log n)Big lists

Tip: if you will search a list many times, sorting it once and then using binary search saves time overall.

Try it: card search race

Write the numbers 1–16 on cards. Shuffle them face down in a row and ask a friend to find 11 by turning one card at a time (linear search). Count the turns. Now lay them in order and find 11 by always turning the middle card (binary search). Which needed fewer turns? Then sort a shuffled set by bubble sort, counting your swaps.

Key formulas and definitions

Worked examples

1. Linear search for 56 in [42, 7, 19, 88, 3, 56]. How many comparisons?

42, 7, 19, 88, 3, 56 → found at the 6th check: 6 comparisons.

2. Binary search for 64 in [3, 7, 11, 19, 23, 35, 42, 56, 64, 71, 88, 90]. Show the middles.

low 0, high 11 → mid 5 = 35 < 64 → low 6. mid 8 = 64 → found. 2 comparisons.

3. One pass of bubble sort on [5, 1, 4, 2].

5>1 swap → [1,5,4,2]; 5>4 swap → [1,4,5,2]; 5>2 swap → [1,4,2,5]. The 5 is now at the end.

4. Merge [2, 9] and [4, 5].

2 vs 4 → take 2; 9 vs 4 → take 4; 9 vs 5 → take 5; then 9. Result [2, 4, 5, 9].

5. At most how many checks does binary search need for 64 items?

64 → 32 → 16 → 8 → 4 → 2 → 1: 6 halvings, so at most 7 checks.

6. Why can't binary search be used on [9, 2, 7, 4]?

The list is not sorted, so throwing away a half might throw away the target. Sort first or use linear search.

Common mistakes

Practice quiz

1. Which search needs a sorted list?
2. After the first pass of bubble sort (ascending), which item is surely in place?
3. Merge sort is an example of:
4. Worst-case checks for linear search on 100 items:
5. Binary search on 1,000 sorted items needs at most about:

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What is the difference between linear search and binary search?

Linear search checks items one by one and works on any list. Binary search checks the middle and halves a sorted list each time, so it is much faster on big lists.

Which sorting algorithm is the fastest?

Of those taught at school, merge sort is fastest for large lists (about n log n comparisons). Bubble and insertion sort are slower (about n²) but simple.

Is binary search always better than linear search?

No. It needs a sorted list. For a small or unsorted list that is searched only once, linear search is often the better choice.

Where this is taught

Canada (Ontario)Grade 12A. Programming Concepts and Skills
Canada (Ontario)Grade 12A. Programming Concepts and Skills
PolandSzkoła podstawowa, klasa VIIUnderstanding, analysing and solving problems
PolandSzkoła podstawowa, klasa VIIIUnderstanding, analysing and solving problems
RomaniaClasa a X-aFundamental algorithms on arrays
England (GCSE, A level)Year 9Computer science
England (GCSE, A level)Year 103.1 Fundamentals of algorithms
England (GCSE, A level)Year 134.3 Fundamentals of algorithms
USA (Common Core, NGSS, AP)Grade 11Data Collections
South Korea고등학교 2학년Algorithms and programming
China高二Sel.1 Data and data structures

Learn first

Learn next

Related lessons

All Computer Science lessons