Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 I have a problem with my Quick sort code.
Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Silent Avenger




PostPosted: Sat Dec 02, 2006 5:46 pm   Post subject: 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.


code:
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
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: