Imports System.Threading
Module Module1
Sub Main()
'test code
'Dim a, b As Integer
'a = 3
'b = 4
'Exchange(a, b)
'Console.WriteLine("a=" & a & ", b=" & b)
Dim int_array(19) As Integer
Dim maxValIndex As Integer
Dim startIdx As Integer '开始位置
Dim rnd As New Random
Console.WriteLine("原始数组:")
'填充数组元素
For i As Integer = 0 To int_array.Length - 1
int_array(i) = rnd.Next(0, 500)
Console.Write(int_array(i) & ", ")
Thread.Sleep(150)
Next
'选择法排序(降序)
startIdx = 0 '默认从第一个元素开始比较
Do
maxValIndex = startIdx '默认第一个元素最大
maxValIndex = GetMaxValIdx(int_array, maxValIndex, startIdx) '求数组中最大值得下标
If (maxValIndex <> startIdx) Then
Exchange(int_array(maxValIndex), int_array(startIdx))
End If
startIdx += 1
Loop While (startIdx < int_array.Length - 1)
Console.WriteLine(vbCrLf & "排序之后:")
For i As Integer = 0 To int_array.Length - 1
Console.Write(int_array(i) & ", ")
Next i
Console.ReadKey()
End Sub
'求数组中最大值得下标,递归函数
Public Function GetMaxValIdx(ByVal array() As Integer, ByVal maxValIdx As Integer, ByVal startIdx As Integer) As Integer
For i As Integer = startIdx To array.Length - 1
If (i <> maxValIdx) Then
If array(i) > array(maxValIdx) Then
Return GetMaxValIdx(array, i, startIdx)
End If
End If
Next
Return maxValIdx
End Function
'交换值
Public Sub Exchange(ByRef val1 As Integer, ByRef val2 As Integer)
Dim temp As Integer
temp = val1
val1 = val2
val2 = temp
End Sub
End Module
相关文章
- 06-26你需要知道的九大排序算法【Python实现】之堆排序
- 06-26拓扑排序
- 06-26SQL 无法解决 EQUAL TO 运算中 "CHINESE_PRC_CI_AS" 和 "CHINESE_PRC_90_CI_AI" 之间的排序规则冲突
- 06-26外部排序
- 06-26计数排序 洛谷P1271 【深基9.例1】选举学生会
- 06-26python 实现快排序
- 06-26数据结构 10-排序5 PAT Judge (25 分)
- 06-26SDNU 1201.简单排序
- 06-26JS排序:localeCompare() 方法实现中文排序、sort方法实现数字英文混合排序
- 06-26113.输入10个国家的名字,按由小到大排序