
-----------------------------------
Silent Avenger
Sat Dec 02, 2006 5:46 pm

I have a problem with my Quick sort code.
-----------------------------------
Okay I'm making sort code for my program, it works fine and all except when the array is already in order. I can't figure out why it keeps giving me the "out of stack space" error. Any help would be great.


Public Sub QSort(first As Integer, last As Integer, maindata() As Variant)
Dim pivot As Variant
'Sort Elements First to Last of maindata
If first < last Then
    
    'assign low and high
    Low = first
    High = last + 1
    
    'loop begins by reducing high
    pivot = maindata(Low)   'maindata(low) may now empty
    
    Do
        Do
            High = High - 1      'reduce high
            
            If Low = High Or maindata(High) < pivot Then Exit Do
        Loop
        
        If Low = High Then Exit Do     'finished
                
 
        maindata(Low) = maindata(High)      'maindata(high) now empty
        
        Do
            Low = Low + 1    'increment low
            
            If Low = High Or maindata(Low) > pivot Then Exit Do
            
        Loop
        
        If Low = High Then Exit Do     'finished
        
        maindata(High) = maindata(Low)  'maindata(low) now empty
        
    Loop
            
    maindata(High) = pivot      'place pivot in proper position
            
    'Call QuickSort recursively to sub divide the two partitions
    'below and above the pivot.
    Call QSort(first, High - 1, maindata())
    Call QSort(High + 1, last, maindata())
End If
 
End Sub
