What is the Difference Between Insertion Sort and Selection Sort?

🆚 Go to Comparative Table 🆚

Insertion sort and selection sort are both sorting algorithms with different approaches to organizing data. Here are the main differences between them:

  1. Selection Criteria:
  • Insertion sort picks an element from the unsorted part and shifts the larger elements one position to the right, inserting the selected element into the sorted part.
  • Selection sort finds the minimum or maximum element in the unsorted part and swaps it with the last element in the sorted part, expanding the sorted part by one element.
  1. Stability:
  • Insertion sort is a stable sorting algorithm, meaning that elements with the same value are preserved in their original order.
  • Selection sort is an unstable sorting algorithm, meaning that elements with the same value may be reordered.
  1. Time Complexity:
  • Insertion sort has a time complexity of O(n^2) in the worst case but can perform better on partially sorted arrays, with a best-case time complexity of O(n).
  • Selection sort has a time complexity of O(n^2) in all cases.
  1. Swaps:
  • Insertion sort requires fewer swaps than selection sort.
  • Selection sort requires more swaps than insertion sort.

In summary, insertion sort is generally more efficient than selection sort, especially when the input array is already partially sorted. Selection sort, on the other hand, always has a time complexity of O(n^2) and is an unstable sorting algorithm.

Comparative Table: Insertion Sort vs Selection Sort

Here is a table comparing the differences between insertion sort and selection sort:

Feature Insertion Sort Selection Sort
Method Inserts the value in the presorted array to sort the set of values in the array. Finds the minimum/maximum number from the list and sorts it in ascending/descending order.
Stability Stable sorting algorithm. Unstable sorting algorithm.
Time Complexity Best-case time complexity is Ω(N) when the array is already in ascending order, and Θ(N^2) in worst case and average case. Best-case, worst-case, and average time complexity is Θ(N^2).
Swaps Fewer swaps than selection sort. More swaps than insertion sort.
Comparisons More comparisons than selection sort. Fewer comparisons than insertion sort.

Insertion sort scans the sorted part of the array to find the correct position to place the element, while selection sort scans the unsorted part to find the minimum or maximum element and sorts it in ascending or descending order.