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

最新文章

  1. Xshell远程管理Linux
  2. 【转】清理Kylin的中间存储数据(HDFS &amp; HBase Tables)
  3. vim时,ctrl+s了一下,程序僵死了
  4. Delegate。。
  5. JavaScript和php常用语法——切割字符串
  6. 【Javascript】搞定JS面试——跨域问题
  7. Ubuntu离线安装Sogou拼音(附老版本安装&amp;输入法自启动)
  8. (@WhiteTaken)设计模式学习——组合模式
  9. Android Weekly Notes Issue #285
  10. flask wtforms组件详解
  11. JavaScript学习day2 (基本语法上)
  12. Windows 主机名映射地址
  13. python tkinter-单选、多选
  14. Improved SEO with mod_rewrite
  15. Project Euler Problem3
  16. python 函数式编程学习笔记
  17. 20172329 2018-2019《Java程序设计与数据结构》课程总结
  18. 有关UITableviewCell 重用内存 内部解析
  19. 【Oracle 12c】最新CUUG OCP-071考试题库(59题)
  20. 在js中获取到的页面元素为undefined

热门文章

  1. mysql 索引对于select速度提升作用实验
  2. Tiles布局
  3. java SWT嵌入IE,SafeArray .
  4. Linux下hosts、host.conf、resolv.conf
  5. Nginx负载均衡和Keepalived的安装设置
  6. 不同版本的mysql字符集的默认编写
  7. phpcms v9 后台首页 去掉团队信息等版权
  8. 8、手把手教你Extjs5(八)自定义菜单2
  9. bzoj4010: [HNOI2015]菜肴制作【拓扑排序】
  10. Spring AOP进行日志记录,管理