Sunday 14 May 2017

Insertion Sort vs. Selection Sort

Selection Sort:
Given a list, take the current element and exchange it with the smallest element on the right-hand side of the current element.

Selection Sort Pseudo Code:

Insertion Sort:
Given a list, take the current element and insert it at the appropriate position of the list, adjusting the list every time you insert. It is similar to arranging the cards in a Card game.


Insertion Sort Pseudo Code:


Time Complexity of selection sort is always n(n - 1)/2, whereas insertion sort has better time complexity as its worst case complexity is n(n - 1)/2. Generally it will take lesser or equal comparisons then n(n - 1)/2.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...