1. 火车调度台是实现火车车厢整理的平台,当相邻2节车厢序号不符合整理要求时,可以对调2节车厢,实现序号顺序调整。相邻2个进行符合目标的交换,和我们学习的冒泡排序思想一致,所以这个调度过程可以用冒泡排序实现。为了提高效率,对冒泡排序做了优化,请完善下列代码:

nums = [3, 1, 2, 4, 5, 6]

k = n - 1

for i in range(n - 1):

   

    for j in range(k):

        if (nums[j] > nums[j + 1]):

            nums[j], nums[j + 1] = nums[j + 1], nums[j]

           

            ex_flag = True

    if (ex_flag):

        break

print(nums)

【考点】
排序算法及程序实现;
【答案】

您现在未登录,无法查看试题答案与解析。 登录
综合题 普通
能力提升
变式训练
拓展培优
真题演练
换一批
3. 图图基于排序算法设计了一个数字矩阵排序的VB程序,功能如下:在文本框Text1中输入矩阵的大小n,单击“生成”按钮Command1,在左侧列表框List1中显示行数和列数均为n的随机整数矩阵。单击“排序”按钮Command2,奇数行从左到右升序排序,偶数行从左到右降序排序,结果显示在列表框List2中,输出时保持矩阵大小不变。运行结果如图所示。

请回答下列问题:

(1) 实现上述功能的 VB 程序如下,请在划线处填入合适的代码。

Dim a(1 To 100) As Integer

Dim n As Integer

Private Sub Command1_Click()

    Dim i As Integer, j As Integer

    Dim s As String, k As Integer

    n = Val(Text1.Text)

    For i = 1 To n

       

        For j = 1 To n

            k = (i - 1) * n + j

            a(k) = Int(Rnd() * 45 + 5) * 2

            s = s + " " + Str(a(k))

        Next j

        List1.AddItem s

    Next i

End Sub

Private Sub Command2_Click()

    Dim i As Integer, L As Integer, R As Integer

    Dim f As Integer

    For i = 1 To n

    If i Mod 2 = 1 Then f = 1 Else f = -1

       

        R = i * n

        List2.AddItem sort(L, R, f)

    Next i

End Sub

Function sort(left As Integer, right As Integer, order As Integer) As String

    Dim i As Integer, j As Integer, k As Integer

    Dim s As String, t As Integer

    s = ""

    For i = left To right - 1

        k = i

        For j = i + 1 To right

            If  Then k = j

        Next j

        If k <> i Then

            t = a(k): a(k) = a(i): a(i) = t

        End If

        s = s + " " + Str(a(i))

    Next i

   

    sort = s

End Function

(2) 若删除上述VB程序中加框处代码,(选填:会/不会)影响程序运行结果。
综合题 困难