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

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




PostPosted: Thu Jun 21, 2007 12:39 pm   Post subject: Insert sort

can anyone give me the code for a simple insert sort, i need it for my exam tommorow.
thanks
Sponsor
Sponsor
Sponsor
sponsor
Albrecd




PostPosted: Thu Jun 21, 2007 1:01 pm   Post subject: Re: Insert sort

This isn't really in any language, but you should be able to figure out the code based on it.

code:

array1 = Unsorted Data
array2 = 0 (the same number of 0s as array1 has elements)
array3 = blank array of same length as array 1

for i : the number of items in array1:
    for j : the number of items in array 1: (again because you need to compare every element to every other element)
        if array1 [i] < array1 [j]: (alphabetically or whatever)
            array2 [i] += 1 (the array gets bumped down a position)

for i : the number of items in array2: (for each possible position)
    for j : the number of items in array 1: (for each possible element)
        if array2 [j] = i: (if this element's position is next)
            array3 [i] = array1 [j] (put in in the new array)

result: array3
bergen




PostPosted: Thu Jun 21, 2007 1:33 pm   Post subject: Re: Insert sort

alright thanks..
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  [ 3 Posts ]
Jump to:   


Style:  
Search: