option explicit
private sub command1_click() '演示數字排序
dim a(10) as double, z(10) as string, i as long
dim x(10) as double
x(0) = 10
x(1) = 2
x(2) = 3
x(3) = 1
x(4) = 0
x(5) = 3
x(6) = 4
x(7) = 4
x(8) = 56
for i = 0 to 10
a(i) = x(i) '複製陣列
z(i) = cstr(x(i)) '轉化為字串陣列
next
msgbox join(z, ","), 64, "原始陣列" '顯示原始順序
numbersort x, "up"
for i = 0 to 10
z(i) = cstr(x(i)) '轉化為字串陣列
next
msgbox join(z, ","), 64, "按數字公升序排序後陣列" '顯示排序結果
numbersort a, "down"
for i = 0 to 10
z(i) = cstr(a(i)) '轉化為字串陣列
next
msgbox join(z, ","), 64, "按數字降序排序後陣列" '顯示排序結果
end sub
sub numbersort(byref a() as double, optional sort as string = "up") '數字排序
dim min as long, max as long, num as long, first as long, last as long, temp as long, all as new collection, steps as long
min = lbound(a)
max = ubound(a)
all.add a(min)
steps = 1
for num = min + 1 to max
last = all.count
if a(num) < cdbl(all(1)) then all.add a(num), before:=1: goto nextnum '加到第一項
if a(num) > cdbl(all(last)) then all.add a(num), after:=last: goto nextnum '加到最後一項
first = 1
do while last > first + 1 '利用do迴圈減少迴圈次數
temp = (last + first) / 2
if a(num) > cdbl(all(temp)) then
first = temp
else
last = temp
steps = steps + 1
end if
loop
all.add a(num), before:=last '加到指定的索引
nextnum:
steps = steps + 1
next
for num = min to max
if sort = "up" or sort = "up" then a(num) = cdbl(all(num - min + 1)): steps = steps + 1 '公升序
if sort = "down" or sort = "down" then a(num) = cdbl(all(max - num + 1)): steps = steps + 1 '降序
next
msgbox "本陣列共經過 " & steps & "步實現" & iif(sort = "up" or sort = "up", "公升序", "降序") & "排序!", 64, "information"
set all = nothing
end sub
sub stringsort(byref a() as string, optional sort as string = "up") '字串排序
dim min as long, max as long, num as long, first as long, last as long, temp as long, all as new collection, steps as long
min = lbound(a)
max = ubound(a)
all.add a(min)
steps = 1
for num = min + 1 to max
first = 1
last = all.count
if a(num) < all(1) then all.add a(num), before:=1: goto nextnum '加到第一項
if a(num) > all(last) then all.add a(num), after:=last: goto nextnum '加到最後一項
do while last > first + 1 '利用do迴圈減少迴圈次數
temp = (last + first) / 2
if a(num) > all(temp) then
first = temp
else
last = temp
steps = steps + 1
end if
loop
all.add a(num), before:=last '加到指定的索引
nextnum:
steps = steps + 1
next
for num = min to max
if sort = "up" or sort = "up" then a(num) = all(num - min + 1): steps = steps + 1 '公升序
if sort = "down" or sort = "down" then a(num) = all(max - num + 1): steps = steps + 1 '降序
next
msgbox "本陣列共經過 " & steps & "步實現" & iif(sort = "up" or sort = "up", "公升序", "降序") & "排序!", 64, "information"
set all = nothing
end sub
難道這就是傳說中的多關鍵字排序 獎學金
題目描述 期末,每個學生都有3門課的成績 語文 數學 英語。先按總分從高到低排序,如果兩個同學總分相同,再按語文成績從高到低排序,如果兩個同學總分和語文成績都相同,那麼規定學號小的同學排在前面,這樣,每個學生的排序是唯一確定的。任務 先根據輸入的3門課的成績計算總分,然後按上述規則排序,最後按排名順...
二叉樹排序,比選擇排序,氣泡排序快很多
初始化乙個長度是100000的隨機數字的陣列 初始化完畢 接下來分別用3種演算法進行排序 選擇法排序,一共耗時15477毫秒 冒泡法排序,一共耗時15801毫秒 二叉樹排序,一共耗時92毫秒 檢視排序結果,是否是不同的陣列物件 i 7d4991ad i 28d93b30 i 1b6d3586 檢視排...
今晚看到了傳說中的方立勳老師
今天晚上自習,看到了傳說中的方立勳老師,胖胖的,也有幾分可愛,和老畢長差不多感覺,不過老畢更可愛些。方老師今晚說了,學校現在和外面的公司開始搞合作,以後有可能可以7k把我們批發走。嗯,主要說了要我們要買個筆記本,每天要寫總結,每天前面10分鐘不講課,用來抽乙個人講自己的總結,如果講不出來就要掃地。以...