Posted: 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
Albrecd
Posted: 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
Posted: Thu Jun 21, 2007 1:33 pm Post subject: Re: Insert sort