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.

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.

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.

Any suggestion to improve this article is always welcome!

Below you can find the Ruby code snippets:-

Selection Sort

Insertion Sort

Thank you

ReplyDeleteThanks a lot! Very helpful and clearly. Helped me to understand better, how InsertionSort and SelectionSort work.

ReplyDeletevery helpful and informative

ReplyDeleteThank you for the visual. It really helped me understand.

ReplyDeleteGreat job, thank you

ReplyDeletethanks, because i was confused , it's really helpful.

ReplyDeletethanks, but if it has C code, it will better

ReplyDeleteI have posted the ruby code for the same. Thanks.

DeleteSo how is selection sort different from bubble sort then?

ReplyDeletethanks,clearly explained,,very helpfull..

ReplyDeleteThanks was really helpful

ReplyDeletevery helpful thank you

ReplyDeleteThank you!!

ReplyDeleteThanks a lot.I hope I can Ace my test ;)

ReplyDeleteThank you so much!!

ReplyDeletety

ReplyDeleteThank you

ReplyDeleteThank you so much

ReplyDeleteCan ı ask somethihg? İf the items are sorted, which one is faster selection or insertion

ReplyDeleteinsertion sort as mentioned in the article..

Delete