插入排序算法导学案
Posted Python算法之旅
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了插入排序算法导学案相关的知识,希望对你有一定的参考价值。
插入排序算法导学案
Const size = 20 '数组分配空间
Dim a(1 To size) As Integer
Dim i As Integer, n As Integer '数组实际长度
Private Sub Command1_Click()
Dim p As Integer, x As Integer
x = Val(Text3.Text) '插入数据
p = Val(Text4.Text) '插入位置
If p > n Then p = n + 1
For i = n To p Step -1
a(i + 1) = a(i)
Next i
a(p) = x
n = n + 1
End Sub
Const size = 20 '数组分配空间
Dim a(1 To size) As Integer
Dim i As Integer, n As Integer '数组实际长度
Private Sub Command2_Click()
Dim p As Integer, x As Integer
x = Val(Text3.Text) '插入数据
For p = 1 To n
If a(p) > x Then ①
Next p
For i = n To p Step -1
+ 1) = ②
Next i
③ =
n = n + 1
End Sub
Const n = 10
Dim a(1 To n) As Integer
Private Sub Command3_Click()
Dim x As Integer
For i = 2 To n
x = a(i)
For j = i - 1 To 1 Step -1
If a(j) <= x Then ①
+ 1) = ②
Next j
+ 1) = ③
Next i
End Sub
Const n = 10
Dim a(0 To n) As Integer
Private Sub Command5_Click()
For i = 2 To n
a(i): j = i - 1 =
Do While ①
+ 1) = ②
j = j - 1
Loop
+ 1) = ③
Next i
End Sub
Const n = 4
Dim a(1 To n) As Integer
Private Sub Command4_Click()
Dim x As Integer
For i = 2 To n
x = a(i): j = i - 1
Do While j >= 1 And x < a(j)
+ 1) = a(j)
j = j - 1
Loop
+ 1) = x
Next i
End Sub
Const n = 5
Dim a(1 To n) As Integer
Private Sub Command6_Click()
Dim t As Integer
For i = 2 To n
For j = i To 2 Step -1
If a(j - 1) > a(j) Then
t = a(j): a(j) = a(j - 1): a(j - 1) = t
Else
Exit For
End If
Next j
Next i
End Sub
1. 【答案】
(1)48,36,24,3,97,77
(2)48,36,24,3,97,77,8
2.【答案】
① Exit For ② a(i) ③ x
参考代码:
Private Sub Command2_Click()
Dim x As Integer
x = Val(Text3.Text) '插入数据
For i = n To 1 Step -1
If a(i) <= x Then Exit For
+ 1) = a(i)
Next i
+ 1) = x
n = n + 1
End Sub
3.【答案】① Exit For ② a(j) ③ x
【解析】插入排序算法最重要的操作就是向左侧已排序区间插入待排序元素。外层循环用来控制待排序元素位置,内层循环用来扫描已排序区间,为待排序元素腾出插入位置。
4.【答案】① a(0) < a(j) ② a(j) ③ a(0)
【解析】设置数组下标下界为0,既解决了数组下标越界问题(无需判断j >= 1,j=0时循环自然结束),又可以使用a(0)作为辅助空间存储a(i)的值一举两得。
5.【答案】程序运行,当j=0时会出现数组下标越界错误。解决数组下标越界问题有两种方法:1.扩大数组下标下界;2.提前跳出循环。
方法一:扩大数组下标下界。把数组a的定义语句改成Dim a(0 To n) As Integer
方法二:提前跳出循环。当j=0时,跳出循环。参考代码如下:
Const n = 4
Dim a(1 To n) As Integer
Private Sub Command4_Click()
Dim x As Integer
For i = 2 To n
x = a(i): j = i - 1
Do While x < a(j) '无需判断j >= 1,j=0时循环结束
+ 1) = a(j)
j = j – 1
If j = 0 Then Exit Do
Loop
+ 1) = x
Next i
End Sub
6. 【答案】(1)2, 3, 5, 6, 8
(2)参考代码:
Const n = 5
Dim a(1 To n) As Integer
Private Sub Command6_Click()
Dim t As Integer
For i = 2 To n
j = i
Do While a(j - 1) > a(j)
t = a(j): a(j) = a(j - 1): a(j - 1) = t
If j > 2 Then j = j - 1 Else Exit Do
Loop
Next i
End Sub
(3)冒泡排序的本质特征是内层循环扫描待排序区间将最小值(或最大值)向左端(或右端)冒泡,交换操作只能发生在相邻元素之间。
插入排序的本质特征是内层循环扫描已排序区间,将待排序元素插入(或交换)到正确位置。
二者的本质区别在于内层循环扫描的范围,冒泡排序是扫描待排序区间,而插入排序是扫描已排序区间。
本题内层循环的扫描范围的已排序区间,故更接近插入排序。
因为本题没有设置临时变量t来存储待排序元素a(i),不能采用移位方式来插入a(i),所以只好采用交换相邻元素的方式将a(i)放到正确位置,这种做法和低效的选择排序相似,我们可以称之为低效的插入排序算法。
需要本文word版的,可以加入“选考VB算法解析”知识星球参与讨论和下载文件,“选考VB算法解析”知识星球汇集了数量众多的同好,更多有趣的话题在这里讨论,更多有用的资料在这里分享。
我们专注选考VB算法,感兴趣就一起来!
相关优秀文章:
以上是关于插入排序算法导学案的主要内容,如果未能解决你的问题,请参考以下文章
求八年级上册英语导学unit12 period2(sectionA 3a---4)答案