Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Sorting confusion
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
CapnChaos




PostPosted: 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
Sponsor
sponsor
Insectoid




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: