假如有10个数字,用数组表示 Dim a(1 to 10) As Integer,现在已经知道a(1)、a(2)、a(3)....
a(10)的值,但如何把它们按从小到大的次序派出来呢?比如具体的值如下:
a(1)=99
a(2)=87
a(3)=50
a(4)=60
a(5)=100
a(6)=65
a(7)=88
a(8)=92
a(9)=93
a(10)=75
根据以上数据,按从小排序的次序应该是
a(3)、a(4)、a(6)、a(10)、a(2)、a(7)、a(8)、a(9)、a(1)、a(5)
如何得到上述结果呢?请指教。
a(10)的值,但如何把它们按从小到大的次序派出来呢?比如具体的值如下:
a(1)=99
a(2)=87
a(3)=50
a(4)=60
a(5)=100
a(6)=65
a(7)=88
a(8)=92
a(9)=93
a(10)=75
根据以上数据,按从小排序的次序应该是
a(3)、a(4)、a(6)、a(10)、a(2)、a(7)、a(8)、a(9)、a(1)、a(5)
如何得到上述结果呢?请指教。
dim temp as long
for i= 0 to 10
for j=i to 10
if a(i)<a(j) then
temp=a(i)
a(i)=a(j)
a(j)=temp
end if
next j
next i这样就行啦!对了,楼主,你的信誉分怎么不是一百分啊???
我会继续努力的,谢谢。
Call BubbleSort(mArray(), Order)
Call Insertion(mArray(), Order)
Call Bucket(mArray(), Order)
Call Selection(mArray(), Order)
Call ShellSort(mArray(), Order)
Call QuickSort(mArray(), 0, UBound(mArray))
Call Heap(mArray())
Option Explicit
Global Const ZERO = 0
Global Const ASCENDING_ORDER = 0
Global Const DESCENDING_ORDER = 1
Global gIterations
Sub BubbleSort(MyArray(), ByVal nOrder As Integer)
Dim Index
Dim TEMP
Dim NextElement
NextElement = ZERO
Do While (NextElement < UBound(MyArray))
Index = UBound(MyArray)
Do While (Index > NextElement)
If nOrder = ASCENDING_ORDER Then
If MyArray(Index) < MyArray(Index -
1) Then
TEMP = MyArray(Index)
MyArray(Index) = MyArray(Index -
1)
MyArray(Index - 1) = TEMP
End If
ElseIf nOrder = DESCENDING_ORDER Then
If MyArray(Index) >= MyArray(Index -
1) Then
TEMP = MyArray(Index)
MyArray(Index) = MyArray(Index -
1)
MyArray(Index - 1) = TEMP
End If
End If
Index = Index - 1
gIterations = gIterations + 1
Loop
NextElement = NextElement + 1
gIterations = gIterations + 1
Loop
End Sub
Sub Bucket(MyArray(), ByVal nOrder As Integer)
Dim Index
Dim NextElement
Dim TheBucket
NextElement = LBound(MyArray) + 1
While (NextElement <= UBound(MyArray))
TheBucket = MyArray(NextElement)
Index = NextElement
Do
If Index > LBound(MyArray) Then
If nOrder = ASCENDING_ORDER Then
If TheBucket < MyArray(Index
- 1) Then
MyArray(Index) = MyArray(I
ndex - 1)
Index = Index - 1
Else
Exit Do
End If
ElseIf nOrder = DESCENDING_ORDER Then
If TheBucket >= MyArray(Index
- 1) Then
MyArray(Index) = MyArray(I
ndex - 1)
Index = Index - 1
Else
Exit Do
End If
End If
Else
Exit Do
End If
gIterations = gIterations + 1
Loop
MyArray(Index) = TheBucket
NextElement = NextElement + 1
gIterations = gIterations + 1
Wend
End Sub
Sub Heap(MyArray())
Dim Index
Dim Size
Dim TEMP
Size = UBound(MyArray) Index = 1
While (Index <= Size)
Call HeapSiftup(MyArray(), Index)
Index = Index + 1
gIterations = gIterations + 1
Wend
Index = Size
While (Index > 0)
TEMP = MyArray(0)
MyArray(0) = MyArray(Index)
MyArray(Index) = TEMP
Call HeapSiftdown(MyArray(), Index - 1)
Index = Index - 1
gIterations = gIterations + 1
Wend
End Sub Sub HeapSiftdown(MyArray(), M)
Dim Index
Dim Parent
Dim TEMP
Index = 0
Parent = 2 * Index
Do While (Parent <= M) If (Parent < M And MyArray(Parent) < MyArray(Pa
rent + 1)) Then
Parent = Parent + 1
End If
If MyArray(Index) >= MyArray(Parent) Then
Exit Do
End If
TEMP = MyArray(Index)
MyArray(Index) = MyArray(Parent)
MyArray(Parent) = TEMP Index = Parent
Parent = 2 * Index
gIterations = gIterations + 1
Loop
End Sub
Sub HeapSiftup(MyArray(), M)
Dim Index
Dim Parent
Dim TEMP
Index = M
Do While (Index > 0)
Parent = Int(Index / 2)
If MyArray(Parent) >= MyArray(Index) Then
Exit Do
End If TEMP = MyArray(Index)
MyArray(Index) = MyArray(Parent)
MyArray(Parent) = TEMP
Index = Parent
gIterations = gIterations + 1
Loop End Sub
Sub Insertion(MyArray(), ByVal nOrder As Integer)
Dim Index
Dim TEMP
Dim NextElement NextElement = LBound(MyArray) + 1
While (NextElement <= UBound(MyArray))
Index = NextElement
Do
If Index > LBound(MyArray) Then
If nOrder = ASCENDING_ORDER Then
If MyArray(Index) < MyArray(In
dex - 1) Then
TEMP = MyArray(Index)
MyArray(Index) = MyArray(I
ndex - 1)
MyArray(Index - 1) = TEM
P
Index = Index - 1
Else
Exit Do
End If
ElseIf nOrder = DESCENDING_ORDER Then
If MyArray(Index) >= MyArray(I
ndex - 1) Then
TEMP = MyArray(Index)
MyArray(Index) = MyArray(I
ndex - 1)
MyArray(Index - 1) = TEM
P
Index = Index - 1
Else
Exit Do
End If
End If
Else
Exit Do
End If
gIterations = gIterations + 1
Loop
NextElement = NextElement + 1
gIterations = gIterations + 1
Wend
End Sub
Sub QuickSort(MyArray(), L, R)
Dim I, J, X, Y
I = L
J = R
X = MyArray((L + R) / 2) While (I <= J)
While (MyArray(I) < X And I < R)
I = I + 1
Wend
While (X < MyArray(J) And J > L)
J = J - 1
Wend
If (I <= J) Then
Y = MyArray(I)
MyArray(I) = MyArray(J)
MyArray(J) = Y
I = I + 1
J = J - 1
End If
gIterations = gIterations + 1
Wend
If (L < J) Then Call QuickSort(MyArray(), L, J)
If (I < R) Then Call QuickSort(MyArray(), I, R)
End Sub Sub Selection(MyArray(), ByVal nOrder As Integer)
Dim Index
Dim Min
Dim NextElement
Dim TEMP
NextElement = 0
While (NextElement < UBound(MyArray))
Min = UBound(MyArray)
Index = Min - 1
While (Index >= NextElement)
If nOrder = ASCENDING_ORDER Then
If MyArray(Index) < MyArray(Min) ThenMin = Index
End If
ElseIf nOrder = DESCENDING_ORDER Then
If MyArray(Index) >= MyArray(Min) The
n
Min = Index
End If
End If
Index = Index - 1
gIterations = gIterations + 1
Wend
TEMP = MyArray(Min)
MyArray(Min) = MyArray(NextElement)
MyArray(NextElement) = TEMP
NextElement = NextElement + 1
gIterations = gIterations - 1
Wend
End Sub
Sub ShellSort(MyArray(), ByVal nOrder As Integer)
Dim Distance
Dim Size
Dim Index
Dim NextElement
Dim TEMP
Size = UBound(MyArray) - LBound(MyArray) + 1
Distance = 1
While (Distance <= Size)
Distance = 2 * Distance
Wend
Distance = (Distance / 2) - 1 While (Distance > 0) NextElement = LBound(MyArray) + Distance While (NextElement <= UBound(MyArray))
Index = NextElement
Do
If Index >= (LBound(MyArray) + Dista
nce) Then
If nOrder = ASCENDING_ORDER ThenIf MyArray(Index) < My
Array(Index - Distance) Then
TEMP = MyArray(Ind
ex)
MyArray(Index) = M
yArray(Index - Distance)
MyArray(Index - Di
stance) = TEMP
Index = Index -
Distance
gIterations = gIte
rations + 1
Else
Exit Do
End If
ElseIf nOrder = DESCENDING_ORDER
Then
If MyArray(Index) >= M
yArray(Index - Distance) Then
TEMP = MyArray(Ind
ex)
MyArray(Index) = M
yArray(Index - Distance)
MyArray(Index - Di
stance) = TEMP
Index = Index -
Distance
gIterations = gIte
rations + 1
Else
Exit Do
End If
End If
Else
Exit Do
End If
Loop
NextElement = NextElement + 1
gIterations = gIterations + 1
Wend
Distance = (Distance - 1) / 2
gIterations = gIterations + 1
Wend End Sub
以上代码保存于: SourceCode Explorer(源代码数据库)
复制时间: 2005-08-14 19:46:31
软件版本: 1.0.870
软件作者: Shawls
E-Mail: [email protected]
QQ: 9181729
1、你知道要排序的数列的范围。比如你的数列范围是100以内整数。
2、范围有限,在你这个例子里面是integer的范围。-32768到32767,范围是65536,因此可行。
3、序列内有大量重复,并且序列本身的元素数量接近或大于值的范围。本例不符合这个条件,因此效率不如正常算法。但如果符合该条件,这个算法是相当有效的。Private Sub Form_Load()
Dim a(1 To 10) As Integer
a(1) = 99
a(2) = 87
a(3) = 50
a(4) = 60
a(5) = 100
a(6) = 65
a(7) = 88
a(8) = 92
a(9) = 93
a(10) = 75 Dim tTable() As Integer
ReDim tTable(-32768 To 32767) Dim tIndex As Long For tIndex = 1 To 10
tTable(a(tIndex)) = tTable(a(tIndex)) + 1
Next Dim tTable_Index As Long
Dim tOutIndex As Long
Dim tOA() As Integer
Dim tOA_Length As Integer
tOA_Length = 1
For tTable_Index = -32768 To 32767
For tOutIndex = 1 To tTable(tTable_Index)
ReDim Preserve tOA(1 To tOA_Length)
tOA(tOA_Length) = tTable_Index
tOA_Length = tOA_Length + 1
Next
Next
For tIndex = 1 To UBound(tOA())
Text1.Text = Text1.Text & " " & tOA(tIndex)
Next
End Sub