Hi all,
I am creating a macro that performs sorting. I am using quicksort algorithm. It's 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 recursions?
Please help me out.
Below is sample code::
[CODE=vb]Sub RecursiveSort(B yVal llow As Long, ByVal lHigh As Long)
Dim lStart As Long
Dim lEnd As Long
Dim vTemp As Variant
Dim vPivot As Variant
'Set new extremes to old extremes
lStart = lHigh
lEnd = llow
vPivot = a_vRowElements( (lStart + lEnd) \ 2)
'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_vRowE lements(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_vRowE lements(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[/CODE]
I am creating a macro that performs sorting. I am using quicksort algorithm. It's 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 recursions?
Please help me out.
Below is sample code::
[CODE=vb]Sub RecursiveSort(B yVal llow As Long, ByVal lHigh As Long)
Dim lStart As Long
Dim lEnd As Long
Dim vTemp As Variant
Dim vPivot As Variant
'Set new extremes to old extremes
lStart = lHigh
lEnd = llow
vPivot = a_vRowElements( (lStart + lEnd) \ 2)
'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_vRowE lements(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_vRowE lements(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[/CODE]
Comment