HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Sorting algo not working =x

02-12-2009, 03:13 AM#1
Blackroot
Not sure what I'm doing wrong here, this puts the last element to every memeber of the stack for some reason. What it should do is add a new element to the stack and push the new order to the back:

Here's my logic:

start
4 3 2 1




Add Element
4 3 2 1 5




Switch first with last
5 3 2 1 4





Switch nth and nth-1
5 3 2 4 1





switch nth and nth-1
5 3 4 2 1




Done
5 4 3 2 1

However; it doesn't work like that? =o

Expand JASS:

This both adds and sorts the elements; I tried using this to avoid inverting them every time I remove an element from the stack. Just doesn't work though? =x
02-12-2009, 04:42 AM#2
Ammorth
Why not add to the back directly? Using a LinkedList would be your best bet.
02-12-2009, 04:48 AM#3
Blackroot
Actually that's a really good idea; as long as the order que is sufficiently long. However; I'd still like to know why this doesn't work.
02-12-2009, 04:52 AM#4
fX_
Collapse JASS:
library Sort initializer Demo

globals
    private constant real OUTPUT_SURVEY_TIME = 5.00

    constant integer gINT_CountValue = 5
    private integer array VALUE[gINT_CountValue]
endglobals

    //! textmacro Sort takes SORTDIRECTION, RELATION
    function $SORTDIRECTION$ takes nothing returns nothing
        local integer INT_Index = 1
        local integer INT_Index2
        local integer INT_Index2Sub
        local integer INT_Temp

        loop
            exitwhen INT_Index == gINT_CountValue
            set INT_Index2 = INT_Index
            loop
                set INT_Index2Sub = INT_Index2 - 1
                exitwhen INT_Index2 == 0 or VALUE[INT_Index2] $RELATION$= VALUE[INT_Index2Sub]
                set INT_Temp = VALUE[INT_Index2Sub]
                set VALUE[INT_Index2Sub] = VALUE[INT_Index2]
                set VALUE[INT_Index2] = INT_Temp
                set INT_Index2 = INT_Index2Sub
            endloop
            set INT_Index = INT_Index + 1
        endloop
    endfunction
    //! endtextmacro
    //! runtextmacro Sort("GreatestToLowest", "<")
    //! runtextmacro Sort("LowestToGreatest", ">")

    function Demo takes nothing returns nothing
        local integer INT_Index = 0

        call BJDebugMsg("==Greatest-to-Lowest==")
        call BJDebugMsg("INPUT:")
        loop
            exitwhen INT_Index == gINT_CountValue
            set VALUE[INT_Index] = GetRandomInt(0, 100)
            call BJDebugMsg(I2S(INT_Index + 1) + ") " + I2S(VALUE[INT_Index]))
            set INT_Index = INT_Index + 1
        endloop
        call GreatestToLowest()
        call BJDebugMsg("OUTPUT:")
        set INT_Index = 0
        loop
            exitwhen INT_Index == gINT_CountValue
            call BJDebugMsg(I2S(INT_Index + 1) + ") " + I2S(VALUE[INT_Index]))
            set INT_Index = INT_Index + 1
        endloop
        call BJDebugMsg("Lowest-to-Greatest demonstration in " + R2S(OUTPUT_SURVEY_TIME) + " seconds.")

        call TriggerSleepAction(OUTPUT_SURVEY_TIME)
        call ClearTextMessages()

        call BJDebugMsg("==Lowest-to-Greatest==")
        call BJDebugMsg("INPUT:")
        set INT_Index = 0
        loop
            exitwhen INT_Index == gINT_CountValue
            set VALUE[INT_Index] = GetRandomInt(0, 100)
            call BJDebugMsg(I2S(INT_Index + 1) + ") " + I2S(VALUE[INT_Index]))
            set INT_Index = INT_Index + 1
        endloop
        call LowestToGreatest()
        call BJDebugMsg("OUTPUT:")
        set INT_Index = 0
        loop
            exitwhen INT_Index == gINT_CountValue
            call BJDebugMsg(I2S(INT_Index + 1) + ") " + I2S(VALUE[INT_Index]))
            set INT_Index = INT_Index + 1
        endloop
    endfunction

endlibrary
02-12-2009, 05:51 AM#5
Ammorth
when norders == 1, you will switch and then switch back in the loop. Move the exitwhen to the start of the loop.
02-12-2009, 06:35 AM#6
Blackroot
Quote:
Originally Posted by Ammorth
when norders == 1, you will switch and then switch back in the loop. Move the exitwhen to the start of the loop.

Ahh I didin't notice that! Thank you; it works like clock work now!


On a side note; I actually had the wrong procedure to pull the values out - so I guess I'm just a terrible person =x.

Expand JASS:

Which was working with my old sorting algo.

thanks you guys! I'll look into LL.