📖 Scenario: You have a list of numbers that you want to arrange from smallest to largest. This is like organizing books on a shelf by their height, one by one.
🎯 Goal: Build a program that sorts a list of numbers using the insertion sort method, showing how the list becomes ordered step by step.
📋 What You'll Learn
Create an integer array called numbers with the exact values: 8, 3, 5, 4, 6
Create an integer variable called n that stores the length of the numbers array
Use a for loop with variable i starting from 1 to n - 1 to iterate over the array
Inside the loop, create an integer variable called key that stores numbers[i]
Use a while loop with variable j starting from i - 1 to move elements greater than key one position ahead
After sorting, print the sorted array elements separated by spaces
💡 Why This Matters
🌍 Real World
Sorting is used in organizing data like names, prices, or dates to find information quickly.
💼 Career
Understanding sorting algorithms is important for software developers to write efficient code for data handling.
Progress0 / 4 steps
1
Create the initial array
Create an integer array called numbers with these exact values: 8, 3, 5, 4, 6
DSA C#
Hint
Use int[] numbers = {8, 3, 5, 4, 6}; to create the array.
2
Add the length variable
Create an integer variable called n that stores the length of the numbers array
DSA C#
Hint
Use numbers.Length to get the array size.
3
Implement the insertion sort logic
Use a for loop with variable i from 1 to n - 1. Inside it, create an integer variable key equal to numbers[i]. Then use a while loop with variable j starting from i - 1 to move elements greater than key one position ahead and finally place key in the correct position.
DSA C#
Hint
Remember to move elements greater than key one step right inside the while loop.
4
Print the sorted array
Print the elements of the numbers array separated by spaces using Console.WriteLine and string.Join
DSA C#
Hint
Use Console.WriteLine(string.Join(" ", numbers)); to print the sorted array.
Practice
(1/5)
1. What is the main idea behind the Insertion Sort algorithm?
easy
A. It builds the sorted array one element at a time by inserting elements in their correct position.
B. It divides the array into halves and sorts each half recursively.
C. It repeatedly swaps adjacent elements if they are in the wrong order.
D. It selects the smallest element and swaps it with the first unsorted element.
Solution
Step 1: Understand insertion sort process
Insertion sort takes one element at a time and inserts it into the correct position in the already sorted part of the array.
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:
It builds the sorted array one element at a time by inserting elements in their 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 selection sort with insertion sort
2. Which of the following is the correct syntax to start the outer loop for insertion sort in C# for an array arr of length n?
easy
A. for (int i = 0; i < n; i++)
B. for (int i = 1; i <= n; i++)
C. for (int i = n - 1; i >= 0; i--)
D. for (int i = 1; i < n; i++)
Solution
Step 1: Identify starting index for insertion sort
Insertion sort starts from the second element (index 1) because the first element is considered sorted.
Step 2: Check loop conditions
The loop runs while i < n to cover all elements. for (int i = 1; i < n; i++) matches this.
Final Answer:
for (int i = 1; i < n; i++) -> Option D
Quick Check:
Start from 1 to n-1 for insertion sort loop [OK]
Hint: Start outer loop at 1, not 0, for insertion sort [OK]
Common Mistakes:
Starting loop at 0 instead of 1
Using <= n instead of < n causing out-of-bounds
Counting backwards incorrectly
3. What will be the output array after applying insertion sort on int[] arr = {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 array
The array is sorted in ascending order: [1, 2, 3, 4].