📖 Scenario: You have a small list of numbers that you want to arrange from smallest to largest, just like organizing books by height on a shelf.
🎯 Goal: Build a program that sorts a list of numbers using the insertion sort method, which picks one number at a time and places it in the right spot.
📋 What You'll Learn
Create an array called numbers with the exact values: 8, 3, 5, 1, 9
Create an integer variable called n that stores the size of the array
Use a for loop with variable i starting from 1 to n - 1
Inside the loop, use a variable key to hold the current number at index i
Use a while loop with variable j to move numbers greater than key one position ahead
Place the key in its correct sorted position
Print the sorted array in one line separated by spaces
💡 Why This Matters
🌍 Real World
Sorting is used in many places like organizing files, arranging data for faster search, or displaying lists in order.
💼 Career
Understanding sorting algorithms like insertion sort helps in software development, data analysis, and preparing for technical interviews.
Progress0 / 4 steps
1
Create the array of numbers
Create an integer array called numbers with these exact values: 8, 3, 5, 1, 9. Also create an integer variable called n and set it to the size of the array, which is 5.
DSA C
Hint
Use curly braces to list the numbers inside the array. Set n to 5 because there are 5 numbers.
2
Set up the outer loop for insertion sort
Add a for loop with variable i starting from 1 up to n - 1 to go through each number in the array except the first one.
DSA C
Hint
The loop starts at 1 because the first element is considered sorted.
3
Implement the insertion logic inside the loop
Inside the for loop, create an integer variable key and set it to numbers[i]. Create another integer variable j and set it to i - 1. Then use a while loop that runs while j >= 0 and numbers[j] > key. Inside the while loop, move numbers[j] to numbers[j + 1] and decrease j by 1. After the while loop, place key at numbers[j + 1].
DSA C
Hint
Think of key as the number you want to insert in the right place. Move bigger numbers one step right to make space.
4
Print the sorted array
After the for loop, use a for loop with variable k from 0 to n - 1 to print each number in numbers separated by a space. Use printf("%d ", numbers[k]). Then print a newline with printf("\n").
DSA C
Hint
Print each number followed by a space, then print a newline after the loop.
Practice
(1/5)
1. What is the main idea behind the Insertion Sort algorithm?
easy
A. Build the sorted array one element at a time by inserting elements in the correct position
B. Divide the array into halves and sort each half recursively
C. Swap adjacent elements if they are in the wrong order repeatedly
D. Use a heap data structure to sort elements
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 among the already sorted elements.
Step 2: Compare with other sorting methods
Other options describe different algorithms: divide and conquer is merge sort, swapping adjacent elements is bubble sort, and heap data structure is heap sort.
Final Answer:
Build the sorted array one element at a time by inserting elements in the correct position -> Option A
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 sort
Thinking insertion sort divides array recursively
Mixing up heap sort with insertion sort
2. Which of the following is the correct syntax for the inner loop in insertion sort in C?
easy
A. for (int j = n - 1; j > i; j--)
B. for (int j = 0; j < i; j++)
C. for (int j = i - 1; j >= 0 && arr[j] > key; j--)
D. for (int j = i + 1; j < n; j++)
Solution
Step 1: Identify inner loop role in insertion sort
The inner loop moves backward from the current element's previous index, shifting elements greater than the key to the right.
Step 2: Match syntax with this logic
for (int j = i - 1; j >= 0 && arr[j] > key; j--) correctly uses a loop starting from i-1 down to 0, checking if arr[j] > key and decrementing j.
Final Answer:
for (int j = i - 1; j >= 0 && arr[j] > key; j--) -> Option C
Quick Check:
Inner loop moves backward while arr[j] > key [OK]
Hint: Inner loop goes backward checking elements > key [OK]
Common Mistakes:
Using forward loop instead of backward
Not checking arr[j] > key condition
Incorrect loop boundaries
3. What will be the output array after running insertion sort on int arr[] = {4, 3, 2, 1};?
medium
A. [4, 3, 2, 1]
B. [1, 3, 2, 4]
C. [2, 3, 4, 1]
D. [1, 2, 3, 4]
Solution
Step 1: Trace insertion sort on given array
Start with 4, then insert 3 before 4, array becomes [3,4,2,1]. Insert 2 before 3, array becomes [2,3,4,1]. Insert 1 before 2, array becomes [1,2,3,4].
Step 2: Confirm final sorted array
After all insertions, the array is sorted ascending: [1, 2, 3, 4].