排序组合应用 '不重复排序 Function gg(xm) Dim arr, Temp() As String '声明变量 Dim s%, r% '声明单值变量 On Error Resume Next '启动一个错误处理程序 r = 0 '初值 s = UBound(xm) '最大下标 ReDim arr(s - 1) For i = 0 To s '循环 Temp = Filter(arr, xm(i)) '搜索数组 If UBound(Temp) = -1 Then '如果未找到 r = r + 1 '序号,自增1 ReDim Preserve arr(1 To r) '定义动态数组大小 arr(r) = xm(i) '把姓名复制到数组Arr()中。 ' Debug.Print Arr(r) End If Next gg = arr Debug.Print End Function Sub lll() Dim xm1(), abc(), Gggg() xm1 = Array(2, 0, 3, 1, 9, 9, 2, 3, 1, 2, 2, 2, 1, 9, 9, 9, 3) abc = gg(xm1) Debug.Print TypeName(abc) Gggg = Bubble_Sort(abc) Debug.Print End Sub Function Bubble_Sort(Ary) Dim aryUBound, i, j aryUBound = UBound(Ary) Debug.Print aryUBound Debug.Print TypeName(Ary) For ii = 1 To aryUBound Debug.Print Ary(ii) Next ii Debug.Print For i = 1 To aryUBound For j = i + 1 To aryUBound If Ary(i) > Ary(j) Then Swap Ary(i), Ary(j) End If Next Next Bubble_Sort = Ary End Function Function Swap(a, b) Dim tmp tmp = a a = b b = tmp End Function |