Insertion Sort is a simple and beginner-friendly sorting algorithm that works the way people often sort cards in their hands. You take one item at a time and insert it into its correct position among the already sorted items. Because this idea feels very natural, Insertion Sort is one of the easiest algorithms to understand when you are new to programming.
with hands-on learning.
get the skills and confidence to land your next move.
Insertion Sort matters because it teaches you how data shifts inside an array and how comparisons guide logic. It is commonly used for small datasets, nearly sorted data, and as a building block inside more advanced algorithms. Learning how to write an Insertion Sort program in TypeScript helps beginners improve their understanding of loops, conditions, arrays, and clean coding habits that are useful in real-world applications.
Program 1: Basic Insertion Sort Using For Loops
This program shows the most common and traditional way to implement Insertion Sort. It uses nested loops to pick elements and insert them into the correct position. This is the best place to start for beginners.
let numbers: number[] = [12, 11, 13, 5, 6];
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("Sorted array:", numbers);This program works by assuming the first element is already sorted. It then takes the next element and moves it left until it finds the correct position. Beginners can clearly see how values shift to make room for the new element, which makes the logic easy to follow and remember.
Program 2: Insertion Sort Using a Function
This version places the Insertion Sort logic inside a reusable function. It helps beginners understand how functions improve code structure and reuse.
function insertionSort(arr: number[]): number[] {
for (let i = 1; i < arr.length; i++) {
let key = arr[i];
let j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
return arr;
}
let data: number[] = [9, 7, 5, 3, 1];
console.log("Sorted array:", insertionSort(data));Using a function makes the code cleaner and easier to manage. Beginners also learn how to pass arrays as parameters and return results. This approach is useful in real projects where sorting logic needs to be reused in different places.
Program 3: Insertion Sort Using a While Loop
This program uses a while loop instead of a for loop to control the outer iteration. It shows that the same algorithm can be written in different styles.
let values: number[] = [8, 4, 6, 2, 9];
let i = 1;
while (i < values.length) {
let key = values[i];
let j = i - 1;
while (j >= 0 && values[j] > key) {
values[j + 1] = values[j];
j--;
}
values[j + 1] = key;
i++;
}
console.log("Sorted array:", values);This version helps beginners practice manual loop control and understand how while loops behave. Even though the structure looks different, the sorting logic remains the same. This reinforces the idea that understanding the algorithm is more important than memorizing syntax.
Program 4: Recursive Insertion Sort in TypeScript
This program uses recursion to implement Insertion Sort. While recursion is not commonly used for this algorithm, it is a valuable learning exercise.
function recursiveInsertionSort(arr: number[], n: number): number[] {
if (n <= 1) {
return arr;
}
recursiveInsertionSort(arr, n - 1);
let last = arr[n - 1];
let j = n - 2;
while (j >= 0 && arr[j] > last) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = last;
return arr;
}
let nums: number[] = [7, 3, 5, 2, 4];
console.log("Sorted array:", recursiveInsertionSort(nums, nums.length));Each recursive call sorts a smaller part of the array before inserting the last element into its correct place. This program helps beginners understand how a large problem can be solved step by step using smaller pieces. Learning recursion early makes future algorithms easier to grasp.
Program 5: Insertion Sort in Descending Order
This final program sorts the array in descending order instead of ascending order. It shows how a small change in logic can produce a different result.
let items: number[] = [10, 1, 7, 4, 8];
for (let i = 1; i < items.length; i++) {
let key = items[i];
let j = i - 1;
while (j >= 0 && items[j] < key) {
items[j + 1] = items[j];
j--;
}
items[j + 1] = key;
}
console.log("Sorted array in descending order:", items);Instead of moving smaller values left, this program moves larger values first. Beginners can see how adjusting one comparison changes the entire output. This flexibility is helpful when sorting data in different ways.
Frequently Asked Questions (FAQ)
This section answers common beginner questions about Insertion Sort in TypeScript in a simple and friendly way.
Q1. Where is Insertion Sort used in real life?
Insertion Sort is used for small datasets and nearly sorted data. It is also used inside more advanced algorithms.
Q2. Is Insertion Sort better than Bubble Sort?
Insertion Sort usually performs better than Bubble Sort, especially when the data is already partially sorted.
Q3. Can Insertion Sort work with negative numbers?
Yes, Insertion Sort works with negative numbers as long as comparisons are done correctly.
Q4. Is recursion required for Insertion Sort?
No, recursion is optional. It is mainly used to help learners understand recursive thinking.
Q5. What should I learn after Insertion Sort?
After Insertion Sort, beginners often move on to Selection Sort, Merge Sort, and Quick Sort.
Conclusion
Insertion Sort is a simple and powerful algorithm that helps beginners understand how sorting really works. In this article, we explored multiple TypeScript programs that implement Insertion Sort using loops, functions, recursion, and reverse ordering. Each example showed a different way to approach the same problem while keeping the logic easy to understand.
If you are new to TypeScript or sorting algorithms, try running these programs and changing the input values. Practice will help you build confidence and prepare you for more advanced topics. Keep experimenting, keep learning, and enjoy the process of becoming a better programmer.




