Difference Between Insertion Sort and Selection Sort

Difference Between Insertion Sort and Selection Sort

6 mins readComment
Esha
Esha Gupta
Associate Senior Executive
Updated on Mar 28, 2024 18:18 IST

Have you ever wondered about the difference between Insertion Sort and Selection Sort? Insertion Sort sequentially inserts elements into a sorted subarray, whereas Selection Sort finds and places the smallest element in each iteration, generally resulting in fewer swaps. Let's understand more!

Sorting algorithms are methods used in computer science to rearrange a sequence of elements in a certain order, typically either ascending or descending. The importance of sorting lies in the fact that it makes data more understandable and easier to analyze. Sorting is also critical in many tasks, such as searching, data analysis, and algorithm optimization. In this blog, we will see the difference between selection sort and insertion sort in detail!

Table of Content

Recommended online courses

Best-suited Data Structures and Algorithms courses for you

Learn Data Structures and Algorithms with these high-rated online courses

– / –
4 months
– / –
16 weeks
Free
– / –
– / –
– / –
– / –
6 months
– / –
150 hours
– / –
4 months
– / –
8 weeks
– / –
12 weeks
4.24 K
6 weeks

Difference Between Insertion Sort and Selection Sort

Below is the table of differences between Insertion sort and selection sort.

Aspect

Insertion Sort

Selection Sort

Basic Idea

Gradually creates a sorted section at the beginning of the list, inserting each new item in its proper place in this section.

Finds the minimum (or maximum) element from the unsorted section and places it at the end of the sorted section.

Efficiency

Generally more efficient than selection sort, especially for small lists or partially sorted lists.

Less efficient, especially for large lists, as it always performs O(n) comparisons for each pass.

Best Case

O(n) (when the list is already sorted)

O(n^2), regardless of the initial order of the elements.

Worst Case

O(n^2) (when the list is sorted in reverse order)

O(n^2), regardless of the initial order of the elements.

Stability

Stable (does not change the relative order of equal elements)

Not stable by default (can be made stable with some modifications)

Auxiliary Space

O(1) (in-place)

O(1) (in-place)

Suitability

Better for datasets where the array is either small or elements are already partially sorted.

Suitable for scenarios where swapping cost is high, as it makes minimal number of swaps.

 

What is Insertion Sort?

Insertion Sort is a simple sorting algorithm commonly used for relatively small datasets. This algorithm sorts a list by building a sorted array one element at a time, picking elements from the unsorted part and placing them at the correct position in the sorted part.

The process of Insertion Sort begins with the assumption that the first element of the array (or list) is already sorted. Then, it takes the next element and scans through the sorted portion (on the left side) to find the appropriate place to insert it. This is done by comparing the selected element with each element in the sorted array, moving each of those elements one position to the right until the correct insertion spot is found. Once the appropriate position is located, the selected element is placed, and the algorithm moves on to the next unsorted element.

Time Complexity of Insertion Sort

Case

Time Complexity

Best Case

O(n)

Average Case

O(n²)

Worst Case

O(n²)

Read more about Insertion Sort Here

Embark on an enriching career in Programming with top-notch programs and online programming courses from prestigious institutions

What is the Selection Sort?

Selection Sort is a sorting algorithm used for arranging a list or array of elements in a specific order (ascending or descending). The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty, and the unsorted sublist is the entire input list.

The algorithm works by repeatedly finding the smallest (or largest, depending on the sorting order) element from the unsorted sublist and swapping it with the leftmost unsorted element, placing it in the sorted sublist. This process continues by moving the boundary of the sorted and unsorted sublists one element to the right each time. In essence, with each iteration, the Selection Sort algorithm selects the next smallest (or largest) element from the unsorted portion and moves it to its final position in the sorted portion.

Time Complexity of Selection Sort

Case

Time Complexity

Best Case

O(n²)

Average Case

O(n²)

Worst Case

O(n²)

Read more about Selection Sort Here

Conclusion

Thus, both Insertion Sort and Selection Sort are fundamental sorting algorithms with unique characteristics and best-suited scenarios, particularly in educational environments for teaching basic sorting concepts. However, their inefficiencies with large datasets often preclude them from practical use in more complex or large-scale applications.

FAQs

What is the main difference in how Insertion Sort and Selection Sort operate?

  • Insertion Sort builds the final sorted array (or list) one item at a time. It works by taking elements from the unsorted list and inserting them at the correct position in the sorted part of the list. This is akin to the way you might sort a hand of playing cards, picking cards one by one and placing them in the right order.
  • Selection Sort also builds the final sorted array one item at a time but works by repeatedly finding the minimum (or maximum) element from the unsorted section and moving it to the sorted section. This means it selects the smallest (or largest) element from the unsorted segment and swaps it with the element at the beginning of the unsorted segment.

How do Insertion Sort and Selection Sort perform on small lists?

Both Insertion Sort and Selection Sort are efficient for small data sets. Insertion Sort, in particular, is noted for its simplicity and can be faster than more complex algorithms like quicksort or mergesort when sorting small lists. Selection Sort is also effective for small lists but does not adapt to the existing order in the list, which can make it slightly less efficient than Insertion Sort in practical scenarios.

Are Insertion Sort and Selection Sort stable sorting algorithms?

  • Insertion Sort is a stable sorting algorithm because it preserves the relative order of equal elements in the sorted list. This is due to the way it inserts each new element into the sorted portion without changing the order of elements with equal keys.
  • Selection Sort, in its basic form, is not a stable sort because it can change the relative order of equal elements due to the way it selects elements and swaps them into position. However, it can be implemented in a stable manner with some modifications, but this is less common.

When would you choose Insertion Sort over Selection Sort and vice versa?

Choose Insertion Sort:

  • When the list is already partially sorted, as Insertion Sort can be more efficient in this case.
  • When stability is important, as Insertion Sort maintains the relative order of equal elements.
  • For small data sets, where its simplicity and efficiency make it a good choice.

Choose Selection Sort:

  • When memory space is limited, as Selection Sort makes fewer swaps compared to Insertion Sort.
  • When the cost of swapping items is significantly less than the cost of comparing them, as Selection Sort minimizes the number of swaps.
  • In scenarios where stability is not a concern, or when working with data where the difference in efficiency between the two algorithms is negligible.
About the Author
author-image
Esha Gupta
Associate Senior Executive

Hello, world! I'm Esha Gupta, your go-to Technical Content Developer focusing on Java, Data Structures and Algorithms, and Front End Development. Alongside these specialities, I have a zest for immersing myself in v... Read Full Bio