Sorting confusion
Author |
Message |
CapnChaos
|
Posted: Thu Sep 29, 2011 4:25 pm Post subject: Sorting confusion |
|
|
So, I'm making a sorting program for a school assignment, and I'm supposed to have a code for insertion sort, and another for selection sort.
I have a sorting code made that works fine, but I find insertion and selection sort very similar, so I'm not really sure what I've created here.
If somebody could tell me which this is, it would be helpful, as I would be able to move on to the other.
Turing: |
var numz : array 1 .. 10 of int
var temp, hold : int := 0
%Random numbers to avoid typing them in every time
for t : 1 .. 10
randint (numz (t ), 1, 10)
put numz (t )
end for
put ""
%Sort
for i : 1 .. 9
temp := numz (i )
hold := i
for j : i .. 9
if (numz (i ) > numz (j )) then
hold := j
numz (i ) := numz (j )
end if
end for
numz (hold ) := temp
end for
%Put sorted numbers
for t : 1 .. 10
put numz (t )
end for
put ""
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Insectoid
|
Posted: Thu Sep 29, 2011 5:52 pm Post subject: RE:Sorting confusion |
|
|
A selection sort will pick an item and put it in the right spot. An insertion sort will pick a spot and put the right item there.
Say we have the list 582397.
A selection sort would start with an empty output list. It will scan the list for the smallest item (2 in this case) and put it in the first spot. Then it finds the 2nd item (3) and puts it in the 2nd spot, until the list is sorted.
An insertion sort would start at the beginning (5) and compare it with the next item (8). The 8 is already sorted, so we move on to the 2, and walk it down the list past the 8 and 5 until it's in the right spot. It does the same with the 3, walking it down past the 8 and 5 but stopping before the 2. By now the list reads 235897. The 9 is already sorted so we continue with the 7 which we walk down past the 8 and 9 to sit in the right spot. |
|
|
|
|
|
CapnChaos
|
Posted: Thu Sep 29, 2011 6:33 pm Post subject: Re: RE:Sorting confusion |
|
|
Insectoid @ Thu Sep 29, 2011 5:52 pm wrote: A selection sort will pick an item and put it in the right spot. An insertion sort will pick a spot and put the right item there.
Say we have the list 582397.
A selection sort would start with an empty output list. It will scan the list for the smallest item (2 in this case) and put it in the first spot. Then it finds the 2nd item (3) and puts it in the 2nd spot, until the list is sorted.
An insertion sort would start at the beginning (5) and compare it with the next item (8). The 8 is already sorted, so we move on to the 2, and walk it down the list past the 8 and 5 until it's in the right spot. It does the same with the 3, walking it down past the 8 and 5 but stopping before the 2. By now the list reads 235897. The 9 is already sorted so we continue with the 7 which we walk down past the 8 and 9 to sit in the right spot.
This is the best I've ever had either of the two explained to me. As such, I see that the code I have shown would be Insertion sort. |
|
|
|
|
|
Insectoid
|
Posted: Thu Sep 29, 2011 10:00 pm Post subject: RE:Sorting confusion |
|
|
Often when I try to code one, I accidentally do the other. |
|
|
|
|
|
CapnChaos
|
Posted: Thu Sep 29, 2011 11:00 pm Post subject: Re: RE:Sorting confusion |
|
|
Insectoid @ Thu Sep 29, 2011 10:00 pm wrote: Often when I try to code one, I accidentally do the other.
Yeah, once you get one working, it's hard to think of other algorithms. |
|
|
|
|
|
|
|