早教吧 育儿知识 作业答案 考试题库 百科 知识分享

对一组无序数据,①设计直接插入排序算法;②再对得到的有序序列进行折半查找.

题目详情
对一组无序数据,①设计直接插入排序算法;②再对得到的有序序列进行折 半查找.
▼优质解答
答案和解析
Sub Insertion(MyArray(),ByVal nOrder As Integer)
Dim Index
Dim TEMP
Dim NextElement
NextElement = LBound(MyArray) + 1
While (NextElement LBound(MyArray) Then
If nOrder = ASCENDING_ORDER Then
If MyArray(Index) < MyArray(Index - 1) Then
TEMP = MyArray(Index)
MyArray(Index) = MyArray(Index - 1)
MyArray(Index - 1) = TEMP
Index = Index - 1
Else
Exit Do
End If
ElseIf nOrder = DESCENDING_ORDER Then
If MyArray(Index) >= MyArray(Index - 1) Then
TEMP = MyArray(Index)
MyArray(Index) = MyArray(Index - 1)
MyArray(Index - 1) = TEMP
Index = Index - 1
Else
Exit Do
End If
End If
Else
Exit Do
End If
gIterations = gIterations + 1
Loop
NextElement = NextElement + 1
gIterations = gIterations + 1
Wend
End Sub
Sub 查找(ByVal ai As Integer)
Dim i As Integer
Dim b As Boolean
i = sumax / 2
b = False
Do While Not b
If ai < a(i) Then i = i / 2
If ai > a(i) Then i = i + (i / 2)
If ai = a(i) Then b = True
Loop
If b Then MsgBox "ok"
End Sub
看了 对一组无序数据,①设计直接插...的网友还看了以下: