'插入排序 OptionExplicit Dim Result, I Dim TestData(100) const N =100 Randomize For I =0To N -1 TestData(I) =ROUND(RND() *32768) Next '插入排序 Sub ISort(byRef Array, low, hi) Dim i, j, t For i = low+1To hi t =Array(i) j = i -1 Do If j < low Then ExitDo EndIf IfArray(j) > t Then Array(j+1) =Array(j) j = j -1 Else ExitDo EndIf Loop Array(j+1) = t Next End Sub ISort TestData, 0, N -1 For I =0To N -1 Result = Result & TestData(I) & VbTab Next MsgBox(Result)
'希尔排序 OptionExplicit Dim Result, I Dim TestData(100) const N =100 Randomize For I =0To N -1 TestData(I) =ROUND(RND() *32768) Next Sub SortWithStep(byRef Array, low, hi, stp) Dim s, i, j, t For s =0To stp-1 For i = s+low+stp To hi Step stp t =Array(i) j = i-stp Do If j < s+low Then ExitDo EndIf IfArray(j) > t Then Array(j+stp) =Array(j) j = j - stp Else ExitDo EndIf Loop Array(j+stp) = t Next Next End Sub '希尔排序 Sub ShellSort(byRef Array, low, hi) Dim Step Step =Int((hi-low+1)/2) +1 Do SortWithStep Array, low, hi, Step If Step <1Then ExitDo Else Step =Int(Step/2) EndIf Loop End Sub ShellSort TestData, 0, N -1 For I =0To N -1 Result = Result & TestData(I) & VbTab Next MsgBox(Result)