📖 Scenario: You work in a small shop that keeps a list of daily sales amounts. The list is not sorted, and you want to organize it from smallest to largest to see trends easily.
🎯 Goal: You will build a simple program that sorts a list of sales amounts using the insertion sort method. This will help you understand how sorting works step-by-step.
📋 What You'll Learn
Create an integer array called sales with exact values
Create an integer variable called n to hold the length of the array
Use a for loop with variable i starting from 1 to n
Inside the loop, use a variable key to hold the current element
Use a while loop with variable j to shift elements greater than key
Print the sorted sales array in one line separated by spaces
💡 Why This Matters
🌍 Real World
Sorting sales data helps businesses analyze trends and make decisions.
💼 Career
Understanding sorting algorithms is fundamental for software development and data processing roles.
Progress0 / 4 steps
1
Create the sales data array
Create an integer array called sales with these exact values: 45, 23, 78, 12, 56.
DSA Java
Hint
Use curly braces {} to list the numbers inside the array.
2
Add the length variable
Add an integer variable called n that stores the length of the sales array using sales.length.
DSA Java
Hint
Use sales.length to get the number of elements in the array.
3
Implement the insertion sort logic
Use a for loop with variable i from 1 to n. Inside it, create an integer key equal to sales[i]. Use a while loop with variable j starting at i - 1 to shift elements greater than key one position right. Insert key at the correct position.
DSA Java
Hint
Remember to move elements one step right inside the while loop and place key after the loop ends.
4
Print the sorted sales array
Use a for loop with variable i from 0 to n to print each element of sales separated by spaces on one line.
DSA Java
Hint
Use System.out.print() inside the loop to print elements with a space after each.
Practice
(1/5)
1. What is the main idea behind the Insertion Sort algorithm?
easy
A. Select the smallest element and swap it with the first unsorted element
B. Divide the array into halves and sort each half separately
C. Swap adjacent elements if they are in the wrong order repeatedly
D. Build the sorted array one element at a time by inserting elements in the correct position
Solution
Step 1: Understand insertion sort process
Insertion sort builds the sorted array 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 sorting algorithms: divide and conquer (merge sort), swapping adjacent elements (bubble sort), selecting smallest element (selection sort).
Final Answer:
Build the sorted array one element at a time by inserting elements in the correct position -> Option D
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 into halves
Mixing insertion sort with selection sort
2. Which of the following is the correct syntax to start the outer loop for insertion sort in Java for an array arr of length n?
easy
A. for (int i = 0; i < n; i++)
B. for (int i = n - 1; i >= 0; i--)
C. for (int i = 1; i < n; i++)
D. for (int i = 1; i <= n; i++)
Solution
Step 1: Identify insertion sort loop start
Insertion sort starts from the second element (index 1) because the first element is considered sorted.
Step 2: Check loop conditions
The loop runs from i = 1 up to i < n (not i <= n) to avoid index out of bounds.
Final Answer:
for (int i = 1; i < n; i++) -> Option C
Quick Check:
Start from second element: i=1 [OK]
Hint: Start outer loop at 1, not 0, in insertion sort [OK]
Common Mistakes:
Starting loop at 0 instead of 1
Using i <= n causing index errors
Looping backwards incorrectly
3. What will be the output array after applying insertion sort on int[] arr = {4, 3, 2, 1};?
4. Consider this insertion sort code snippet in Java:
for (int i = 1; i < arr.length; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j] = key;
}
What is the error in this code?
medium
A. The assignment should be arr[j + 1] = key; instead of arr[j] = key;
B. The loop should start from i = 0 instead of 1
C. The condition in while loop should be arr[j] < key
D. The decrement j-- should be j++
Solution
Step 1: Analyze where key is placed after shifting
After shifting elements greater than key to the right, key should be placed at arr[j + 1] because j was decremented one extra time.
Step 2: Identify incorrect assignment
The code assigns arr[j] = key; which overwrites the wrong index. Correct is arr[j + 1] = key;.
Final Answer:
The assignment should be arr[j + 1] = key; instead of arr[j] = key; -> Option A
Quick Check:
Place key at arr[j+1] after shifting [OK]
Hint: Place key at arr[j+1] after shifting elements [OK]
Common Mistakes:
Assigning key to arr[j] instead of arr[j+1]
Starting loop at wrong index
Wrong while condition direction
5. You have an array int[] arr = {5, 3, 8, 6, 2};. After performing insertion sort, what will be the state of the array after the third iteration of the outer loop?