Saturday, May 16, 2009

Selection Sort Vs Insertion Sort

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.
insertion   sort 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

19 comments:

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

    ReplyDelete
  2. very helpful and informative

    ReplyDelete
  3. Thank you for the visual. It really helped me understand.

    ReplyDelete
  4. thanks, because i was confused , it's really helpful.

    ReplyDelete
  5. thanks, but if it has C code, it will better

    ReplyDelete
    Replies
    1. I have posted the ruby code for the same. Thanks.

      Delete
  6. So how is selection sort different from bubble sort then?

    ReplyDelete
  7. thanks,clearly explained,,very helpfull..

    ReplyDelete
  8. Thanks a lot.I hope I can Ace my test ;)

    ReplyDelete
  9. Can ı ask somethihg? İf the items are sorted, which one is faster selection or insertion

    ReplyDelete
    Replies
    1. insertion sort as mentioned in the article..

      Delete