Lesson 01
How Sorting Algorithms Reorder Data
Sorting is basically taking an unsorted list of numbers or text and rearranging everything in order, usually from smallest to largest. It sounds simple, but different algorithms go about it in totally different ways.
1. Bubble Sort
Bubble sort is usually the first algorithm people learn because the idea is so basic. It goes down the list comparing neighbor items two at a time, swapping them if they are in the wrong order until everything is sorted.
How it works step by step:
- Start at the very beginning of your list with the first two items.
- Compare the left number with the right number.
- If the left number is bigger, swap their positions.
- Move over one spot to the right and repeat the comparison.
- Keep looping through the whole list until you can make a full pass without swapping anything.
Visualizer: Bubble Sort in Action
Here is a helpful animation showing how neighboring numbers compare and swap step by step until the entire array is arranged in order:
Audio Overview: Visualizing Swaps
Listen to my quick breakdown on how array swaps and algorithm passes function during Bubble Sort:
Other Classic Sorting Methods
- Selection Sort: Searches the whole list to find the smallest value, puts it at the front, and then repeats that process for whatever is left.
- Insertion Sort: Builds up a sorted list one item at a time by inserting each new item right where it belongs.