Difference Between Insertion Sort and Selection Sort

Computer ProgrammingProgrammingMiscellaneous

Insertion Sort

  • In Insertion Sort, the values are inserted in a list/array wherein some of the values are previous sorted.

  • It is a stable sorting algorithm.

  • The best case time complexity is O(N) (when the list has been sorted in ascending order).

  • A key value is defined, and the array is iterated from the beginning to the end.

  • The current element during iteration is compared to the key value.

  • The number of comparison operations made during insertion sort is less than the number of the element swapping that is done.

  • If the key element is smaller than the element with which it is being compared, the previous elements are compared with it.

  • The elements greater than key are moved one position up to make space for the element that is swapped.

  • The elements are known before hand, only their position is determined during insertion sort.

Selection sort

  • In Selectin Sort, at first, the minimum or the maximum number from the list is obtained.

  • The list is sorted in ascending or descending order.

  • It is considered as an unstable sorting algorithm.

  • The time complexity in all cases is O(n squared).

  • It is less efficient in comparison to insertion sort.

  • The number of comparisons made during iterations is more than the element swapping that is done.

  • The location of every element in the list is previously known.

  • This means the user only searches for the element that needs to be inserted at the specific position.

raja
Published on 23-Mar-2021 07:02:27
Advertisements