English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Can someone please explain how to write a Count Sort or Quick Sort in VB or VBA? I need a step by step explanation with some example code.

I'm wanting to learn how to program a Count sort due to it's fast processing of large arrays.

The sort method will be used to match the values in two columns in Excel. Fast matching techniques for large arrays is what I want to focus on.

I can write a macro that matches the data up, however I want to improve the speed of my macro for large lists of data.

If there is another method that works better I'd be open to learning that. I'm not interested in learning the Bubble Sort method. It is too slow for my large lists.

2007-10-24 01:38:20 · 1 answers · asked by devilishblueyes 7 in Computers & Internet Programming & Design

Can you add some notes to the coding? It would be very helpful to know what you are using some of the different variables to refer to. Some notes above the different sections of coding explaining what that section of coding does would help too. Like for instance how the the one note in the first answer says that the two arrays are swapped. I self-taught myself VBA by reading through VB books, so this is one of the more complex aspects that I'm now learning. So understanding how each section works is what is fundamentally important to me.

2007-10-24 03:23:41 · update #1

1 answers

Try this:

Public Sub QuickSort(ByRef MyArray() As Long, ByVal lngLBound As Long, ByVal lngUBound As Long)
Dim lngPivot As Long
Dim k As Long
Dim p As Long
Dim lngTemp As Long

If lngLBound >= lngUBound Then
Exit Sub
End If

k = lngLBound + 1

If lngUBound = k Then
If MyArray(lngLBound) > MyArray(lngUBound) Then
'swap MyArray(lngLBound) and MyArray(lngUBound)
lngTemp = MyArray(lngLBound)
MyArray(lngLBound) = MyArray(lngUBound)
MyArray(lngUBound) = lngTemp
End If
Exit Sub
End If

lngPivot = MyArray(lngLBound)
p = lngUBound

Do Until (MyArray(k) > lngPivot) Or (k >= lngUBound)
k = k + 1
Loop

Do Until MyArray(p) <= lngPivot
p = p - 1
Loop

Do While k < p
'swap MyArray(k) and MyArray(p)
lngTemp = MyArray(k)
MyArray(k) = MyArray(p)
MyArray(p) = lngTemp

Do
k = k + 1
Loop Until MyArray(k) > lngPivot

Do
p = p - 1
Loop Until MyArray(p) <= lngPivot
Loop

'swap MyArray(p) and MyArray(lngLBound)
lngTemp = MyArray(p)
MyArray(p) = MyArray(lngLBound)
MyArray(lngLBound) = lngTemp

QuickSort MyArray, lngLBound, p - 1
QuickSort MyArray, p + 1, lngUBound
End Sub

2007-10-24 02:20:54 · answer #1 · answered by Anonymous · 0 0

fedest.com, questions and answers