当前位置: 高中信息技术 / 综合题
  • 1. (2017·浙江模拟) 小张学习了冒泡排序后,觉得还可以再改进,用K指向要排序的数的头部,用m指向要排序的数的尾部,每一轮先从前向后扫描k~m-1, 比较相邻若逆序则交换,找出本轮最大数放到本轮数据的尾部,再从后到前扫描剩下的序列m~k+1,比较相邻若逆序则交换,找出本轮最小数放到本轮数据头部。直到k=m结束程序。以下是改进后的程序请补充完整。
    Dim n As Integer   ‘数字的个数
    Rem 以下程序段为产生n个[10,99]之间的随机整数并在list1中显示
    Private Sub Command1_Click()
    Randomize Time
    n = 50
    For i = 1 To n
        List1.AddItem Str(Int(Rnd * 90) + 10)
    Next
    End Sub
    Rem 以下程序段为从list1中取出数并进行排序,最后在list2中显示排序后的数
    Private Sub Command2_Click()
    Dim a(50) As Integer
    Dim k As Integer, m As Integer, i As Integer, j As Integer
    For i = 1 To n
        a(i) = Val(List1.List(i - 1))
    Next i
    m = n:k=1
    Do While
        J=m-1 : m=1
     For i = k To j
          If a(i+1) < a(i) Then t = a(i): a(i) = a(i + 1): a(i + 1) = t: m=i
        Next i
    j =
    k=1
    For i = m To  j  Step -1
            If a(i) < a(i - 1) Then t = a(i): a(i) = a(i - 1): a(i - 1) = t: 
        Next i
       
    Loop
    For i = 1 To n
        List2.AddItem
    Next i
    End Sub

微信扫码预览、分享更方便