写了一个VB语言基础的快速排序,但不能完成任务,有小部分数据会排错位,哪位可以帮助看看,谢谢
程序员文章站
2022-05-17 21:36:06
...
程序源码如下:
Function Partitions(ByRef a() As Integer, LeftNum As Integer, RightNum As Integer) As Integer
Dim signal As Integer
Dim Tail As Integer
Dim I As Integer
signal = a(RightNum)
Tail = RightNum - 1
For I = LeftNum To Tail
If a(I) < signal Then
Call Swap(a(I), a(Tail)): Tail = Tail - 1: I = I - 1
If I > Tail Then Exit For
If I < LeftNum Then I = I + 1
End If
Next I
Call Swap(a(Tail + 1), a(RightNum))
Partitions = Tail + 1
End Function
Private Sub QuickSorts(ByRef a() As Integer, LeftNum As Integer, RightNum As Integer)
Dim signal As Integer
If LeftNum < RightNum - 1 Then
signal = Partitions(a, LeftNum, RightNum)
Call QuickSorts(a, LeftNum, signal - 1)
Call QuickSorts(a, signal + 1, RightNum)
ElseIf LeftNum = RightNum - 1 Then
If a(LeftNum) < a(RightNum) Then Call Swap(a(LeftNum), a(RightNum))
End If
End Sub
Private Sub Command2_Click()
Dim signal As Integer
Dim I As Integer
Dim h As Integer
Dim SerialNumber(1 To 50) As String
Dim a(50) As Integer
List1.Clear
List2.Clear
Randomize
For I = 1 To 50
SerialNumber(I) = CStr(I)
Do While Len(SerialNumber(I)) < 2
SerialNumber(I) = "0" + SerialNumber(I)
Loop
a(I) = Int(Rnd() * 10000)
List1.AddItem SerialNumber(I) + " " + Str(a(I))
Next I
Call QuickSorts(a, 1, 50)
For I = 1 To 50
List2.AddItem SerialNumber(I) + " " + Str(a(I))
Next I
End Sub
上一篇: java 根据年月统计当月有多少个工作日
下一篇: 数据结构与算法——插值查找算法实现