
-----------------------------------
Tyler0477
Sat Nov 10, 2007 1:19 pm

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

-----------------------------------
Tyler0477
Sat Nov 10, 2007 1:42 pm

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 