0
0
Intro to Computingfundamentals~6 mins

Sorting algorithms (bubble, selection) in Intro to Computing - Full Explanation

Choose your learning style9 modes available
Introduction
Imagine you have a messy pile of books and want to arrange them from shortest to tallest. Sorting algorithms help computers organize data in a specific order, making it easier to find or use later.
Explanation
Bubble Sort
Bubble sort works by repeatedly stepping through the list, comparing pairs of items next to each other. If a pair is in the wrong order, it swaps them. This process repeats until no more swaps are needed, meaning the list is sorted.
Bubble sort sorts by swapping adjacent items repeatedly until the whole list is ordered.
Selection Sort
Selection sort divides the list into a sorted part and an unsorted part. It repeatedly finds the smallest item in the unsorted part and moves it to the front of the sorted part. This continues until all items are sorted.
Selection sort sorts by repeatedly selecting the smallest remaining item and placing it in order.
Real World Analogy

Imagine lining up students by height. Bubble sort is like students comparing heights with their neighbor and swapping places if needed, passing through the line many times. Selection sort is like picking the shortest student from the group and placing them at the front one by one.

Bubble Sort → Students comparing heights with their neighbor and swapping places repeatedly
Selection Sort → Picking the shortest student from the group and placing them at the front one by one
Diagram
Diagram
Unsorted List: 5 3 8 4 2

Bubble Sort Passes:
Pass 1: 3 5 4 2 8
Pass 2: 3 4 2 5 8
Pass 3: 3 2 4 5 8
Pass 4: 2 3 4 5 8

Selection Sort Steps:
Step 1: Find min (2), swap with first (5) → 2 3 8 4 5
Step 2: Find min (3), already in place → 2 3 8 4 5
Step 3: Find min (4), swap with third (8) → 2 3 4 8 5
Step 4: Find min (5), swap with fourth (8) → 2 3 4 5 8
Step 5: Last item sorted2 3 4 5 8
This diagram shows step-by-step how bubble sort swaps adjacent items and how selection sort picks the smallest item to place in order.
Key Facts
Bubble SortA simple sorting method that swaps adjacent items repeatedly until sorted.
Selection SortA sorting method that selects the smallest item from unsorted data and moves it to the sorted part.
SwapExchanging the positions of two items in a list.
Sorted PartThe portion of the list that is already arranged in order.
Unsorted PartThe portion of the list that still needs to be arranged.
Common Confusions
Thinking bubble sort compares items far apart, not just neighbors.
Thinking bubble sort compares items far apart, not just neighbors. Bubble sort only compares and swaps adjacent items in each pass.
Believing selection sort swaps items every time it looks at them.
Believing selection sort swaps items every time it looks at them. Selection sort only swaps once per pass after finding the smallest item.
Summary
Bubble sort organizes a list by swapping neighbors repeatedly until sorted.
Selection sort organizes a list by repeatedly picking the smallest item and placing it in order.
Both methods help computers arrange data but work in different ways.