Select Sort - Sorting Algarithims HELP
Author |
Message |
Tyler0477
|
Posted: Sat Nov 10, 2007 1:19 pm Post subject: Select Sort - Sorting Algarithims HELP |
|
|
I have an assignment in my computer class dealing with sorting algarithims and I need some help with my code.
I decided to do a SelectSort but cannot figure out the problem, "j has not been declared", I am kinda new to this so help would be much appreciated.
Here is the code (I dont really know how to explain it but basically it is supposed to sort bars according to size, which is random, thanks for your help!):
%Selection Sort
proc selectsort
for i: 1..99
var min1: int
var j: int
%Find the smallest element in the unsorted list
j: i + 1 .. 99
if bars(j).length1 < bars(min1).length1 then
min1:= j
end if
end for
%Swap the smallest unsorted element into the end of the sorted list
var temp := bars (j)
bars (j) := bars (j + 1)
bars (j + 1) := temp
drawbars
end selectsort |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Tyler0477
|
Posted: Sat Nov 10, 2007 1:42 pm Post subject: Re: Select Sort - Sorting Algarithims HELP |
|
|
If you would like some more of my code (we had to choose 3 sorting algarithims) here it is...
%Setup the screen for graphics
setscreen ("graphics:256")
setscreen ("offscreenonly")
%Declare our record type
type data :
record
length1 : int
width : int
color1 : int
end record
var bars : array 1 .. 100 of data
%Once we've declared our variables we need to initialize them
for i : 1 .. 100
bars (i).length1 := Rand.Int (5, 400)
bars (i).width := 4
bars (i).color1 := Rand.Int (1, 50)
end for
%Draws the bars
procedure drawbars
cls
for i : 1 .. 100
drawfillbox (i * 4, 0, i * 4 + bars (i).width, bars (i).length1, bars (i).color1)
end for
View.Update
end drawbars
%Bubblesort
proc bubblesort
var timeRunning : int
for i : 1 .. 99
for j : 1 .. 100 - i
if bars (j).length1 > bars (j + 1).length1 then
var temp := bars (j)
bars (j) := bars (j + 1)
bars (j + 1) := temp
end if
end for
drawbars
end for
timeRunning := Time.Elapsed
put "This process has run ", timeRunning, " milliseconds"
end bubblesort
%Shell Sort
proc shellsort
var h : int
h := 1
%Finds largest h value possible
loop
exit when h * 3 + 1 > 100
h := 3 * h + 1
end loop
put h
View.Update
loop
exit when h < 1
%for each set of elements (there are h sets)
for i : h -+ 1 .. 99
%pick the last element in the set
var B : int := bars (i).length1
var j : int
j := i
%Compare the element at B to the one before it in the set, if they are out of order continue this loop,
%moving elements "back" to make room for B inserted
loop
exit when j < h or bars (j - h).length1 <= B
bars (j) := bars (j - h)
j := j - h
end loop
bars (j).length1 := B
drawbars
%Alls sets h-sorted, now decrease set size
h := h div 3
end for
end loop
end shellsort |
|
|
|
|
|
Tony
|
Posted: Sat Nov 10, 2007 3:14 pm Post subject: RE:Select Sort - Sorting Algarithims HELP |
|
|
That's a lot of code. Please use [ code ] tags, and specify the line of code where the error occurs.
Does not look like a valid line. Did you mean for j:? |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
Tyler0477
|
Posted: Sat Nov 10, 2007 10:19 pm Post subject: Re: Select Sort - Sorting Algarithims HELP |
|
|
Sorry, Im kinda new to this site...
The errors occur on: (btw i have tried the for j: i + 1..99, but it still doesnt work)
for j: i + 1 .. 99
var temp := bars (j)
The code may seem a little confusing, our teacher wanted us to convert sorting algarithims from Java into Turing so its a little tough. My teacher couldn't even firgure out how to get the shell sort to work.
Thanks for your help,
Tyler
If you need any more code or have any more questions just post. Thanks. |
|
|
|
|
|
Tony
|
Posted: Sat Nov 10, 2007 10:24 pm Post subject: RE:Select Sort - Sorting Algarithims HELP |
|
|
well considering that Java and Turing are different languages, it might be less confusing to writing the code from scratch, instead of translating existing code... besides, this translation contributes nothing to learning.
What's the problem on that line (that is, what is the error message from the debugger?)
It might have to be
code: |
for j: (i + 1) .. 99
|
And you're still not using [ code ] tags! |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
Tyler0477
|
Posted: Sun Nov 11, 2007 12:51 am Post subject: Re: Select Sort - Sorting Algarithims HELP |
|
|
Sorry about the code tags... I have never used this website before. I tried what you suggested but it still doesn't work..I'll have to talk to my teacher on monday.
Thanks for your help.
Tyler |
|
|
|
|
|
|
|