Algorithm - Insertion Sort Vs. Selection Sort - Stack Overflow

Total Page:16

File Type:pdf, Size:1020Kb

Algorithm - Insertion Sort Vs. Selection Sort - Stack Overflow

× programmers. It's 100% free, no registration required. Insertion Sort vs. Selection Sort I am trying to understand the differences between Insertion Sort and Selection Sort.

They both seem to have two components: an unsorted list and a sorted list. They both seem to take one element from the unsorted list and put it into the sorted list at the proper place. I have seen some sites/books saying that selection sort does this by swapping one at a time while insertion sort simply finds the right spot and inserts it. However, I have seen other articles say something, saying that insertion sort also swaps. Consequently, I am confused. Is there any canonical source? algorithm insertion-sort share|improve this question asked Apr 3 '13 at 22:04

eb80 13718 1 The wikipedia for selection sort comes with pseudo code and pretty illustrations, as does the one for insertion sort. – G. Bach Apr 3 '13 at 22:05 @G.Bach

-- thanks for this... I have read both pages multiple times but don't understand the difference-- hence this question. – eb80 Apr 4 '13 at 3:53 add comment 3 Answers active oldest votes It's possible that the confusion is because you're comparing a description of sorting a linked list with a description of sorting an sources. The easiest way to understand sorting algorithms is often to get a detailed description of the algorithm (not vague stuff like "this sort uses swap. Somewhere. I'm not saying where"), get some playing cards (5-10 should be enough for simple sort algorithms), and run the algorithm by hand.

Selection sort: scan through the unsorted data looking for the smallest remaining element, then swap it into the position immediately following the sorted data. Repeat until finished. If sorting a list, you don't need to swap the smallest element into position, you could instead remove the list node from its old position and insert it at the new.

Insertion sort: take the element immediately following the sorted data, scan through the sorted data to find the place to put it, and put it there. Repeat until finished.

Insertion sort can use swap during its "scan" phase, but doesn't have to and it's not the most efficient way unless you are sorting an array of a data type which: (a) cannot be moved, only copied or swapped; and (b) is more expensive to copy than to swap. If insertion sort does use swap, the way it works is that you simultaneously search for the place element there, by repeatedly swapping the new element with the element immediately before it, for as long as the element before it is bigger than it. Once you reach an element that isn't bigger, you've found the correct location and you move on to the next new element.

Both insertion sort and selection sort have an outer Selection Sort: Given a list, take the current element loop (over every index), and an inner loop (over a and exchange it with the smallest element on the right subset of indices). Each pass of the inner loop hand side of the current element. expands the sorted region by one element, at the expense of the unsorted region, until it runs out of Insertion Sort: Given a list, take the current element unsorted elements. and insert it at the appropriate position of the list, adjusting the list every time you insert. It is similar to The difference is in what the inner loop does: arranging the cards in a Card game.

 In selection sort, the inner loop is over the Time Complexity of selection sort is always n(n - 1)/2, unsorted elements. Each pass selects one whereas insertion sort has better time complexity as element, and moves it to its final location (at the its worst case complexity is n(n - 1)/2. Generally it will current end of the sorted region). take lesser or equal comparisons then n(n - 1)/2.  In insertion sort, each pass of the inner loop iterates over the sorted elements. Sorted Source: elements are displaced until the loop finds the http://cheetahonfire.blogspot.com/2009/05/selection- correct place to insert the next unsorted element. sort-vs-insertion-sort.html So, in a selection sort, sorted elements are found in order, and stay put once they are found. Conversely, share|improve this answer editedanswered Apr 3 Apr 3 '13 in an insertion sort, the unsorted elements stay put '13 at at 22:07 until consumed in input order, while elements of the 22:13 sorted region keep getting moved around. As far as swapping is concerned: selection sort does Nikolay Kostov one swap per pass of the inner loop. Insertion sort 995213 typically saves the element to be inserted as temp before the inner loop, leaving room for the inner loop to shift sorted elements up by one, then copies temp to the insertion point afterwards. share|improve this answer editedanswered Apr 3 Apr 3 '13 at '13 at 22:42 23:08

comingstorm 10.8k724

I think linking the source of this post would be nice. – G. Bach Apr 3 '13 at 22:13

My mistake. I will edit my answer :) – Nikolay Kostov Apr 3 '13 at 22:13

@Nikolay - This is just copied from cheetahonfire.blogspot.com/2009/05/… which I

already read. Is there anything more concerte as I have read conflicting articles. – eb80 Apr 3 '13 at 22:13 The main difference is the selection step. The selection sort selects the smallest one and swaps it

with the first one The insertion sort inserts the current one in its appropriate position – Nikolay Kostov Apr 3 '13 at 22:14 @eb80 I'm not sure what material you're reading, but I don't see how an example could be more concrete

than this? – G. Bach Apr 3 '13 at 22:15 add comment add comment

add comment up vote 2 down vote 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.

