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
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
Re: avoiding too much recursion in quicksort
I have 30,000 data to be sorted which algorithm is best for sorting.
Re: avoiding too much recursion in quicksort
I'd look into implementing an Introspective sort..
chem
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.??
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.
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