Inserted Sort :/
Author |
Message |
Flikerator
|
Posted: Thu Nov 17, 2005 5:46 pm Post subject: Inserted Sort :/ |
|
|
code: | var test : array 1 .. 8 of int
var temp : int
for i : 1 .. 8
test (i) := Rand.Int (1, 99)
put test (i) : 2, " " ..
end for
put ""
procedure move (i : int, j : int)
for ii : i .. j
test (ii) := test (ii + 1)
end for
end move
for decreasing i : 8 .. 1
for j : i .. 8
if j + 1 > upper (test) then
elsif test (i) < test (j + 1) then
temp := test (i)
move (i, j)
end if
end for
end for
for i : 1 .. 8
put test (i) : 2, " " ..
end for |
I have a mind block. This is inserted sort :/ It goes from itself to the end of the array looking for a smaller number. and then it "inserts" itself by moving all the numbers from that number over (The number next to it is put into its place) and that number is placed in the spot where the smallest number was (its over one place remember?). I know im doing something stupid and simple wrong. Any ideas? (It has to do with the move proc) |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Paul
|
Posted: Thu Nov 17, 2005 11:36 pm Post subject: (No subject) |
|
|
are you talking about insertion sort?
if so this pseudocode may help you:
code: |
function insertsort (A : list[0..n-1])
{
var int i, j;
for i from 0 to n - 1
{
value = a[i];
j := i - 1;
while (j >= 0) and (a[j] > value)
{
a[j+1] = a[j];
j = j - 1;
}
a[j+1] = value;
}
}
|
|
|
|
|
|
|
Flikerator
|
Posted: Mon Nov 21, 2005 8:47 am Post subject: (No subject) |
|
|
Yes it was insertion, but I figured it out last week (after I sent here).
code: |
var temp, hold : int
var numbers : array 1 .. 8 of int
for i : 1 .. 8
numbers (i) := Rand.Int (1, 99)
put numbers (i) : 2, " " ..
end for
put ""
for decreasing i : 7 .. 1
temp := numbers (i)
hold := i
for j : i .. 8
if temp > numbers (j) then
numbers (j - 1) := numbers (j)
hold := j
end if
end for
numbers (hold) := temp
end for
for i : 1 .. 8
put numbers (i) : 2, " " ..
end for
|
This is the core of what I have to test and make sure it works. Now onto shell sort |
|
|
|
|
|
|
|