Source: http://cheetahonfire.blogspot.com/2009/05/selection- sort-vs-insertion-sort.html

share|improve this answer editedanswered Apr 3 Apr 3 '13 '13 at at 22:07 22:13

Nikolay Kostov 995213

I think linking the source of this post would be nice. – G. Bach Apr 3 '13 at 22:13

My mistake. I will edit my answer :) – Nikolay Kostov Apr 3 '13 at 22:13

@Nikolay - This is just copied from cheetahonfire.blogspot.com/2009/05/… which I

already read. Is there anything more concerte as I have read conflicting articles. – eb80 Apr 3 '13 at 22:13 The main difference is the selection step. The selection sort selects the smallest one and swaps it

with the first one The insertion sort inserts the current one in its appropriate position – Nikolay Kostov Apr 3 '13 at 22:14 @eb80 I'm not sure what material you're reading, but I don't see how an example could be more

concrete than this? – G. Bach Apr 3 '13 at 22:15 add comment

Your Answer

Sign up or log in Sign up using Google Sign up using Facebook Sign up using Stack Exchange Post as a guest

Name

Email Post as a guest

Name

Email

Post Your Answ er discard By posting your answer, you agree to the privacy policy and terms of service. Not the answer you're looking for? Browse other questions tagged algorithm insertion-sort or ask your own question. asked 11 months ago viewed 8390 times active 11 months ago Community Bulletin event Burn the close review queue – until crispy

Related

4 Is this equivalent to insertion sort?

1 Can't get insertion sort from introduction to algorithms 3rd ed. right. Where is my thinking mistake?

1 Record elements compared in sorting algorithm in java

5 quicksort and insertion sort hybrid expected running time

7 An efficient sorting algorithm for almost sorted list containing time data?

1 Big theta notation of insertion sort algorithm

0 Insertion sort - Descending order

-1 Insertion sort Proof by Induction

1 Choosing minimum length k of array for merge sort where use of insertion sort to sort the subarrays is more optimal than standard merge sort

0 Running time of Selection Sort vs Insertion Sort Hot Network Questions . Were birds used for communication historically? . "Coat" vs. "jacket" in AE . Precalc - Prove Trig Identity . Do US citizens need a visa for transit in Frankfurt? . Cross-matching regular expressions . Adequate representation of frequency domain amplitude/magnitude of FFT of a signal . Are there rules in D&D 3.5 for characters who smoke? . How is this problem related to the study of algorithms and big O notation? . Can the Delorean meet its speed requirement without moving? . Composition and Onto Functions . Enabling GPU rendering for Cycles? . When you use 在 with a time, where does 在 go in the sentence? . Can I submit published articles to arXiv as well? . Why did the Capitol's highly civilized people never vote down the Hunger Games? . How to view CD key for a Steam game? . Is photography from a satellite good enough to make out a person on the ground? . Tridion Logging in DEBUG mode- Unable to turn off . Why do breathtaking views turn into "boring" photos, and how can I do better? . Could you help me translate this sentence? . How to intentionally get denied entry to the US, without getting into trouble? . Does "to cook leeks" imply anything about the method of preparing them? . Removing everything after a certain character . Minimize norm of a polynomial around a circle (count the solutions) . How can I type formula cosine of two vectors nice? more hot questions question feed

about help badges blog chat data legal privacy policy jobs advertising info mobile contact us

feedback Culture / Technology Life / Arts Recreation Science Other 1. Engli 1. Progra sh mmers Languag 2. Unix & e & Linux Usage 3. Ask 2. Skep Different tics (Apple) 1. Photo 3. Mi 1. S 1. Stack 4. WordP graphy Yodeya tack Overflow ress 2. Scienc (Judais App 2. Server Answers e Fiction m) s Fault 5. Geogr 1. Datab & 4. Trav 1. Mathe 2. M 3. Super aphic ase Fantasy el matics eta User Informatio Administr 3. Seaso 5. Chris 2. Cross Stac 4. Web n Systems ators ned tianity Validated k Applicatio 6. Electric 2. Drupal Advice 6. Arqa (stats) Ove ns al Answers (cooking) de 3. Theore rflow 5. Ask Engineeri 3. Share 4. Home (gaming tical 3. A Ubuntu ng Point Improve ) Computer rea 6. Webm 7. Androi 4. User ment 7. Bicyc Science 51 asters d Experienc 5. Perso les 4. Physic 4. S 7. Game Enthusiast e nal 8. Role- s tack Develop s 5. Mathe Finance playing 5. MathO Ove ment 8. Inform matica & Money Games verflow rflow 8. TeX - ation 6. more 6. more 9. more 6. more Car LaTeX Security (14) (12) (21) (7) eers site design / logo © 2014 stack exchange inc; user contributions licensed under cc by-sa 3.0 with attribution required rev 2014.3.3.1414 Stack Overflow works best with JavaScript enabled

Recommended publications