Introduction
Insertion Sort is a simple sorting algorithm that builds the sorted array one element at a time. It takes an element from the unsorted portion and places it in its correct position among the already sorted elements. In this chapter, we will practice Insertion Sort using JavaScript through practical questions covering ascending and descending order, duplicate values, strings, objects, passes, comparisons, and reusable functions. Data Structure Insertion Sort practice questions with solutions help to build concepts.
Question 1: Sort an Array Using Insertion Sort
Questions
Use Insertion Sort to arrange the following numbers in ascending order.
let numbers = [9, 5, 1, 4, 3];
Solution
let numbers = [9, 5, 1, 4, 3];
for (let i = 1; i < numbers.length; i++) {
let key = numbers[i];
let j = i - 1;
while (j >= 0 && numbers[j] > key) {
numbers[j + 1] = numbers[j];
j--;
}
numbers[j + 1] = key;
}
console.log(numbers);
Output
[1, 3, 4, 5, 9]
The algorithm takes each element and inserts it into its correct position in the sorted portion.
Question 2: Sort an Array in Descending Order
Questions
Use Insertion Sort to arrange the following numbers from largest to smallest.
let numbers = [12, 5, 19, 8, 3];
Solution
let numbers = [12, 5, 19, 8, 3];
for (let i = 1; i < numbers.length; i++) {
let key = numbers[i];
let j = i - 1;
while (j >= 0 && numbers[j] < key) {
numbers[j + 1] = numbers[j];
j--;
}
numbers[j + 1] = key;
}
console.log(numbers);
Output
[19, 12, 8, 5, 3]
For descending order, larger elements are shifted toward the beginning.
Question 3: Count the Number of Shifts
Questions
Use Insertion Sort to sort the array and count how many times an element is shifted.
let numbers = [5, 4, 3, 2, 1];
Solution
let numbers = [5, 4, 3, 2, 1];
let shifts = 0;
for (let i = 1; i < numbers.length; i++) {
let key = numbers[i];
let j = i - 1;
while (j >= 0 && numbers[j] > key) {
numbers[j + 1] = numbers[j];
j--;
shifts++;
}
numbers[j + 1] = key;
}
console.log("Sorted array:", numbers);
console.log("Total shifts:", shifts);
Output
Sorted array: [1, 2, 3, 4, 5]
Total shifts: 10
An element is shifted whenever a larger element needs to move one position to the right.
Question 4: Display the Array After Every Pass
Questions
Use Insertion Sort and display the array after every pass.
let numbers = [8, 4, 6, 2, 7];
Solution
let numbers = [8, 4, 6, 2, 7];
for (let i = 1; i < numbers.length; i++) {
let key = numbers[i];
let j = i - 1;
while (j >= 0 && numbers[j] > key) {
numbers[j + 1] = numbers[j];
j--;
}
numbers[j + 1] = key;
console.log("Pass " + i + ":", numbers);
}
Output
Pass 1: [4, 8, 6, 2, 7]
Pass 2: [4, 6, 8, 2, 7]
Pass 3: [2, 4, 6, 8, 7]
Pass 4: [2, 4, 6, 7, 8]
After every pass, the sorted portion becomes one element larger.
Question 5: Sort an Already Sorted Array
Questions
Use Insertion Sort on an already sorted array and count how many shifts are performed.
let numbers = [1, 2, 3, 4, 5];
Solution
let numbers = [1, 2, 3, 4, 5];
let shifts = 0;
for (let i = 1; i < numbers.length; i++) {
let key = numbers[i];
let j = i - 1;
while (j >= 0 && numbers[j] > key) {
numbers[j + 1] = numbers[j];
j--;
shifts++;
}
numbers[j + 1] = key;
}
console.log("Sorted array:", numbers);
console.log("Shifts:", shifts);
Output
Sorted array: [1, 2, 3, 4, 5]
Shifts: 0
Because the array is already sorted, no shifting is necessary.
Question 6: Sort an Array with Duplicate Values
Questions
Use Insertion Sort to arrange the following array in ascending order while keeping duplicate values.
let numbers = [4, 2, 4, 1, 3, 2];
Solution
let numbers = [4, 2, 4, 1, 3, 2];
for (let i = 1; i < numbers.length; i++) {
let key = numbers[i];
let j = i - 1;
while (j >= 0 && numbers[j] > key) {
numbers[j + 1] = numbers[j];
j--;
}
numbers[j + 1] = key;
}
console.log(numbers);
Output
[1, 2, 2, 3, 4, 4]
Insertion Sort keeps duplicate values while arranging the elements.
Question 7: Sort Strings by Alphabetical Order
Questions
Use Insertion Sort to arrange these programming languages alphabetically.
let languages = ["Python", "C", "Java", "Ruby", "Go"];
Solution
let languages = ["Python", "C", "Java", "Ruby", "Go"];
for (let i = 1; i < languages.length; i++) {
let key = languages[i];
let j = i - 1;
while (j >= 0 && languages[j] > key) {
languages[j + 1] = languages[j];
j--;
}
languages[j + 1] = key;
}
console.log(languages);
Output
["C", "Go", "Java", "Python", "Ruby"]
The comparison between strings determines their alphabetical order.
Question 8: Sort Students by Marks
Questions
Use Insertion Sort to arrange students from highest marks to lowest marks.
let students = [
{ name: "Rahul", marks: 72 },
{ name: "Priya", marks: 91 },
{ name: "Aman", marks: 65 },
{ name: "Neha", marks: 84 }
];
Solution
let students = [
{ name: "Rahul", marks: 72 },
{ name: "Priya", marks: 91 },
{ name: "Aman", marks: 65 },
{ name: "Neha", marks: 84 }
];
for (let i = 1; i < students.length; i++) {
let key = students[i];
let j = i - 1;
while (j >= 0 && students[j].marks < key.marks) {
students[j + 1] = students[j];
j--;
}
students[j + 1] = key;
}
console.log(students);
Output
[
{ name: "Priya", marks: 91 },
{ name: "Neha", marks: 84 },
{ name: "Rahul", marks: 72 },
{ name: "Aman", marks: 65 }
]
The marks property determines the sorting order.
Question 9: Create a Reusable Insertion Sort Function
Questions
Create a reusable function called insertionSort() that accepts an array and returns the sorted array.
let numbers = [20, 7, 15, 3, 10];
Solution
function insertionSort(array) {
for (let i = 1; i < array.length; i++) {
let key = array[i];
let j = i - 1;
while (j >= 0 && array[j] > key) {
array[j + 1] = array[j];
j--;
}
array[j + 1] = key;
}
return array;
}
let numbers = [20, 7, 15, 3, 10];
console.log(insertionSort(numbers));
Output
[3, 7, 10, 15, 20]
The function can be reused for different numerical arrays.
Question 10: Insert a New Value into a Sorted Array
Questions
The array is already sorted. Insert 25 into its correct position using the basic Insertion Sort idea.
let numbers = [10, 20, 30, 40];
let newValue = 25;
Solution
let numbers = [10, 20, 30, 40];
let newValue = 25;
numbers.push(newValue);
let i = numbers.length - 1;
let key = numbers[i];
let j = i - 1;
while (j >= 0 && numbers[j] > key) {
numbers[j + 1] = numbers[j];
j--;
}
numbers[j + 1] = key;
console.log(numbers);
Output
[10, 20, 25, 30, 40]
The new value is placed between 20 and 30, keeping the array sorted.
Key Takeaways
- Insertion Sort builds the sorted portion one element at a time.
- The first element is considered sorted initially.
- The next element is stored in a
keyvariable. - Larger elements are shifted to the right.
- The
keyis then inserted into its correct position. - Insertion Sort can work in ascending and descending order.
- It can sort numbers, strings, and objects.
- Duplicate values can be handled normally.
- Insertion Sort performs very well when data is already or nearly sorted.
- Its best-case time complexity is O(n).
- Its average-case and worst-case time complexity is O(n²).
- Its standard in-place implementation uses O(1) extra space.
- Insertion Sort is useful for small datasets and nearly sorted data.
FAQs
1. What is Insertion Sort?
Insertion Sort is a sorting algorithm that takes elements one at a time and inserts each element into its correct position within the already sorted portion.
2. How does Insertion Sort work?
It selects a key element, compares it with previous elements, shifts larger elements to the right, and places the key in its correct position.
3. What is the time complexity of Insertion Sort?
The best-case time complexity is O(n), while the average-case and worst-case complexity is O(n²).
4. When does Insertion Sort perform well?
Insertion Sort performs particularly well when the array is already sorted or nearly sorted.
5. Can Insertion Sort handle duplicate values?
Yes. It can sort duplicate values without removing them.
6. Can Insertion Sort sort strings and objects?
Yes. The comparison condition can be changed to sort strings or object properties such as marks, price, age, or ID.
7. What is the space complexity of Insertion Sort?
The standard in-place implementation requires O(1) extra space.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
