📘 CodingMarble Learn

Sorting Algorithms

A sorting algorithm puts a list in order. Bubble sort swaps neighbours, insertion sort slides each item into a sorted part, selection sort picks the smallest each time, and merge sort splits the list and merges sorted halves. Merge sort needs far fewer comparisons on long lists (about n log₂ n instead of about n²/2).

🎬 Step-by-step story

  1. Here is a list of 8 numbers. To sort means to put them in order, from smallest to biggest.
  2. Bubble sort: compare two neighbours. If they are in the wrong order, swap them. After one pass, the biggest number is at the end (green).
  3. Insertion sort: the left part is already sorted. Take the next number and slide it left until it fits, like sorting playing cards in your hand.
  4. Selection sort: look through the rest to find the smallest number. Put it in the next place at the front. Repeat.
  5. Merge sort: split the list into halves, again and again, until each piece has one number. Then merge the pieces back in order (blue = being merged).
  6. Your turn: pick a method, shuffle the list and play it. Watch the counters for comparisons and swaps.

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

🤔 Common doubts, cleared

Why does the biggest number end up at the end after one bubble pass?

Once the biggest number is reached, it wins every comparison, so it keeps being swapped right until it reaches the end. Watch step 2.

Why is insertion sort called “insertion”?

Each new item is inserted into its correct spot in the already-sorted left part, like slipping a card into your hand. See step 3.

Does selection sort swap many times?

No, at most once per place. It compares a lot, but swaps little. Count the swaps in step 4.

Why do we split down to single items in merge sort?

A list of one item is always sorted, so we can start merging sorted lists. See step 5.

Why is merge sort faster if it seems to do more steps?

Each merge level does about n comparisons and there are only about log₂ n levels. Compare the counters for bubble and merge in free play.

Do all methods give the same final list?

Yes. They only differ in how much work they do. Try each one on the same shuffled list.

What does sorting mean?

Sorting means arranging items in order. The order can be ascending (small to big, A to Z) or descending (big to small).

Why sort? A sorted list is quicker to search (you can use binary search), easier to read, and makes it easy to find the smallest, biggest or middle value.

An algorithm is a list of exact steps. Different sorting algorithms give the same answer but take different amounts of work.

Bubble sort

Go through the list from left to right. Compare each pair of neighbours. If the left one is bigger, swap them. This is one pass. After the first pass the biggest item has “bubbled” to the end.

Repeat passes. Each pass can stop one place earlier. If a pass makes no swaps, the list is sorted and you can stop early.

repeat
  swapped ← false
  for i ← 0 to n − 2
    if A[i] > A[i+1] then
      swap A[i], A[i+1]
      swapped ← true
until swapped = false

Worst case: about n²/2 comparisons. Easy to write, slow for big lists.

Insertion sort

Think of the first item as a sorted list of one. Take the next item and move it left past every bigger item, then drop it in. Now the sorted part is one longer. Repeat until the end.

It is fast when the list is almost sorted (few moves), and it works well for small lists. Worst case (list in reverse order): about n²/2 comparisons.

Selection sort and counting sort

Selection sort: find the smallest item in the whole list and swap it into place 1. Find the smallest of the rest and swap it into place 2. Keep going. It always makes about n²/2 comparisons, but at most n − 1 swaps.

Counting sort: useful when values are small whole numbers, like marks out of 10. Make a frequency list: how many 0s, how many 1s, … how many 10s. Then write each value out as many times as it was counted. No comparisons at all!

Merge sort: divide and conquer

Divide: split the list into two halves. Split each half again, until every piece has one item (a list of one is already sorted).

Merge: join two sorted lists by looking at the front item of each and taking the smaller one, again and again.

Example: merge [2, 5] and [1, 8]: take 1, then 2, then 5, then 8 → [1, 2, 5, 8].

A list of n items is halved about log₂ n times, and each level of merging does about n comparisons. So merge sort takes about n log₂ n comparisons. It needs extra memory for the merged lists.

