ªð¦^¦Cªí ¤W¤@¥DÃD µo©«

[µo°Ý] VBA ¦r¼Æ±Æ§Ç

[µo°Ý] VBA ¦r¼Æ±Æ§Ç

½Ð°ÝVBA¦³¿ìªk¨Ì¤¤¤å¦r¼Æ¦h¹è°µ±Æ§Ç¶Ü?
­n«ç»ò¼g©O?

¦^´_ 2# s13030029

          ¤W¶Ç¤@­Ó¤u§@ªíÁ|­Ó¨Ò À³¸Ó·|¤ñ¸û®e©ö²z¸Ñ

TOP

¦^´_ 1# s13030029
  1. Public Sub MergeSort(array_in, begin, final, Optional array_temp)
  2.     'array_in: 1 dimension array
  3.     If begin >= final Then Exit Sub
  4.     If IsMissing(array_temp) Then
  5.         ReDim array_temp(LBound(array_in) To UBound(array_in))
  6.     End If
  7.     Dim mid
  8.     mid = (begin + final) \ 2
  9.     MergeSort array_in, begin, mid, array_temp
  10.     MergeSort array_in, mid + 1, final, array_temp
  11.     Merge array_in, begin, mid, final, array_temp
  12. End Sub

  13. Private Sub Merge(ar, begin, mid, final, temp)
  14.     Dim left: left = begin
  15.     Dim right: right = mid + 1
  16.     Dim i: i = begin
  17.     Do
  18.         If left > mid Then Exit Do
  19.         If right > final Then Exit Do
  20.         If CmprOperator(ar(left), ar(right)) Then
  21.             temp(i) = ar(left)
  22.             left = left + 1
  23.         Else
  24.             temp(i) = ar(right)
  25.             right = right + 1
  26.         End If
  27.         i = i + 1
  28.     Loop
  29.     For n = left To mid
  30.         temp(i) = ar(n)
  31.         i = i + 1
  32.     Next
  33.     For n = right To final
  34.         temp(i) = ar(n)
  35.         i = i + 1
  36.     Next
  37.     For n = begin To final
  38.         ar(n) = temp(n)
  39.     Next
  40. End Sub
  41. Private Function CmprOperator(a, b) As Boolean
  42.     If Len(a) <= Len(b) Then
  43.         CmprOperator = True
  44.     Else
  45.         CmprOperator = False
  46.     End If
  47. End Function
½Æ»s¥N½X
  1. Public Sub test()
  2.     Dim ar
  3.     ar = Array("½Ð°Ý", "VBA", "¦³¿ìªk", "¨Ì", "¤¤¤å¦r¼Æ¦h¹è", "°µ", "±Æ§Ç", "¶Ü")
  4.     MergeSort ar, LBound(ar), UBound(ar)
  5.     Debug.Print Join(ar, ",")
  6.     ' Output>> ¨Ì,°µ,¶Ü,½Ð°Ý,±Æ§Ç,VBA,¦³¿ìªk,¤¤¤å¦r¼Æ¦h¹è
  7. End Sub
½Æ»s¥N½X
ªí¹F¤£²M¡BÃD·N¤£©ú½T¡B¨SªþÀɮ׮榡¡B¨S¦³°Q½×°ÝÃDªººA«×~~~~~~¥H¤W·R²ö¯à§U¡C

TOP

¦^´_ 3# Scott090
¦^´_ 4# stillfish00
¨Ì·ÓCÄæ»ö¾¹°µ±Æ§Ç
test.rar (11.32 KB)

TOP

¦^´_ 4# s13030029


    test20190528.rar (13.45 KB)
    ½Ð´ú¸Õ¬O¤£¬O§A­nªº

TOP

¦^´_ 5# Scott090
ÁÂÁÂ~³o¬O§Ú­nªº~

TOP

        ÀR«ä¦Û¦b : §Ú­Ì³Ì¤jªº¼Ä¤H¤£¬O§O¤H¡D¥i¯à¬O¦Û¤v¡C
ªð¦^¦Cªí ¤W¤@¥DÃD