cactus13
Automotive
- Jul 16, 2001
- 25
Hello,
I'm trying to sort a list of data and found the following code (for a Quick Sort routine) on the devx domain ( but unfortunatley it's not working in my VBA application. Can some one please help me?
At the bottom is a simple routine called Test(), I wrote just to see if it worked. The function CompMe is not included here but is included in my source.
I first want to try if it works with numbers and than see if it will also work with string which I want to use it with.
When I run the test program and trace back the error I get is a error 9:Subject is out of range. Is this due to it being source code fo VB only,not ment to be implemented in VBA?
I sincerly hop some one can solve the puzzle or ive me a clue, as I don't want to sort manually....
Jonathan
NB This is the source code below, the error line is bold and captured between commentlines.
Public Sub Quicksort(varArray As Variant, ByVal pIndex As Integer, ByVal pFirst As Long, ByVal pLast As Long)
Dim lngPivot As Long
' If the range is valid then sort
If pFirst < pLast Then
' Split the array and return the index of the item
' that is in the correct location
PartitionArray pFirst, pLast, lngPivot, varArray, pIndex
' Sort the lower portion
Quicksort varArray, pIndex, pFirst, lngPivot - 1
' Sort the upper portion
Quicksort varArray, pIndex, lngPivot + 1, pLast
End If
End Sub
Private Sub PartitionArray(ByVal pFirst As Long, ByVal pLast As Long, ByRef
pPivot As Long, varArray As Variant, ByVal pIndex As Integer)
Dim varPivot As Variant
Dim varTemp As Variant
Dim varArrTemp As Variant
Dim i As Long
Dim j As Long
Dim z As Long
' Choose the pivot as the first element in the range
'** In the following line occurs error 9 **
varPivot = varArray(pIndex, pFirst)
'** In the above line occurs error 9 **
i = pFirst
j = pLast + 1
Do
' Loop from the beginning of the range until you
' find a larger element or there are none
Do
i = i + 1
Loop Until CompMe(varArray(pIndex, i), varPivot, False, True) Or i >= pLast
' Loop from the end of the array until you
' find a smaller element or there are none
Do
j = j - 1
Loop Until CompMe(varArray(pIndex, j), varPivot, True, True) Or j <= pFirst
' If they haven't crossed then swap them
If i < j Then
ReDim varArrTemp(UBound(varArray, 1), 0)
For z = LBound(varArray, 1) To UBound(varArray, 1)
varArrTemp(z, 0) = varArray(z, i)
Next z
For z = LBound(varArray, 1) To UBound(varArray, 1)
varArray(z, i) = varArray(z, j)
Next z
For z = LBound(varArray, 1) To UBound(varArray, 1)
varArray(z, j) = varArrTemp(z, 0)
Next z
End If
Loop Until j <= i
' Swap the pivot with the split in the array
varArray(pIndex, pFirst) = varArray(pIndex, j)
varArray(pIndex, j) = varPivot
' Return the index of the element that is in the correct
' location to enable another sort of the two halves
pPivot = j
End Sub
Sub Test()
Dim Lijst(9)
For i = 0 To 9
Lijst(i) = 10 * Rnd
Next i
Quicksort Lijst, 5, 0, 9
End Sub
I'm trying to sort a list of data and found the following code (for a Quick Sort routine) on the devx domain ( but unfortunatley it's not working in my VBA application. Can some one please help me?
At the bottom is a simple routine called Test(), I wrote just to see if it worked. The function CompMe is not included here but is included in my source.
I first want to try if it works with numbers and than see if it will also work with string which I want to use it with.
When I run the test program and trace back the error I get is a error 9:Subject is out of range. Is this due to it being source code fo VB only,not ment to be implemented in VBA?
I sincerly hop some one can solve the puzzle or ive me a clue, as I don't want to sort manually....
Jonathan
NB This is the source code below, the error line is bold and captured between commentlines.
Public Sub Quicksort(varArray As Variant, ByVal pIndex As Integer, ByVal pFirst As Long, ByVal pLast As Long)
Dim lngPivot As Long
' If the range is valid then sort
If pFirst < pLast Then
' Split the array and return the index of the item
' that is in the correct location
PartitionArray pFirst, pLast, lngPivot, varArray, pIndex
' Sort the lower portion
Quicksort varArray, pIndex, pFirst, lngPivot - 1
' Sort the upper portion
Quicksort varArray, pIndex, lngPivot + 1, pLast
End If
End Sub
Private Sub PartitionArray(ByVal pFirst As Long, ByVal pLast As Long, ByRef
pPivot As Long, varArray As Variant, ByVal pIndex As Integer)
Dim varPivot As Variant
Dim varTemp As Variant
Dim varArrTemp As Variant
Dim i As Long
Dim j As Long
Dim z As Long
' Choose the pivot as the first element in the range
'** In the following line occurs error 9 **
varPivot = varArray(pIndex, pFirst)
'** In the above line occurs error 9 **
i = pFirst
j = pLast + 1
Do
' Loop from the beginning of the range until you
' find a larger element or there are none
Do
i = i + 1
Loop Until CompMe(varArray(pIndex, i), varPivot, False, True) Or i >= pLast
' Loop from the end of the array until you
' find a smaller element or there are none
Do
j = j - 1
Loop Until CompMe(varArray(pIndex, j), varPivot, True, True) Or j <= pFirst
' If they haven't crossed then swap them
If i < j Then
ReDim varArrTemp(UBound(varArray, 1), 0)
For z = LBound(varArray, 1) To UBound(varArray, 1)
varArrTemp(z, 0) = varArray(z, i)
Next z
For z = LBound(varArray, 1) To UBound(varArray, 1)
varArray(z, i) = varArray(z, j)
Next z
For z = LBound(varArray, 1) To UBound(varArray, 1)
varArray(z, j) = varArrTemp(z, 0)
Next z
End If
Loop Until j <= i
' Swap the pivot with the split in the array
varArray(pIndex, pFirst) = varArray(pIndex, j)
varArray(pIndex, j) = varPivot
' Return the index of the element that is in the correct
' location to enable another sort of the two halves
pPivot = j
End Sub
Sub Test()
Dim Lijst(9)
For i = 0 To 9
Lijst(i) = 10 * Rnd
Next i
Quicksort Lijst, 5, 0, 9
End Sub