Comparing the algorithms

AlgorithmComparisons (big lists)Extra memoryGood for
Bubbleabout n²/2noneteaching, tiny lists
Insertionabout n²/2 (few if nearly sorted)nonesmall or nearly sorted lists
Selectionabout n²/2nonefew swaps needed
Mergeabout n log₂ nyesbig lists

For 1,000 items: n²/2 = 500,000 comparisons, but n log₂ n ≈ 10,000. That is why merge sort wins on large data.

Try it: sort cards by hand

Take 8 playing cards (or paper slips with numbers). Sort them three times: with bubble sort, insertion sort and merge sort. Keep a tally of each comparison. Which method used the fewest? Now check your tally with the counters in the 3D free-play step.

Key formulas and definitions

Worked examples

1. Show the first pass of bubble sort on [5, 2, 8, 1, 6].

5>2 swap → [2,5,8,1,6]; 5<8 no swap; 8>1 swap → [2,5,1,8,6]; 8>6 swap → [2,5,1,6,8]. After pass 1: [2, 5, 1, 6, 8]; 8 is in its final place.

2. Use insertion sort on [4, 1, 3, 2]. Show the list after each insertion.

Insert 1 → [1, 4, 3, 2]. Insert 3 → [1, 3, 4, 2]. Insert 2 → [1, 2, 3, 4].

3. Use selection sort on [7, 3, 9, 1]. How many swaps are made?

Smallest is 1 → swap with 7: [1, 3, 9, 7]. Smallest of rest is 3, already in place. Smallest of [9, 7] is 7 → swap: [1, 3, 7, 9]. 2 swaps.

4. Show how merge sort sorts [6, 2, 7, 3].

Split: [6, 2] and [7, 3] → [6] [2] [7] [3]. Merge: [2, 6] and [3, 7]. Merge those: take 2, 3, 6, 7 → [2, 3, 6, 7].

5. Sort the marks [3, 1, 3, 0, 2, 1, 3] (out of 3) with counting sort.

Counts: 0→1, 1→2, 2→1, 3→3. Write out: [0, 1, 1, 2, 3, 3, 3].

6. Bubble sort needs how many comparisons in the worst case for 10 items (without early stop)? How many does merge sort need, roughly?

Bubble: 9 + 8 + … + 1 = 45. Merge: about 10 × log₂10 ≈ 10 × 3.3 ≈ 33 (in fact at most 25 for 10 items).

Common mistakes

Practice quiz

1. After the first pass of bubble sort (ascending), which item is surely in place?
2. Which algorithm splits the list into halves?
3. Which sort works like arranging playing cards in your hand?
4. For a very large list, which is usually fastest?
5. Merge [1, 4] and [2, 3]. The result is:

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

Which sorting algorithm is the best?

There is no single best. Merge sort (and quicksort) are fast on big lists. Insertion sort is great for small or nearly sorted lists. Bubble sort is mainly used for learning.

What is the difference between bubble sort and merge sort?

Bubble sort swaps neighbours in repeated passes and takes about n²/2 comparisons. Merge sort splits the list and merges sorted halves, taking about n log₂ n comparisons but needing extra memory.

Why do we need to sort data?

Sorted data is faster to search (binary search), easier to read, and lets you find the smallest, largest and median values quickly.

Where this is taught

PolandLiceum ogólnokształcące, klasa IUnderstanding, analysing and solving problems
RomaniaClasa a IX-aProblem-solving strategies
RomaniaClasa a IX-aProblem-solving strategies
RomaniaClasa a IX-aProblem-solving strategies
Ukraine11 класAlgorithms
England (GCSE, A level)Year 103.1 Fundamentals of algorithms
USA (Common Core, NGSS, AP)Grade 11Data Collections
South Korea고등학교 2학년Algorithms and programming
Russia9 классAlgorithms and programming
Russia11 классAlgorithms and programming

Learn first

Related lessons

All Computer Science lessons