|
|
|
Click here to copy the following block |
Sub QuickSort(arr As Variant, Optional numEls As Variant, _ Optional descending As Boolean)
Dim value As Variant, temp As Variant Dim sp As Integer Dim leftStk(32) As Long, rightStk(32) As Long Dim leftNdx As Long, rightNdx As Long Dim i As Long, j As Long
If IsMissing(numEls) Then numEls = UBound(arr) leftNdx = LBound(arr) rightNdx = numEls sp = 1 leftStk(sp) = leftNdx rightStk(sp) = rightNdx
Do If rightNdx > leftNdx Then value = arr(rightNdx) i = leftNdx - 1 j = rightNdx If descending Then Do Do: i = i + 1: Loop Until arr(i) <= value Do: j = j - 1: Loop Until j = leftNdx Or arr(j) >= value temp = arr(i) arr(i) = arr(j) arr(j) = temp Loop Until j <= i Else Do Do: i = i + 1: Loop Until arr(i) >= value Do: j = j - 1: Loop Until j = leftNdx Or arr(j) <= value temp = arr(i) arr(i) = arr(j) arr(j) = temp Loop Until j <= i End If temp = arr(j) arr(j) = arr(i) arr(i) = arr(rightNdx) arr(rightNdx) = temp sp = sp + 1 If (i - leftNdx) > (rightNdx - i) Then leftStk(sp) = leftNdx rightStk(sp) = i - 1 leftNdx = i + 1 Else leftStk(sp) = i + 1 rightStk(sp) = rightNdx rightNdx = i - 1 End If Else leftNdx = leftStk(sp) rightNdx = rightStk(sp) sp = sp - 1 If sp = 0 Then Exit Do End If Loop End Sub |
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|