# Insertion Sort with examples in Python

39
0

Insertion sort is a simple sorting algorithm that works by iterating through the list and inserting each element into its correct position in the sorted portion of the list. It has a time complexity of O(n^2) in the worst case, which makes it relatively slow for large lists. However, it is easy to understand and implement, which makes it a good choice for small lists or as a learning exercise.

Here is an example of how to implement insertion sort in Python:

``````def insertion_sort(arr):
# Iterate over the list, starting at the second element
for i in range(1, len(arr)):
# Save the current element
current = arr[i]

# Find the correct position for the current element
j = i - 1
while j >= 0 and arr[j] > current:
arr[j + 1] = arr[j]
j -= 1

# Insert the current element into the correct position
arr[j + 1] = current

# Return the sorted list
return arr``````

This implementation of insertion sort has a time complexity of O(n^2) in the worst case, which means that it will take longer to sort large lists.

To use this function, you can simply pass in the list of elements you want to sort as an argument, like this:

``````sorted_list = insertion_sort([5, 2, 4, 1, 3])
print(sorted_list)  # Output: [1, 2, 3, 4, 5]``````

Alternatively, you can use the built-in `sorted()` function, which uses a variation of insertion sort to sort the elements in a list:

``````sorted_list = sorted([5, 2, 4, 1, 3])
print(sorted_list)  # Output: [1, 2, 3, 4, 5]``````

It has a number of limitations, including:

1. Time complexity: Insertion sort has a time complexity of O(n^2) in the worst case, which means that it becomes increasingly slow as the size of the list increases. This makes it less efficient than other sorting algorithms for large lists.
2. Space complexity: Insertion sort has a space complexity of O(1), which means that it does not require additional memory to perform the sort. However, it does require multiple passes through the list, which can use up additional time and resources.
3. Adaptability: Insertion sort is more adaptable to different data patterns than some other sorting algorithms, and it performs well on lists that are already partially sorted or lists with many duplicate elements. However, it still has a time complexity of O(n^2) in the worst case, which means that it can be slow for large lists.
4. Stability: Insertion sort is a stable sorting algorithm, which means that it preserve the relative order of elements with equal values. However, this also means that it requires additional comparisons and swaps to maintain the relative order, which can increase the time complexity.

Here are a few examples of where insertion sort might be used in real life:

1. Sorting a small list of personal contacts: If you have a small list of personal contacts (e.g. a few hundred names), you might use insertion sort to sort the list alphabetically by last name. This would allow you to quickly find a specific contact or to browse through the list in a consistent order.
2. Sorting a list of grades: If you are a teacher and you want to sort a list of grades for a class, you might use insertion sort to put the grades in ascending order. This would allow you to easily see how each student is performing and to identify any students who may need additional help.
3. Sorting a list of tasks: If you are using a to-do list to track your tasks, you might use insertion sort to sort the tasks by priority or by due date. This would allow you to focus on the most important tasks first and to make sure that you are completing your tasks on time.
4. Sorting a list of recipes: If you are a cook and you have a list of recipes that you want to organize, you might use insertion sort to sort the recipes by category (e.g. appetizers, main dishes, desserts). This would allow you to quickly find a specific recipe or to browse through the list to find inspiration for your next meal.

Overall, insertion sort is a simple and easy-to-understand sorting algorithm, but it is generally not the most efficient choice for large lists or lists with complex data patterns. There are many other sorting algorithms that are better suited for these types of lists.