• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[JASS] Sorting Arrays

Status
Not open for further replies.
Level 16
Joined
Feb 22, 2006
Messages
960
Hi,

may some of you know the methods from java,c,c++ etc. to sort arrays.

example:

array[0] = 5
array[1] = 1
array[2] = 8
array[3] = 6
array[4] = 2

should be after the sort

array[0] = 1
array[1] = 2
array[2] = 5
array[3] = 6
array[4] = 8

my question is => is there a way in jass to sort arrays.
if nobody should find a solution i will create my own function but atm i have not that much time to do it.

greetings
schurke :)
 
Level 14
Joined
Nov 23, 2008
Messages
187
There is a bunch of sorting methods: insertion, "bubble", Shaker, merging, binary heap, Charles Hoare (quick sort), etc.

Some methods would be rather slower in jass due to its specifics.

Also, method speed will depend on array size and structure (for example, quick sort method is effective, when array size is quite big and naturally unsorted, while merging sort is good for small arrays).

If you have no time, just use bubble sort, and it will be enough, I think:

JASS:
globals
  integer array A
endglobals

function BubbleSort takes integer count returns nothing
  local integer i = 0
  local integer j = 0
  local integer k = 0
  if count > 8190 then
    return
  endif

  loop
    set j = 0

    loop
      if A[j] > A[j+1] then
        set k      = A[j]
        set A[j]   = A[j+1]
        set A[j+1] = k
      endif
      set j = j + 1
      exitwhen j >= count - i
    endloop

    set i = i + 1
    exitwhen i >= count
  endloop
endfunction

EDIT: zomg! I've found my binary search function! :-O My first function written at Jass, by the way :)
 
Level 23
Joined
Nov 29, 2006
Messages
2,482
No well, you would have to use globals for it, as functions cant take arrays as parameters.
However you could split up the global array easily making it dynamical, using specific start and end indexes for the "subarrays". Anyhow, a faster sort function than BubbleSort is SelectionSort but it is not the fastest one (binary is alot faster for instance).

This is how it could look (I made a small print function as well just to test when I did it):
JASS:
globals
    integer array iarr
endglobals

function SelectionSort takes integer lowIndex, integer endIndex returns nothing
    local integer i          = lowIndex
    local integer j          
    local integer tmpVal     = 0
    local integer tmpIndex
    
    if endIndex > 8190 then
        return
    endif
    
    loop
        exitwhen i > endIndex-1
        set j = i
        set tmpIndex = i
        loop
            exitwhen j > endIndex-1
            if iarr[tmpIndex] > iarr[j+1] then
                set tmpIndex = j+1
            endif
            set j = j+1
        endloop
        set tmpVal = iarr[i]
        set iarr[i] = iarr[tmpIndex]
        set iarr[tmpIndex] = tmpVal
        set i = i+1
    endloop
endfunction

function PrintArray takes integer lowIndex, integer endIndex returns nothing
    local integer i = lowIndex
    loop
        exitwhen i > endIndex
        call BJDebugMsg(I2S(iarr[i]))
        set i = i+1
    endloop
endfunction

//===========================================================================
function InitTrig_SelectionSort takes nothing returns nothing
    local integer i = 0
    loop
        exitwhen i >= 10
        set iarr[i] = GetRandomInt(0,10)
        set i = i+1
    endloop
    call PrintArray(0,9)
    call SelectionSort(0,9)
    call BJDebugMsg("---")
    call PrintArray(0,9)
endfunction
 
Status
Not open for further replies.
Top