← Back to Games

📝 Sorting Algorithms

Put the steps in the correct order

🔄 What is Sorting?

Sorting is the process of arranging data into a specific order (e.g., ascending or descending). Sorting algorithms take an unordered list and rearrange the elements. Different algorithms have different approaches — some are simple but slow, others are more complex but faster.

Bubble Sort
Insertion Sort
Merge Sort

🫧 Bubble Sort

Bubble Sort repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. This continues until no more swaps are needed.

How It Works

  1. Start at the beginning of the list
  2. Compare the first two adjacent elements
  3. If the first is greater than the second, swap them
  4. Move to the next pair and repeat
  5. After each complete pass, the largest unsorted element "bubbles up" to its correct position
  6. Repeat passes until no swaps are needed (list is sorted)

Why "Bubble" Sort?

It's called "bubble" sort because larger elements gradually "bubble up" to the end of the list with each pass, like bubbles rising in water.

Example: Sorting [5, 3, 8, 1]

Pass 1: [5,3,8,1] → [3,5,8,1] → [3,5,8,1] → [3,5,1,8] ✓ (8 in place)
Pass 2: [3,5,1,8] → [3,5,1,8] → [3,1,5,8] ✓ (5 in place)
Pass 3: [3,1,5,8] → [1,3,5,8] ✓ (3 in place)
Result: [1, 3, 5, 8] - Sorted!

Advantages & Disadvantages

✅ Advantages: Simple to understand and implement, requires no extra memory, detects if already sorted.

❌ Disadvantages: Very slow for large lists, inefficient compared to other algorithms.

🎯 Correct Order

📦 Steps

📥 Insertion Sort

Insertion Sort builds the sorted list one element at a time. It takes each element and inserts it into its correct position among the already-sorted elements, like sorting playing cards in your hand.

How It Works

  1. Start with the second element (first element is already "sorted")
  2. Compare it with elements to its left
  3. Shift larger elements one position to the right
  4. Insert the current element in its correct position
  5. Move to the next element and repeat
  6. Continue until all elements are in place

The Playing Cards Analogy

Imagine sorting cards in your hand: you pick up each new card and slide it into the right place among the cards you've already sorted. That's exactly how Insertion Sort works!

Example: Sorting [5, 3, 8, 1]

Start: [5] is sorted | remaining: 3, 8, 1
Insert 3: 3 < 5, shift 5 right → [3, 5] | remaining: 8, 1
Insert 8: 8 > 5, stays in place → [3, 5, 8] | remaining: 1
Insert 1: 1 < 8, 1 < 5, 1 < 3, shift all right → [1, 3, 5, 8]
Result: [1, 3, 5, 8] - Sorted!

Advantages & Disadvantages

✅ Advantages: Simple to implement, efficient for small lists, works well when data is nearly sorted, sorts "in place" (no extra memory needed).

❌ Disadvantages: Slow for large lists, requires many shifts if data is in reverse order.

🎯 Correct Order

📦 Steps

🔀 Merge Sort

Merge Sort is a "divide and conquer" algorithm that divides the list into smaller sublists, sorts them, then merges them back together. It's more efficient than Bubble Sort and Insertion Sort for large datasets.

How It Works

  1. Divide: Split the list in half recursively until you have sublists of size 1
  2. Conquer: A list of 1 element is already sorted
  3. Merge: Combine sublists by comparing elements and placing them in order
  4. Continue merging until you have one fully sorted list

The Merge Process

When merging two sorted sublists:

  • Compare the first element of each sublist
  • Take the smaller one and add it to the result
  • Move to the next element in that sublist
  • Repeat until all elements are merged

Example: Sorting [38, 27, 43, 3]

Divide: [38, 27, 43, 3] → [38, 27] and [43, 3]
Divide: [38, 27] → [38] and [27] | [43, 3] → [43] and [3]
Merge: [38] + [27] → [27, 38] | [43] + [3] → [3, 43]
Merge: [27, 38] + [3, 43] → [3, 27, 38, 43]
Result: [3, 27, 38, 43] - Sorted!

Advantages & Disadvantages

✅ Advantages: Consistent performance, stable sort (preserves order of equal elements), excellent for large datasets.

❌ Disadvantages: Requires additional memory for merging, more complex to implement than Bubble Sort.

🎯 Correct Order

📦 Steps