📖 Scenario: You have a list of unsorted numbers representing daily temperatures. You want to arrange them from lowest to highest to easily see the trend.
🎯 Goal: Build a program that sorts a list of integers using the insertion sort algorithm step-by-step.
📋 What You'll Learn
Create a slice of integers with exact values
Use a variable to track the current index in the sorting process
Implement the insertion sort logic using loops
Print the sorted slice at the end
💡 Why This Matters
🌍 Real World
Sorting data like temperatures helps in analyzing trends and making decisions, such as weather forecasting or climate studies.
💼 Career
Understanding sorting algorithms is fundamental for software development roles, especially when optimizing data processing and improving performance.
Progress0 / 4 steps
1
Create the initial slice of temperatures
Create a slice called temps with these exact integer values: 29, 15, 42, 8, 33
DSA Go
Hint
Use temps := []int{29, 15, 42, 8, 33} to create the slice.
2
Add a variable to track the current index
Add a variable called i of type int to track the current index in the sorting process, starting from 1
DSA Go
Hint
Write i := 1 to start from the second element.
3
Implement the insertion sort logic
Use a for loop with variable i from 1 to the length of temps. Inside it, use a variable key to hold temps[i] and another variable j starting at i - 1. Use a for loop to move elements greater than key one position ahead. Insert key at the correct position.
DSA Go
Hint
Use nested loops to shift elements and insert the key in the right place.
4
Print the sorted slice
Print the sorted temps slice using fmt.Println(temps)
DSA Go
Hint
Use fmt.Println(temps) to display the sorted slice.
Practice
(1/5)
1. What is the main idea behind the Insertion Sort algorithm?
easy
A. Divide the array into halves and sort each half recursively
B. Build the sorted array one element at a time by inserting elements into their correct position
C. Swap adjacent elements if they are in wrong order repeatedly
D. Select the smallest element and swap it with the first unsorted element
Solution
Step 1: Understand insertion sort process
Insertion sort builds the sorted list by taking one element at a time and placing it in the correct position relative to the already sorted part.
Step 2: Compare with other sorting methods
Other options describe merge sort (B), bubble sort (C), and selection sort (D), which are different algorithms.
Final Answer:
Build the sorted array one element at a time by inserting elements into their correct position -> Option B
Quick Check:
Insertion sort = Insert elements step-by-step [OK]
Hint: Insertion sort inserts elements one by one in order [OK]
Common Mistakes:
Confusing insertion sort with bubble or selection sort
Thinking insertion sort divides array recursively
Assuming insertion sort swaps all elements repeatedly
2. Which of the following Go code snippets correctly starts the outer loop for insertion sort on a slice arr?
easy
A. for i := 1; i < len(arr); i++ {
B. for i := 0; i < len(arr); i++ {
C. for i := len(arr)-1; i >= 0; i-- {
D. for i := 1; i <= len(arr); i++ {
Solution
Step 1: Identify correct loop start for insertion sort
Insertion sort starts from the second element (index 1) because the first element is considered sorted.
Step 2: Check loop conditions
Loop runs while i is less than length of array. for i := 1; i < len(arr); i++ { matches this exactly.
Final Answer:
for i := 1; i < len(arr); i++ { -> Option A
Quick Check:
Start from index 1 to end [OK]
Hint: Start outer loop at 1, not 0, for insertion sort [OK]
Common Mistakes:
Starting loop at 0 instead of 1
Using <= instead of < in loop condition
Looping backwards incorrectly
3. What will be the output slice after running insertion sort on arr := []int{4, 3, 2, 1}?
medium
A. [1, 3, 2, 4]
B. [4, 3, 2, 1]
C. [1, 2, 3, 4]
D. [2, 3, 4, 1]
Solution
Step 1: Trace insertion sort on given array
Start with 4 (sorted). Insert 3 before 4 -> [3,4,2,1]. Insert 2 before 3 -> [2,3,4,1]. Insert 1 before 2 -> [1,2,3,4].
Step 2: Confirm final sorted slice
The array is sorted in ascending order: [1, 2, 3, 4].