recursive functions?

Everything else that doesn't fall into one of the other PB categories.
Dreglor
Enthusiast
Enthusiast
Posts: 759
Joined: Sat Aug 02, 2003 11:22 pm
Location: OR, USA

recursive functions?

Post by Dreglor »

i been devloping my 3d engein and i hit a snag (acually second snag)
i doing z sorting and i was woring on a sorting function for it, i need a costum sorting function because ineed to keep track of which faces belong to what object and vicea versa, so i look up some sorting a algorithms and found quick sort.
the only problem is, is that it is recursive (massivly so).
i try to get from every angle and it didn't work any way, gosub and goto just give me erros and procedures would even allow me to call procedure with in a procedure
heres the code to the quick sort test

Code: Select all

Dim sortmeO(100)
Dim sortmeF(100)
OpenConsole()
PrintN("sorting...")
tot=100
For i=0 To 100
  sortmeO(i)=Random(500)
  sortmeF(i)=Random(500)
Next i
left=0
right=tot-1
qsort:
  pivot=sortmeF(left)
  l_hold=left
  r_hold=right
  While left < right
    While sortmeF(right) >= pivot And left < right
      right=right-1
    Wend
    If left = right
      sortmeF(left) = sortmeF(right)
      right=right+1
    EndIf
    While sortmeF(left) <= pivot And left < right
      left=left+1
    Wend
    If left <> right
      sortmeF(right) = sortmeF(left)
      right=right-1
    EndIf
  Wend
  sortmeF(left)=pivot
  pivot=left
  left=l_hold
  right=r_hold
  If left < pivot
    right=pivot-1
    FakeReturn
    Goto qsort
  EndIf
  If right > pivot
    left=pivot+1
    FakeReturn
    Goto qsort
  EndIf
Return
as you can see i stopped after goto's
is there some work around that i didn't see?
asm?
or is it just impossible and another "wish" for the next version
~Dreglor
sec
Enthusiast
Enthusiast
Posts: 792
Joined: Sat Aug 09, 2003 3:13 am
Location: 90-61-92 // EU or ASIA
Contact:

Post by sec »

try:

Code: Select all

Dim SortmeF(100) 

OpenConsole() 
PrintN("before sorting...") 
For i=0 To 10 
   SortmeF(i)=Random(500) 
   Print(Str(SortmeF(i))+" ")
Next i 
PrintN("")
PrintN("after sorting...") 

l = 0
r = 10

qsort:
    i = l
    j = r
    pivot = SortmeF(l)


    While (i<=j)
        While SortmeF(i)<pivot: i+1: Wend
        While SortmeF(j)>pivot: j-1: Wend

        If (i<=j)
            temp=SortmeF(i) : SortmeF(i)=SortmeF(j) : SortmeF(j)=temp
            i+1
            j-1
        EndIf
    Wend
    If (l<j) : r=j : Goto qsort : EndIf
    If (i<r) : l=i : Goto qsort : EndIf


For i=0 To 10
   Print(Str(SortmeF(i))+" ")
Next i
Input()
CloseConsole()
Dreglor
Enthusiast
Enthusiast
Posts: 759
Joined: Sat Aug 02, 2003 11:22 pm
Location: OR, USA

Post by Dreglor »

thanks for the help, sec.
so i guess not haveing a fakereturn helps when having recursive functions ;)
~Dreglor
sec
Enthusiast
Enthusiast
Posts: 792
Joined: Sat Aug 09, 2003 3:13 am
Location: 90-61-92 // EU or ASIA
Contact:

Post by sec »

above proc is non-Recursive!
why don't we continue viewtopic.php?t=7503 :D
Post Reply