-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmQuickSort.bas
132 lines (113 loc) · 3.89 KB
/
mQuickSort.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
Attribute VB_Name = "mQuickSort"
Option Explicit
'http://www.vbforums.com/showthread.php?231925-VB-Quick-Sort-algorithm-(very-fast-sorting-algorithm)&p=4739885&viewfull=1#post4739885
Public SORTSWAPS As Long
Attribute SORTSWAPS.VB_VarUserMemId = 1073938433
Public Sub QuicksortSingle(List() As Single, ByVal Min As Long, ByVal Max As Long)
Attribute QuicksortSingle.VB_UserMemId = 1073938434
' from Low to hi
Dim med_value As Single
Dim hi As Long
Dim lo As Long
Dim I As Long
If Max <= Min Then Exit Sub
'I = Int((max - min + 1) * Rnd + min)
I = (Max + Min) \ 2
med_value = List(I)
List(I) = List(Min)
lo = Min
hi = Max
Do
Do While List(hi) >= med_value
hi = hi - 1&
If hi <= lo Then Exit Do
Loop
If hi <= lo Then
List(lo) = med_value
Exit Do
End If
List(lo) = List(hi)
lo = lo + 1
Do While List(lo) < med_value
lo = lo + 1&
If lo >= hi Then Exit Do
Loop
If lo >= hi Then
lo = hi
List(hi) = med_value
Exit Do
End If
' Swap the lo and hi values.
List(hi) = List(lo)
Loop
QuicksortSingle List(), Min, lo - 1&
QuicksortSingle List(), lo + 1&, Max
End Sub
Public Sub QuickSortSingle2(Dist() As Single, OtherInfo() As Long, ByVal Min As Long, ByVal Max As Long)
Attribute QuickSortSingle2.VB_UserMemId = 1073741850
Dim med_value As Single
Dim med_OtherInfo As Long
Dim hi As Long
Dim lo As Long
Dim I As Long
If Max <= Min Then Exit Sub
' I = Int((max - min + 1) * Rnd + min)
I = (Max + Min) \ 2
med_value = Dist(I)
med_OtherInfo = OtherInfo(I)
Dist(I) = Dist(Min)
OtherInfo(I) = OtherInfo(Min)
lo = Min
hi = Max
Do
Do While Dist(hi) >= med_value
hi = hi - 1&
If hi <= lo Then Exit Do
Loop
If hi <= lo Then
Dist(lo) = med_value
OtherInfo(lo) = med_OtherInfo
Exit Do
End If
Dist(lo) = Dist(hi)
OtherInfo(lo) = OtherInfo(hi)
lo = lo + 1&
Do While Dist(lo) < med_value
lo = lo + 1&
If lo >= hi Then Exit Do
Loop
If lo >= hi Then
lo = hi
Dist(hi) = med_value
OtherInfo(hi) = med_OtherInfo
Exit Do
End If
' Swap the lo and hi values.
Dist(hi) = Dist(lo)
OtherInfo(hi) = OtherInfo(lo)
SORTSWAPS = SORTSWAPS + 1&
Loop
QuickSortSingle2 Dist(), OtherInfo(), Min, lo - 1&
QuickSortSingle2 Dist(), OtherInfo(), lo + 1&, Max
End Sub
Public Sub QuickSortSingle3(Dist() As Single, OtherInfo() As Long, ByVal Min As Long, ByVal Max As Long)
Attribute QuickSortSingle3.VB_UserMemId = 1610612744
' FROM HI to LOW 'https://www.vbforums.com/showthread.php?11192-quicksort
Dim Low As Long, high As Long, temp As Single, TestElement As Single, tmp&
' Debug.Print min, max
Low = Min: high = Max
' TestElement = Dist((Min + Max) \ 2)
TestElement = (Dist(Min) + Dist(Max)) * 0.5
Do
Do While Dist(Low) > TestElement: Low = Low + 1&: Loop
Do While Dist(high) < TestElement: high = high - 1&: Loop
If (Low <= high) Then
temp = Dist(Low): Dist(Low) = Dist(high): Dist(high) = temp
tmp = OtherInfo(Low): OtherInfo(Low) = OtherInfo(high): OtherInfo(high) = tmp
Low = Low + 1&: high = high - 1&
SORTSWAPS = SORTSWAPS + 1&
End If
Loop While (Low <= high)
If (Min < high) Then QuickSortSingle3 Dist(), OtherInfo(), Min, high
If (Low < Max) Then QuickSortSingle3 Dist(), OtherInfo(), Low, Max
End Sub