- ©«¤l
- 1440
- ¥DÃD
- 40
- ºëµØ
- 0
- ¿n¤À
- 1464
- ÂI¦W
- 0
- §@·~¨t²Î
- Windows 7
- ³nÅ骩¥»
- Excel 2010 & 2016
- ¾\ŪÅv
- 50
- ©Ê§O
- ¨k
- ¨Ó¦Û
- ¥xÆW
- µù¥U®É¶¡
- 2020-7-15
- ³Ì«áµn¿ý
- 2024-8-22
|
¥»©«³Ì«á¥Ñ Andy2483 ©ó 2022-12-26 13:56 ½s¿è
¦^´_ 11# singo1232001
ÁÂÁ«e½ú«ü¾É
«á¾ÇÂǦ¹©«²ß±o°}¦C±Æ§Ç¬ÛÃöª¾ÃÑ,½Ð«e½ú¦A«ü¾É
°õ¦æµ²ªG:
µ{¦¡½X¤ß±oµù¸Ñ¦p¤U:
Option Explicit
Sub SortArray()
Dim Arr, Y, i
'¡ô«Å§iÅܼÆ:Arr,Y,i¬O³q¥ÎÅܼÆ
Arr = Array("4A", "5A", "6A", "7A", "2A", "4A", "6A", "1A", "2A", "3A")
'¡ô¥OArr¬O¤@ºû°}¦C
Set Y = CreateObject("Scripting.Dictionary")
'¡ô¥OY¬O ¦r¨å
For Each i In Arr
'¡ô³]¶¶°j°é!¥Oi¬O Arr°}¦C¸Ìªº¤@û,±q«e±½ü¨ì«á±
Y(i) = ""
'¡ô¥O¥HiÅܼÆȬ°key,item¬OªÅ¦r¤¸,©ñ¤JY¦r¨å¤¤
Next
Arr = Y.KEYS()
'¡ô¥OArr˽Õì¨Ó°}¦CÈ,´«¸ËY¦r¨åkey²Õ¦¨ªº°}¦CÈ
QuickSort Arr, LBound(Arr), UBound(Arr)
'¡ôµ¹QuickSort³o¦Ûq¨ç¼Æ3Ó ÅܼƥO¨ä¦^¶ÇÈ
'1.arÅܼÆ: Arr°}¦C
'2.iLo&: LBound(Arr):Arr°}¦C³Ì¤p¯Á¤Þ¸¹¼Æ
'3.iHi&: UBound(Arr):Arr°}¦C³Ì¤j¯Á¤Þ¸¹¼Æ
Debug.Print Join(Arr, ",")
'¡ô¦b§Y®É¹Bºâµ¡Åã¥Ü: ¥H","²Å¸¹³sµ² Arr°}¦CȪº¦r¦êµ²ªG
End Sub
Function QuickSort(ByRef ar, ByVal iLo As Long, ByVal iHi As Long)
'«Å§iar¬O³q¥Î«¬ÅܼÆ,ByRef_«ü©w¶Ç»¼¤Þ¼Æªº¤è¦¡¬O©I¥sµ{¦¡¥i¥HÅܧó©I¥sµ{¦¡¤º¤Þ¼Æ°ò¦ªºÅܼÆÈ¡C
'«Å§iiLo, iHi ¬Oªø¾ã¼Æ,ByVal«ü©w¤Þ¼Æ¬O¥H ¶ÇȤ覡¶Ç»¼
Dim pivot As Variant, tmp As Variant, lo As Long, hi As Long
'¡ô«Å§iÅܼÆ:(pivot,tmp)¬O³q¥Î«¬ÅܼÆ,(lo,hi)¬Oªø¾ã¼Æ
lo = iLo
'¡ô¥Olo³o³q¥Î«¬ÅܼƬOiLoÅܼƥNªºÈ (Arr°}¦C³Ì¤p¯Á¤Þ¸¹¼Æ)
hi = iHi
'¡ô¥Ohi³o³q¥Î«¬ÅܼƬOiHiÅܼƥNªºÈ (Arr°}¦C³Ì¤j¯Á¤Þ¸¹¼Æ)
pivot = UCase(ar((lo + hi) \ 2))
'¡ô¥Opivot¬O ((Arr°}¦C³Ì¤p¯Á¤Þ¸¹¼Æ+³Ì¤j¯Á¤Þ¸¹¼Æ)/2 ¥h§À¾ã¼ÆÈ)¯Á¤Þ¸¹ªºar°}¦CȦAÂର¤j¼g^¤å¦r
While lo <= hi
'¡ô³]·íloÅܼÆ<= hiÅÜ¼Æ ±ø¥ó¦¨¥ß°õ¦æªº°j°é
While (UCase(ar(lo)) < pivot And lo < iHi)
'¡ô³]·íloÅܼư}¦CȦAÂà¤j¼g < pivotÅܼÆ,¦Ó¥BloÅÜ¼Æ < iHiÅܼÆ,±ø¥ó¦¨¥ß°õ¦æªº°j°é
lo = lo + 1
'¡ô¥OloÅܼƲ֥[ 1
Wend
While (UCase(ar(hi)) > pivot And hi > iLo)
'¡ô³]·íhiÅܼư}¦CȦAÂà¤j¼g > pivotÅܼÆ,¦Ó¥BhiÅÜ¼Æ > iLoÅܼÆ,±ø¥ó¦¨¥ß°õ¦æªº°j°é
hi = hi - 1
'¡ô¥OhiÅܼƳv¦¸´î 1
Wend
If lo <= hi Then
'¡ô¦pªG loÅÜ¼Æ <= hiÅܼÆ
tmp = ar(lo)
'¡ô¥Otmp³o³q¥Î«¬ÅܼƬO loÅܼƯÁ¤Þ¸¹ªºar°}¦CÈ
ar(lo) = ar(hi)
'¡ô¥OloÅܼƯÁ¤Þ¸¹ªºar°}¦CȬO hiÅܼƯÁ¤Þ¸¹ªºar°}¦CÈ
ar(hi) = tmp
'¡ô¥OhiÅܼƯÁ¤Þ¸¹ªºar°}¦CȬO tmp³o³q¥Î«¬ÅܼÆÈ
lo = lo + 1
'¡ô¥OloÅܼƲ֥[ 1
hi = hi - 1
'¡ô¥OhiÅܼƳv¦¸´î 1
tmp = ""
'¡ô¥Otmp³o³q¥Î«¬ÅܼƬO ªÅ¦r¤¸
End If
Wend
If hi > iLo Then QuickSort ar, iLo, hi
'¡ô¦pªGhiÅÜ¼Æ > iLoÅܼÆ,µ¹QuickSort³o¦Ûq¨ç¼Æ3Ó ÅܼƥO¨ä¦^¶ÇÈ
'1.arÅܼÆ: ar°}¦C
'2.iLo&: iLoÅܼÆ
'3.iHi&: hiÅܼÆ
If lo < iHi Then QuickSort ar, lo, iHi
'¡ô¦pªGloÅÜ¼Æ < iHiÅܼÆ,µ¹QuickSort³o¦Ûq¨ç¼Æ3Ó ÅܼƥO¨ä¦^¶ÇÈ
'1.arÅܼÆ: ar°}¦C
'2.iLo&: loÅܼÆ
'3.iHi&: iHiÅܼÆ
End Function |
|