### Ticker

20/recent/ticker-posts

# Insertion Sort Algorithm: A Simple and Efficient Sorting Technique in Python

Sorting algorithms are essential in computer science, used to organize data and optimize search times. There are many different types of sorting algorithms, each with its own strengths and weaknesses. One of the simplest and most widely used sorting algorithms is Insertion Sort. In this article, we will explore the Insertion Sort algorithm in detail, including how it works and how to implement it in Python.

#### What is Insertion Sort?

Insertion Sort is a simple sorting algorithm that works by building a sorted list one element at a time. The algorithm takes each element from the unsorted list and inserts it into the correct position in the sorted list. The algorithm is called "Insertion" sort because it inserts each element into the correct position in the sorted list.

#### How Does Insertion Sort Work?

Insertion Sort is a straightforward algorithm. Here is a step-by-step explanation of how it works:

Step 2: Take the first element from the unsorted list and insert it into the sorted list.

Step 3: Take the second element from the unsorted list and insert it into the correct position in the sorted list.

Repeat step 3 for each remaining element in the unsorted list.

Let's take an example array to see how Insertion Sort works:

[5, 3, 8, 6, 7, 2]

First iteration:

[3, 5, 8, 6, 7, 2]

Second iteration:

[3, 5, 6, 8, 7, 2]

Third iteration:

[3, 5, 6, 7, 8, 2]

Fourth iteration:

[2, 3, 5, 6, 7, 8]

The array is now sorted.

#### Implementing Insertion Sort in Python

Now that we understand how Insertion Sort works, let's see how to implement it in Python. Here is the code for Insertion Sort in Python:

``````
def insertion_sort(array):
for i in range(1, len(array)):
key = array[i]
j = i - 1
while j >= 0 and key < array[j]:
array[j+1] = array[j]
j -= 1
array[j+1] = key
``````

The function insertion_sort takes an array as input and iterates through the array using a for loop. The function then takes the current element and inserts it into the correct position in the sorted list by comparing it to the elements before it. This process is repeated until the entire array is sorted.

To use the function, simply create an array and pass it as an argument to the insertion_sort function. Here is an example usage:

``````
array = [5, 3, 8, 6, 7, 2]
insertion_sort(array)
print("Sorted array:", array)
``````

In this example, we define the insertion_sort function that takes an array as input and sorts it using the Insertion Sort algorithm. We then create an example array and pass it to the insertion_sort function. Finally, we print the sorted array using the print function.

#### Conclusion

Insertion Sort is a simple and efficient sorting algorithm that is widely used in computer science. It is easy to understand and implement, making it a great choice for small to medium-sized arrays. However,  for larger arrays, Selection Sort can be less efficient than other sorting algorithms such as Quick Sort or Merge Sort.

We hope this article has helped you understand the Insertion Sort algorithm and how to implement it.

Here is detail on each sorting algorithm in python