永发信息网

随机产生十个10-100的整数,用选择排序法按值从小到大顺序排序,我是学VB的。急急。。。我要具体的方法

答案:2  悬赏:80  手机版
解决时间 2021-02-15 13:38
  • 提问者网友:爱唱彩虹
  • 2021-02-14 17:05
随机产生十个10-100的整数,用选择排序法按值从小到大顺序排序,我是学VB的。急急。。。我要具体的方法
最佳答案
  • 五星知识达人网友:风格不统一
  • 2021-02-14 17:42
窗体上加一list1,一command1,代码如下

Private Sub Command1_Click()
Dim arr(10)
Randomize
For I = 0 To 9
arr(I) = Int(Rnd * 90)+10
Next
Selection arr(), 0
For I = 0 To 9
List1.AddItem arr(I)
Next
End Sub

标准模块,里面有常用的7种排序方法,Selection是选择排序

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(Index - 1)
Index = Index - 1
Else
Exit Do
End If
ElseIf nOrder = DESCENDING_ORDER Then
If TheBucket >= MyArray(Index - 1) Then
MyArray(Index) = MyArray(Index - 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(Parent + 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(Index - 1) Then
TEMP = MyArray(Index)
MyArray(Index) = MyArray(Index - 1)
MyArray(Index - 1) = TEMP
Index = Index - 1
Else
Exit Do
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
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) Then
Min = Index
End If
ElseIf nOrder = DESCENDING_ORDER Then
If MyArray(Index) >= MyArray(Min) Then
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) + Distance) Then
If nOrder = ASCENDING_ORDER Then
If MyArray(Index) < MyArray(Index - Distance) Then
TEMP = MyArray(Index)
MyArray(Index) = MyArray(Index - Distance)
MyArray(Index - Distance) = TEMP
Index = Index - Distance
gIterations = gIterations + 1
Else
Exit Do
End If
ElseIf nOrder = DESCENDING_ORDER Then
If MyArray(Index) >= MyArray(Index - Distance) Then
TEMP = MyArray(Index)
MyArray(Index) = MyArray(Index - Distance)
MyArray(Index - Distance) = TEMP
Index = Index - Distance
gIterations = gIterations + 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
全部回答
  • 1楼网友:西岸风
  • 2021-02-14 19:12
急什么啊,百度上很多啊,随便搜一下就有了. private sub command1_click() dim su(1 to 10) as integer '定义数组 '产生随机数 for i = 1 to 10 su(i) = rnd * 90 + 10 '产生10-100的数 print su(i); next print '排序 '这样好像是选择排序吧,先选一个最小的数放在前面 for i = 1 to 9 for j = i to 10 if su(i) > su(j) then t = su(i) su(i) = su(j) su(j) = t end if next print su(i); next print su(i) end sub
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