利用集合進行陣列的排序

2022-03-22 01:03:02 字數 4301 閱讀 3996

筆者在學習vb中發現利用集合可以進行排序,優化後比冒泡法快得多。下面是完整的函式**,演示了如何進行陣列的公升序或降序排列。

option explicit

dim x(100) as double

dim y(100) as string

private sub command1_click() '演示數字排序

dim a(100) as double, z(100) as string, i as long

for i = 0 to 100

a(i) = x(i) '複製陣列

z(i) = cstr(x(i)) '轉化為字串陣列

next

msgbox join(z, ","), 64, "原始陣列" '顯示原始順序

numbersort x, "up"

for i = 0 to 100

z(i) = cstr(x(i)) '轉化為字串陣列

next

msgbox join(z, ","), 64, "按數字公升序排序後陣列" '顯示排序結果

numbersort a, "down"

for i = 0 to 100

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

private sub command2_click() '演示字串排序

dim z(100) as string, i as long '複製陣列

for i = 0 to 100

z(i) = y(i)

next

msgbox join(y, ","), 64, "原始陣列" '顯示原始順序

stringsort y, "up"

msgbox join(y, ","), 64, "按字串公升序排序後陣列" '顯示排序結果

stringsort z, "down"

msgbox join(z, ","), 64, "按字串降序排序後陣列" '顯示排序結果

end sub

private sub command3_click() ' 排序計時

dim a(3000) as string, i as long, starttime as long, endtime as long

for i = 0 to 3000

a(i) = chr(int(rnd * 26) + 65) & chr(int(rnd * 26) + 65) & chr(int(rnd * 26) + 65) & chr(int(rnd * 26) + 65) & chr(int(rnd * 26) + 65) & chr(int(rnd * 26) + 65) '生成隨機6字元的字串

next

starttime = timer '計時開始

stringsort a

endtime = timer '計時結束

msgbox "排序共耗時 " & endtime - starttime & " 秒!"

end sub

private sub form_load()

randomize

dim i as long

for i = 0 to 100

x(i) = format(rnd * 1000, "0.00")

y(i) = chr(int(rnd * 26) + 65) & chr(int(rnd * 26) + 65) & chr(int(rnd * 26) + 65)

next

end sub

js進行陣列排序

排序例項 有時候後台返回給前端的資料並不是有序的,這時候就需要前端對所有資料進行排序處理,然後再進行分頁展示,通常我們選擇原生js提供的sort 來對陣列進行排序。sort 方法用於對陣列的元素進行排序,並返回陣列。預設根據字串unicode碼點順序來排序。語法 array.sort fun 引數f...

用bit map進行陣列排序

定義每個byte中有8個bit位 include memory.h define bytesize 8 void setbit char p,int posi p p 0x01 posi bytesize 將該bit位賦值1 return void bitmapsortdemo bufferlen這...

冒泡法進行陣列的排序

作 者 王穎 完成日期 2013 年 11 月 28 日 版 本 號 v1.0 輸入描述 無 問題描述 用冒泡法進行陣列的排序 程式輸出 略 問題分析 略 演算法設計 略 include using namespace std int bubble sort int a,int n 兩個函式bubb...