Sorting Algorithms
Why this matters
Imagine you're a camp counselor in charge of lining up your group of kids for a photo. They're all jumbled up, and you need to arrange them from shortest to tallest. How would you do it?
You could find the shortest kid in the entire group and place them at the very front. Then, you could look at the remaining kids, find the next shortest, and place them in the second spot. Or, you could start with the first kid, then have the second kid stand in the right spot relative to the first. Then the third kid finds their spot among the first two, and so on, like people joining a line at a concession stand.
These are two different strategies—two different algorithms—for achieving the same goal. Today, we're going to explore the computer science versions of these strategies: Selection Sort and Insertion Sort.
Concept overview
flowchart TD
A[Start with unsorted array] --> B{Is there an unsorted part?};
B -- Yes --> C[Find the minimum value in the unsorted part];
C --> D[Swap minimum with the first element of the unsorted part];
D --> E[Mark that element as sorted];
E --> B;
B -- No --> F[End: Array is sorted];
Core explanation
When you have a jumble of data—test scores, contact names, product prices—you often need to put it in order. Sorting algorithms are the specific, step-by-step procedures a computer follows to do this. For the AP exam, you need to be able to trace two common sorting algorithms: Selection Sort and Insertion Sort.
Let's imagine we have an array of integers we want to sort from smallest to largest: [6, 2, 8, 4].
Selection Sort: The Patient Planner
Think of Selection Sort as the "find the best and put it in place" method. It works by repeatedly finding the smallest element in the unsorted part of the list and swapping it into its correct, final position.
Let's use our camp counselor analogy. You have a line of kids of different heights.
- Pass 1: You scan the entire line to find the absolute shortest kid. You then take that kid and swap them with the person currently at the front of the line. Now, the first position is correct and final. You can mentally "rope off" that spot.
- Pass 2: You look at the rest of the line (everyone except the first kid). You find the shortest kid among this remaining group and swap them into the second position. Now the first two spots are correct.
- You repeat this process, with your "unsorted" section getting smaller by one each time, until the whole line is sorted.
Let's trace Selection Sort with our array: [6, 2, 8, 4]
[[fig:selection_sort_trace]]
- Initial Array
[6, 2, 8, 4] - Pass 1
- Look through the whole array
[6, 2, 8, 4]. The minimum value is2. - Swap the minimum value (
2) with the element at the first position (6). - Result:
[2, 6, 8, 4]. The2is now in its final, sorted position.
- Look through the whole array
- Pass 2
- Look at the unsorted part:
[6, 8, 4]. The minimum value is4. - Swap the minimum (
4) with the element at the first position of the unsorted part (6). - Result:
[2, 4, 8, 6]. The2and4are now sorted.
- Look at the unsorted part:
- Pass 3
- Look at the unsorted part:
[8, 6]. The minimum value is6. - Swap the minimum (
6) with the element at the first position of the unsorted part (8). - Result:
[2, 4, 6, 8]. The2,4, and6are now sorted. The last element,8, must also be in the correct place, so we're done!
- Look at the unsorted part:
[[fig:selection_sort_mistake]]
Insertion Sort: The Tidy Organizer
Insertion Sort works like organizing a hand of playing cards. You start with an empty left hand (the "sorted" part) and pick up cards one by one from the pile on the table (the "unsorted" part), inserting each new card into its correct position in your hand.
- You assume the first element is a "sorted list of one."
- You take the next element from the unsorted section.
- You compare it to the elements in the sorted section, moving from right to left. You shift the larger elements to the right to make space.
- You "insert" the element into the gap you've created.
- Repeat until all elements have been inserted.
Let's trace Insertion Sort with the same array: [6, 2, 8, 4]
- Initial Array
[6, 2, 8, 4] - Pass 1
- The sorted portion is
[6]. The unsorted is[2, 8, 4]. - Take the next unsorted element:
2. - Compare
2with6.2is smaller, so shift6to the right. Insert2in its place. - Result:
[2, 6, 8, 4]. The sorted portion is now[2, 6].
- The sorted portion is
- Pass 2
- The sorted portion is
[2, 6]. The next unsorted element is8. - Compare
8with6.8is larger, so it's already in the correct spot relative to the sorted portion. No shifts needed. - Result:
[2, 6, 8, 4]. The sorted portion is now[2, 6, 8].
- The sorted portion is
- Pass 3
- The sorted portion is
[2, 6, 8]. The next unsorted element is4. - Compare
4with8.4is smaller, so shift8to the right. The array becomes[2, 6, _, 8]. - Compare
4with6.4is smaller, so shift6to the right. The array becomes[2, _, 6, 8]. - Compare
4with2.4is larger, so we've found the spot. Insert4into the gap. - Result:
[2, 4, 6, 8]. The entire array is now sorted.
- The sorted portion is
A critical point of confusion: With Insertion Sort, an element is placed in its correct position within the already sorted part, but this might not be its final position in the overall array until the very end. Notice in Pass 1, 6 moved to the second spot, but in Pass 3, it moved again to the third spot. This is different from Selection Sort, where each placement is final.
Both algorithms get the job done, but their internal processes are very different. On the AP exam, you'll be asked to trace these steps, so practice thinking through each pass for both methods.
See it in action
Worked examples
Let's walk through a couple of examples to make this crystal clear. Pay close attention to the state of the array after each major step or "pass."
Tracing Selection Sort
Problem: Show the state of the array [7, 3, 1, 9] after each pass of a Selection Sort that sorts in ascending (smallest to largest) order.
Solution: Remember, the rule for Selection Sort is: find the minimum in the unsorted portion and swap it with the first element of that portion.
- Initial State
[7, 3, 1, 9]The sorted portion is empty. The unsorted portion is the whole array. - After Pass 1
- Thinking: We scan
[7, 3, 1, 9]. The minimum value is1. - Action: Swap the minimum (
1) with the element at the start of the unsorted section (7). - Array State:
[1, 3, 7, 9] - Status: The
1is now locked in its final position. The sorted portion is[1].
- Thinking: We scan
- After Pass 2
- Thinking: We scan the remaining unsorted portion,
[3, 7, 9]. The minimum is3. - Action: Swap the minimum (
3) with the first element of this section (3). A swap with itself! This can happen, and it's perfectly fine. - Array State:
[1, 3, 7, 9](No visible change, but the logic was executed). - Status:
[1, 3]is now the sorted portion.
- Thinking: We scan the remaining unsorted portion,
- After Pass 3
- Thinking: We scan the unsorted portion,
[7, 9]. The minimum is7. - Action: Swap
7with itself. - Array State:
[1, 3, 7, 9] - Status:
[1, 3, 7]is now sorted. The last element9is automatically in place.
- Thinking: We scan the unsorted portion,
Final Sorted Array: [1, 3, 7, 9]
Tracing Insertion Sort
Problem: Show the state of the array [7, 3, 1, 9] after each pass of an Insertion Sort that sorts in ascending order.
Solution: Remember, Insertion Sort's rule is: take the next unsorted element and insert it into the growing sorted portion by shifting larger elements over.
-
Initial State:
[7, 3, 1, 9]We consider[7]as our initial sorted portion. -
After Pass 1 (inserting
3):- ThinkingTake
3. Compare it to7.3is smaller. - ActionShift
7one position to the right. Insert3into the created opening. - Array State
[3, 7, 1, 9] - StatusThe sorted portion is now
[3, 7].
- Thinking
-
After Pass 2 (inserting
1):- ThinkingTake
1. Compare it to7.1is smaller. Shift7right. Compare1to3.1is smaller. Shift3right. - ActionWe've reached the beginning of the array. Insert
1. - Array State
[1, 3, 7, 9] - StatusThe sorted portion is now
[1, 3, 7].
- Thinking
-
After Pass 3 (inserting
9):- ThinkingTake
9. Compare it to7.9is larger. - ActionNo shifts are needed.
9is already in the correct position relative to the sorted portion. - Array State
[1, 3, 7, 9] - StatusThe sorted portion is the entire array.
- Thinking
Final Sorted Array: [1, 3, 7, 9]
Try it yourself
Ready to try it on your own? Grab a piece of paper and trace these out. The physical act of writing down each step helps build the muscle memory you'll need on the exam.
- 1ProblemYour friend Marcus is tracking his daily basketball free throw percentages for a week:
[75, 82, 68, 90, 85]. Show the state of this array after each of the first two passes of a Selection Sort in ascending order.- Hint: For Pass 1, what's the absolute lowest percentage in the whole list? Where does it go? For Pass 2, what's the lowest percentage of the remaining four values?
- 2ProblemNow take that same initial array:
[75, 82, 68, 90, 85]. Show the state of the array after each of the first two passes of an Insertion Sort in ascending order.- Hint: For Pass 1, you're just inserting
82relative to75. For Pass 2, you're taking68and finding its correct spot within the[75, 82]sorted portion. What needs to shift?
- Hint: For Pass 1, you're just inserting
Practice — 8 questions
In simple terms, sorting algorithms are step-by-step methods, like recipes, that a computer uses to arrange a list of items, such as numbers or names, into a specific order (like smallest to largest).
- 4.15.A: Determine the result of executing each step of sorting algorithms to sort the elements of a collection.
- 4.15.A.1
- Selection sort and insertion sort are iterative sorting algorithms that can be used to sort elements in an array or ArrayList.
- 4.15.A.2
- Selection sort repeatedly selects the smallest (or largest) element from the unsorted portion of the list and swaps it into its correct (and final) position in the sorted portion of the list.
- 4.15.A.3
- Insertion sort inserts an element from the unsorted portion of a list into its correct (but not necessarily final) position in the sorted portion of the list by shifting elements of the sorted portion to make room for the new element.
flowchart TD
A[Start with unsorted array] --> B{Is there an unsorted part?};
B -- Yes --> C[Find the minimum value in the unsorted part];
C --> D[Swap minimum with the first element of the unsorted part];
D --> E[Mark that element as sorted];
E --> B;
B -- No --> F[End: Array is sorted];
Read what Saavi narrates
(gentle, warm intro music fades)
Hi there, it's Saavi from Shrutam. Let's talk about getting organized.
Imagine you're a camp counselor trying to line up your kids for a photo, from shortest to tallest. How would you do it? You could find the shortest kid in the whole group and put them at the front. Then find the next shortest from the rest, and put them in the second spot. Or… you could start with one kid, and have the next kid just find their spot relative to the first, and so on, building the line one kid at a time.
These are two different strategies, or algorithms, for sorting. In computer science, we call them Selection Sort and Insertion Sort. They are both step-by-step methods a computer uses to arrange a list of items, like numbers in an array.
Let's walk through an example of Selection Sort. Imagine we have an array of numbers: seven, three, one, nine.
Our rule is: find the smallest number in the unsorted part, and swap it into the first available spot.
So, in our first pass, we look at the whole array: seven, three, one, nine. The smallest is one. So, we swap the one with the number in the first position, which is seven. Our array is now: one, three, seven, nine. The number one is now locked in its final, correct spot.
For our second pass, we ignore the one. We look at the rest: three, seven, nine. The smallest number here is three. We need to swap it with the first element of this section… which is also three. So, we swap it with itself. The array doesn't change visually, but the step was still performed. Now, one and three are sorted.
We'd continue this until the whole array is sorted. The key is that each pass puts one number in its final, permanent home.
Now, here's a really common mistake students make with Selection Sort. They think they should swap numbers every time they find a smaller one while scanning. That's not right. You scan the whole section to *find* the minimum, but you only do *one* swap at the end of the pass. Think of it like finding the winner of a race... you wait for everyone to finish before you award the medal.
Sorting might seem tricky at first, but it's just following a set of rules, step by step. You are absolutely capable of mastering this. Just be patient with the process, and with yourself.
(gentle, warm outro music fades in)
Selection Sort *selects* a minimum and does one *swap* per pass. Insertion Sort *inserts* an element by *shifting* multiple others. Mixing them up (e.g., shifting in Selection Sort) will get you the wrong answer on a tracing question.
Memorize a simple analogy for each. Selection Sort = "awarding a gold medal" (find the best, put them on the podium). Insertion Sort = "organizing a hand of cards" (take one, slide it in).
A pass in Selection Sort is for *finding* the minimum. The swap only happens once, at the very end of the pass, to place that minimum in its final position.
Use a mental variable, like `minIndex`, to keep track of the minimum's location as you scan. Only when the scan is complete do you perform the swap.
If you take `4` from `[2, 6, 8, 4]` and just place it where `8` is, you lose the `8`. The array becomes `[2, 6, 4, 4]`. Shifting preserves all the data.
When you need to make space, think of it as a chain reaction. The element at index `i` moves to `i+1`, the one at `i-1` moves to `i`, and so on, until a gap is opened.
Insertion Sort only guarantees an element is sorted relative to the elements *before* it. In our example `[7, 3, 1, 9]`, after inserting `3`, the array was `[3, 7, 1, 9]`. The `7` was in the second position, but it ended up in the third.
Remember that only in Selection Sort is each placement final. In Insertion Sort, elements can be shifted multiple times.
For an array of size `n`, both algorithms generally require `n-1` passes to guarantee a sorted result. Stopping early might leave the last one or two elements out of order.
If an array has 5 elements, be prepared to trace 4 full passes. Don't stop just because it "looks" sorted.