高中信息技术算法与程序设计 排序算法
试卷更新日期:2022-03-30 类型:同步测试
一、选择题
-
1. 采用冒泡排序算法对数组a中的6个数据“8,20,5,17,30,9”进行排序,部分程序代码如下:
For i=1 To 2
For j=6 To i+1 Step -1
If a(j)>a(j-1) Then
t=a(j): a(j)=a(j-1): a(j-1)=t
End If
Next j
Next i
运行该程序,数据交换的次数为( )
A、1 B、2 C、3 D、42. 采用冒泡排序对数组d中元素进行排序,第一趟排序后的结果为“10,25,15,30,50,38,60,55”,则d数组中元素的初始顺序不可能是( )A、25,15,30,50,38,60,55,10 B、25,15,30,50,38,60,10,55 C、25,15,10,50,38,60,30,55 D、25,15,30,50,10,38,60,553. 对数组元素b(1)~b(8)进行降序排序,其排序算法的VB部分程序如下:For x = 1 To 7
w = x
For y = x + 1 To 8
Next y
If w < > x Then
Temp = b(x): b(i) = b(w): b(w) = temp
End If
Next x
方框中应填入的语句为( )
A、If b(w) > b(y) Then w = y B、If b(w) < b(y) Then w = y C、If b(w) > b(y) Then w = x D、If b(w) < b(y) Then w = x4. 有如下程序段:For i = 1 To 4
For j = i + 1 To 5
If a(j) > a(i) Then t = a(j): a(j) = a(i): a(i) = t
Next j
Next i
数组元素a(1)~a(5)的值依次为“15,4,12,7,9”,整个排序过程中,数组中数据比较次数和交换次数分别是( )
A、比较11次,交换3次 B、比较11次,交换4次 C、比较10次,交换3次 D、比较10次,交换4次5. 如下VB程序段:st = “ ”
For i = 1 To 4
For j = 1 To 10 - i
If d(j) < d(j + 1) Then
temp = d(j): d(j) = d(j + 1): d(j + 1) = temp
End If
Next j
st = Str(d(j)) + st
Next i
Text1.Text = st
数组元素d(1)~d(10)中的数据依次为“21,8,12,16,26,71,65,89,42,36”,程序执行后,在文本框Text1中显示的内容是( )
A、8,12,16,21 B、42,65,71,89 C、21,16,12,8 D、89,71,65,426. 有如下VB程序段:Private Sub Command1_Click()
Dim a(1 To 100) As String, x As String, st As String
Dim i As Integer, j As Integer, n As Integer
x = Text1.Text
n = Len(Text1.Text)
For i = 1 To n
a(i) = Mid(x, i, 1)
Next i
For i = 1 To n - 1
For j = n To i + 1 Step -1
If a(j) > a(j - 1) Then
st = a(j): a(j) = a(j - 1): a(j - 1) = st
End If
Next j
Next i
st = “ ”
For i = 1 To n
st = st + a(i)
Next i
Label1.Caption = st
End Sub
程序运行时,在文本框Text1中输入“Text”(不包含双引号),单击按钮Command1,则在标签Label1上显示的内容为( )
A、Tetx B、Txte C、xtTe D、xteT二、非选择题
-
7. 下面是一个采用拉锯式排序法对数组元素按升序进行排序的程序,所谓“拉锯式排序法”是这一遍把最小的元素从下往上移动到最上面的位置,下一遍则是从上往下把最大的元素移动到最下面的位置。 程序运行效果如下图所示。
Private Sub Command1_Click()
Dim a(10) As Integer, i As Integer
Randomize ′产生不同的随机数
For i = 1 To 10
a(i) = Int(Rnd * 100) + 1
Text1.Text = Text1.Text & Str(a(i))
Next i
Call Shaker_sort(a()) '调用Shaker_sort
For i = 1 To 10
Text2.Text = Text2.Text & Str(a(i))
Next i
End Sub
Private Sub Shaker_sort(k() As Integer)
Dim i As Integer,c As Integer,d As Integer
Dim t As Integer
c = 1
d = 10
Do While c<=d
For i= '从下往上把最小的元素移到最上面的位置
If k(i-1)>k(i) Then
t = k(i - 1):k(i- 1) = k(i):k(i) = t
End If
Next i
For i = c+1 To d-c '从上到下把最大的元素移到最下面的位置
If Then
t = k(i+1):k(i+1) = k(i):k(i) = t
End If
Next i
c = c + 1
Loop
End Sub
8. 小李基于冒泡排序算法编写了一个VB程序,功能如下:在文本框Text1中显示排序前的数据,单击“排序”按钮Command1,在文本框Text2中显示剔除重复数据后的升序排序结果。程序运行界面如下图所示。实现上述功能的VB程序如下,但加框处代码有错,请改正。
Const n = 10
Dim a(1 To n) As Integer
Private Sub Command1_Click()
Dim i As Integer, j As Integer, t As Integer,bottom As Integer
'获取排序前数据依次存储在数组a中,并在文本框Text1中显示。代码略
bottom = n
i = 1
Do While i <= bottom - 1
For j = bottom To i + 1 Step -1
If Then '①
t = a(j): a(j) = a(j - 1): a(j - 1) = t
ElseIf a(j) = a(j - 1) Then '相邻两个数据相等,进行剔除处理
'②
bottom = bottom - 1
End If
Next j
i = i + 1
Loop
Text2.Text = “ ”
For i = 1 To bottom
Text2.Text = Text2.Text + Str(a(i))
Next i
End Sub
程序中加框①处应改正为;
加框②处应改正为。
9. 编写一个VB程序,实现功能为在n个数中寻找第m大的数。程序运行时,自动生成n个三位数随机整数,并显示在列表框List1中,单击“排序”按钮Command1,将数据进行排序,并将排好序的数据显示在列表框List2中,在文本框Text1中输入m,单击“计算”按钮Command2,在文本框Text2中显示第m大的数。程序运行效果如下图所示:为实现上述功能的VB程序如下,在程序划线处填入合适的代码。
Dim d(1 To 20) As Integer
Const n = 20
Private Sub Form_Load()
Dim i As Integer, j As Integer, t As Single
List1.Clear
Randomize
For i = 1 To n
List1.AddItem Str(d(i))
Next i
End Sub
Private Sub Command1_Click()
Dim i As Integer, j As Integer, t1 As Integer
For i = 1 To n - 1
For j =
If d(j) < d(j - 1) Then
t1 = d(j): d(j) = d(j - 1): d(j - 1) = t1
End If
Next j
Next i
For i = 1 To n
List2.AddItem Str(d(i))
Next i
End Sub
Private Sub Command2_Click()
Dim m As Integer
m = Val(Text1.Text)
Text2.Text = “第” + Str(m) + “ 大的数为:” +
End Sub
10. 小王编写了一个数据排序且将重复数删除的VB程序。算法的基本思想:先随机生成20个[1,50]区间内的整数,然后按照数据从低到高进行排序,最后删除重复数(相同的数据只保留一个)。程序运行时,单击“生成随机数”按钮Command1,则生成20个[1,50]区间内的整数,存储在数组a中,并显示在文本框Text1中;单击“排序并去重”按钮Command2,则将数组a中数据进行从低到高排序,并将排序结果显示在文本框Text2中,将去重后结果显示在文本框Text3中,在标签Label4中显示共删除的重复数个数,程序运行界面如下图所示。
实现上述功能的VB程序如下,请回答下列问题:
(1)、若20个随机数分别为:7,18,5,7,7,12,7,1,10,14,17,7,9,7,10,16,5,18,8,18,则去掉的重复数共个。(2)、请在划线处填入合适的代码。Dim a(1 To 20) As Integer
Const n = 20
Private Sub Command1_Click()
Dim i As Integer, s1 As String
Randomize
s1 = “ ”
For i = 1 To n
a(i) = Int(Rnd * 50 + 1)
s1 = s1 + Str(a(i))
Next i
Text1.Text = s1
End Sub
Private Sub Command2_Click()
Dim i As Integer, j As Integer, p As Integer, tt As Integer
Dim ans As Integer, s2 As String, s3 As String
For i = 1 To n - 1
p = i
For j = n To i + 1 Step -1
If a(j) < a(p) Then p = j
Next j
If a(i) < > a(p) Then
tt = a(i)
a(i) = a(p)
a(p) = tt
End If
Next i
s2 = “ ”:s3 = “ ”
ans = 0
For i = 1 To n
s2 = s2 + Str(a(i))
Next i
Text2.Text = s2
s3 = Str(a(1))
For i = 2 To n
If a(i) < > q Then
q = a(i)
Else
End If
Next i
Text3.Text = s3
Label4.Caption = Str(ans)
End Sub