Results 1 to 7 of 7

Thread: avoiding too much recursion in quicksort

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    3

    avoiding too much recursion in quicksort

    hi all,

    I m creating a macro that performs sorting.I m using quicksort algoritm.Its working fine for data below 5000 but as soon as data exceeds beyond it .
    It shows Run-time error - 28 out of stack.
    Is there any way to avoid too many recursion??
    Please help me out...
    below is code sample....
    Sub RecursiveSort(ByVal llow As Long, ByVal lHigh As Long)
    Dim lStart As Long
    Dim lEnd As Long
    Dim vTemp As Variant
    Dim vPivot As Variant
    lStart = lHigh
    lEnd = llow
    vPivot = a_vRowElements(lEnd)
    'Till the count is less or equal to the max limit
    Do While lEnd <= lStart

    If bChkFlag = True Then
    ' While a_vRowElements(lEnd) < vPivot
    While Compare(a_vRowElements(lEnd), vPivot)
    lEnd = lEnd + 1
    Wend
    'While a_vRowElements(lStart) > vPivot
    While Compare(vPivot, a_vRowElements(lStart))
    lStart = lStart - 1
    Wend
    Else
    'While a_vRowElements(lEnd) > vPivot
    While Compare(vPivot, a_vRowElements(lEnd))
    lEnd = lEnd + 1
    Wend
    'While a_vRowElements(lStart) < vPivot
    While Compare(a_vRowElements(lStart), vPivot)
    lStart = lStart - 1
    Wend

    End If
    '
    If lStart >= lEnd Then
    If lStart <> lEnd Then
    vTemp = a_vRowElements(lEnd)
    a_vRowElements(lEnd) = a_vRowElements(lStart)
    a_vRowElements(lStart) = vTemp
    End If
    lStart = lStart - 1
    lEnd = lEnd + 1
    End If
    Loop
    If llow <= lStart Then
    RecursiveSort llow, lStart
    End If
    If lEnd < lHigh Then
    RecursiveSort lEnd, lHigh
    End If
    End Sub

  2. #2
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: avoiding too much recursion in quicksort

    No. Bubble sorting requires you to have a recursive function. If your data is too large.. you can't expect anything else than a stack overflow. It also depends on how unsorted the data is beforehand, if its horribly unsorted, there are more recursions.

    Why are you bubble sorting something so large anyway? Use a different algo..

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    3

    Re: avoiding too much recursion in quicksort

    I have 30,000 data to be sorted which algorithm is best for sorting.

  4. #4
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: avoiding too much recursion in quicksort

    I'd look into implementing an Introspective sort..

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    3

    Re: avoiding too much recursion in quicksort

    Thz for reply..
    If possible can you provide some psedocode of Introspective sort..

    Is it good to go for non recursive quicksort implementation.??

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: avoiding too much recursion in quicksort

    I don't know if it includes that particular sort, but this CodeBank thread (which is linked from our Classic VB FAQs) contains code for various kinds of sorting, as well as advice for which to use in different situations.

  7. #7
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: avoiding too much recursion in quicksort

    Intro sort starts with a quicksort, and switches to a heapsort when recursion-depth becomes an issue.

    I wasn't able to find an example online.. so I guess I'll write one up tonight.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width