Computer science is all about understanding the complexities of algorithms and data structures.

You have a list of items that need to be sorted, but you don’t have the time or resources to use a more complex sorting algorithm.

Insertion sorting is one of the simplest sorting algorithms, but it can be slow for large lists.

Easy implementation and understanding have made this method a favorite among programmers. It’s perfect for small lists or when you need a quick solution.

In this blog post, we will look at the time complexity of insertion sorting. This algorithm is used to sort arrays, and it has a runtime of O(n^{2}). This means that the time complexity increases with the size of the array.

However, this algorithm can be faster often than other sorting algorithms, such as quicksort.

Let’s take a closer look at how insertion sorting works!

## What Is Insertion Sort Algorithm?

One element at a time, insertion sort generates a sortable array, which is frequently termed as a list.

For example, sorting is applied in complicated computer programs such as compilers, where the order of tokens is important to the interpretation of the program.

## How Does Insertion Sort Work?

When we use insertion sort to sort an array, the algorithm starts by finding the smallest item in the list and inserting it into the correct position.

It then finds the next smallest item and inserts it into the correct position, and so on.

The algorithm works by looping through the list, comparing each item to the one that comes before it.

If the items are in the wrong order, the algorithm swaps them. It then checks to see if the list is sorted, and if it is, the algorithm ends.

In practice, insertion sort is often implemented using a few lines of code, making it a popular choice for sorting small arrays. However, time complexity should be considered when using this algorithm.

## Example:

Here is an example of how insertion sorting works. We will use the following array:

1, 2, 3, 4, 5, 6

The algorithm starts by finding the smallest item in the list, which is 1. It then inserts it into the correct position, the first position. It then finds the next smallest item, which is 2. It inserts it into the correct position, which is the second position.

It then finds the next smallest item, which is 3. It inserts it into the correct position, which is the third position.

It then finds the next smallest item, which is 4. It inserts it into the correct position, which is the fourth position, and so on. The list is now sorted!

We can see from the example that the algorithm takes six comparisons and swaps to sort the list. This is because it takes n^{2} comparisons and swaps to sort a list of n items. In this case, n=6.

## How to Improve Insertion Sort Time Complexity?

While insertion sort has a runtime of O(n^{2}), it can be improved by using a better sorting algorithm, such as quicksort.

Quicksort has an O(n log n) runtime, which is much faster than O(n^{2}).

However, in some cases, insertion sorting can be faster than quicksort.

For example, if the list is already in order, insertion sorting will take less time than quicksort.

In practice, insertion sort is often implemented using a few lines of code, making it a popular choice for sorting small arrays.

However, time complexity should be considered when using this algorithm.

## Time Complexities

### Worst Case Complexity O(n^{2}):

The time complexity increases with the size of the array. It takes n^{2} comparisons and swaps to sort a list of n items.

For example, if we have an array of size 1000, the algorithm will take 1,000,000 comparisons and swaps to sort the array.

### Best Case Complexity O(n):

The time complexity is the same as the size of the input array. I

t takes n comparisons and swaps to sort a list of n items. For example, consider an array of size 5. The algorithm will take five comparisons and swaps to sort the array.

### Average Case Complexity O(n^{2}):

The time complexity is between the worst and best case complexities in this case.

It takes n^{2} comparisons and swaps to sort a list of n items.

Thus, insertion sorting is a stable sorting algorithm.

## Why Is Insertion Sort Stable?

Insertion sort is stable because it preserves the order of equal elements in the input array.

This is important for many applications, such as data retrieval or financial analysis. For example, if we have two lists of numbers and want to compare them, we need to make sure that the order of the elements is preserved.

If the lists are not sorted, we will not compare them accurately.

## Leave a Reply