jkluio668
11/14/2017 - 5:43 AM

arr_sort1


从第m开始,反转数组的后半部分:
Function reverse_arr(arr,optional m = 1)
    lv = UBound(arr)
    lh = UBound(arr, 2)
    ReDim crr(1 To lv, 1 To lh)
    For i = 0 To Int((lv - m) / 2) -1
        For j = 1 To lh
        tmp1 = crr(m + i, j)
        crr(m + i, j) = crr(lv - i, j)
        crr(lv - i, j) = tmp1
        Next
    Next
    reverse_arr = crr
End Function



'long数组的
Sub t2()
Dim a() As Long
ReDim a(1 To 2)
a(1) = 1
a(2) = 2
Debug.Print a(1.4) '1
Debug.Print a(1.5) '2
End Sub

Sub t3()
Dim a() As Long, n1&, n2&
'Dim a() As Long, n1%, n2%
ReDim a(1 To 4)
n1 = LBound(a)
n2 = UBound(a)
a(1) = 1
a(2) = 2
a(3) = 3
a(4) = 4
Debug.Print a((n1 + n2) * 0.5) '同
Debug.Print a((LBound(a) + UBound(a)) * 0.5) '同
Debug.Print a(2.5) '有时是2,有时是3
End Sub


Public Sub ZSort(r, Key&, Ord)
'r为排序二维数组
'Key为排序关键字,为列号
'Ord:1为升序,其他为降序
Dim a() As Long, n1&, n2&, i&, j&
n1 = LBound(r)
n2 = UBound(r)
ReDim a(n1 To n2)
For i = n1 To n2
  a(i) = i
Next
rt = r
If Ord = 1 Then QSortSx r, Key, a, n1, n2 Else QSortJx r, Key, a, n1, n2
For i = n1 To n2
  For j = 1 To UBound(r, 2)
    r(i, j) = rt(a(i), j)
  Next
Next
End Sub
Private Sub QSortSx(r, Key&, a() As Long, L&, H&)
Dim i&, j&, x, y
i = L
j = H
x = r(a((L + H) * 0.5), Key)
While (i <= j)
  While (r(a(i), Key) < x And i < H)
    i = i + 1
  Wend
  While (x < r(a(j), Key) And j > L)
    j = j - 1
  Wend
  If (i <= j) Then
    y = a(i)
    a(i) = a(j)
    a(j) = y
    i = i + 1
    j = j - 1
  End If
Wend
If (L < j) Then QSortSx r, Key, a, L, j
If (i < H) Then QSortSx r, Key, a, i, H
End Sub
Private Sub QSortJx(r, Key&, a() As Long, L&, H&)
Dim i&, j&, x, y
i = L
j = H
x = r(a((L + H) * 0.5), Key)
While (i <= j)
  While (r(a(i), Key) > x And i < H)
    i = i + 1
  Wend
  While (x > r(a(j), Key) And j > L)
    j = j - 1
  Wend
  If (i <= j) Then
    y = a(i)
    a(i) = a(j)
    a(j) = y
    i = i + 1
    j = j - 1
  End If
Wend
If (L < j) Then QSortJx r, Key, a, L, j
If (i < H) Then QSortJx r, Key, a, i, H
End Sub