在vb中用快速排序法的例子,VB高手改寫快速排序

2021-04-24 14:04:59 字數 5756 閱讀 4180

1樓:vb妮可

private sub command1_click()dim i%, j%, m%, s(9)

randomize

'下面先生成陣列,裡面是10個隨機數

for i = 0 to 9

s(i) = int(rnd * 101)next

print "原陣列:"; join(s)'下面開始排序

for i = 0 to 9

for j = i + 1 to 9

if s(j) < s(i) then m = s(i): s(i) = s(j): s(j) = m

next

next

print "排序後陣列:"; join(s)end sub

2樓:

ls幾個回答都是氣泡排序(時間複雜度o(n^2)),人家要的可是快速排序(時間複雜度o(n log n))

'text1:輸入框,用空格分隔的數字

'text2:輸出框

'command1:排序命令

private sub command1_click()

dim k() as string, num() as double, s as long, l as long, i as long

k = split(text1.text, " ")

l = ubound(k): s = lbound(k): redim num(l)

for i = s to l

num(i) = val(k(i))

next i

call quick_sort(s, l, num)

for i = s to l

k(i) = num(i)

next i

text2.text = join(k, " ")

end sub

'快速排序程式****************************************===

private sub exchange(byref n1 as double, byref n2 as double)

dim t as double

t = n1: n1 = n2: n2 = t

end sub

private function partition(byval p as long, byval r as long, byref a() as double) as long

dim x as double, t as long, i as long, j as long

randomize

t = clng((r - p) * rnd + p)

call exchange(a(r), a(t))

x = a(r): i = p - 1

for j = p to r - 1

if a(j) <= x then i = i + 1: call exchange(a(i), a(j))

next j

call exchange(a(i + 1), a(r))

partition = i + 1

end function

private sub quick_sort(byval p as long, byval r as long, byref a() as double)

if p < r then

dim q as long

q = partition(p, r, a)

call quick_sort(p, q - 1, a)

call quick_sort(q + 1, r, a)

end if

end sub

vb高手改寫快速排序

3樓:聽不清啊

private sub command1_click()print

print "原數列是"

randomize

for i = 1 to 10

a(i) = int(rnd * 90 + 10)print a(i); " ";

next i

print

sort 1, 10

print "排列後的數列是:"

for i = 1 to 10

print a(i); " ";

next i

end sub

sub sort(l, r)

i = l: j = r

m = a((i + j) \ 2)

while i < j

while a(i) < m: i = i + 1: wendwhile a(j) > m: j = j - 1: wendif i <= j then

t = a(i): a(i) = a(j): a(j) = ti = i + 1: j = j - 1

end if

wend

if l < j then sort l, jif i < r then sort i, rend sub

vb**求快速排序的遞迴演算法**?

4樓:匿名使用者

public sub quicksort(byref astrsort() as string, byval lngleft as long, byval lngright as long)

dim i as long

dim j as long

dim temp as string

i = lngleft

j = lngright

temp = astrsort(i)

nextstep: do until i >= j

while (astrsort(j) > temp and j > i)

j = j - 1

end while

if j > i then

astrsort(i) = astrsort(j)

astrsort(j) = temp

i = i + 1

end if

while (astrsort(i) < temp and j > i)

i = i + 1

end while

if j > i then

astrsort(j) = astrsort(i)

astrsort(i) = temp

j = j - 1

end if

loop

if lngleft < i - 1 then quicksort(astrsort, lngleft, i - 1)

if lngright > i + 1 then quicksort(astrsort, i + 1, lngright)

end sub

將陣列的第10到20個元素用快速演算法遞迴排序

quicksort(a, 10, 20)

5樓:匿名使用者

病情分析:

新生兒出生後抵抗力低下,感冒可能和著涼有關,表現為鼻塞、流涕、打噴嚏等症狀,

指導意見:

建議結合當地醫生服用小兒速效感冒顆粒、利巴韋林顆粒,多喂水,根據氣溫適當增減衣服,有發熱時積極藥物退熱,

病情分析:

根據您以上描述初步考慮大概是急性上呼吸道感染指導意見:

可以給予小兒氨酚黃那敏顆粒**,讓寶寶多喝水,家人也要注意預防感冒,若感冒了記得戴上口罩接觸寶寶,祝健康

快速排序的vb程式,**

6樓:飄葉雜談

dim b

dim i as integer, j as integer, t as integer

private sub command1_click()

b = array(8, 5, 6, 3, 5, 9, 10, 2, 1, 4)

print "排序前:"

for i = 0 to 9

print b(i);

next

end sub

private sub command2_click()

for i = 1 to 9

for j = 1 to 10 - i

if b(j - 1) > b(j) then

t = b(j - 1)

b(j - 1) = b(j)

b(j) = t

end if

next j

next i

print

print "排序後:"

for i = 0 to 9

print b(i);

next

end sub

private sub command3_click()

dim a(1 to 10), x as integer

dim low as integer, high as integer

dim flag as integer, mid as integer

for i = 1 to 10

a(i) = b(i - 1)

next

print

print "在"; "a(i)=";

for i = 1 to 10

print a(i);

next

print "中查詢"

high = ubound(a)

low = lbound(a)

x = val(inputbox("輸入要查詢的數"))

flag = 0

do while flag = 0 and high >= low

mid = (high + low) \ 2 '中間的數的下標

if a(mid) = x then

flag = 1

else

if x < a(mid) then

high = mid - 1

else

low = mid + 1

end if

end if

loop

if high < low then

print

print "沒有要找的數"

else

print "要查的數是a("; mid; ")="; x

end if

end sub

private sub command4_click()

for i = 1 to 10

a(i) = val(inputbox("請輸入a(" & i & ")的值"))

print a(i);

next

end sub

7樓:匿名使用者

我在外地,我手裡暫時沒vb,

很想幫你,建議用電子**也就可以了

希望對你有用

快速排序 vb 要每一步的講解 50

8樓:沙慧月

使用選擇排序法

假設值都放在陣列裡

假設有a(0)-a(9)

**for i=0 to 8

for j=i+1 to 9

if a(i)>a(j) then

temp=a(i)

a(i)=a(j)

a(j)=temp

next

next

這樣就可把數從小到大進行排列

9樓:匿名使用者

你的**呢?沒有**怎麼「每一步的講解」?

元素的陣列。隨機所有元素並排序vb編寫

private sub mand1 click randomize dim a 20 as integer for i 1 to 20 a i int rnd 101 100 print a i next i for i 1 to 20 for j 1 to 20 i if a j a j 1 th...

在VB中EOF的作用

在vb中,eof一般用於兩個方面,一是關於檔案操作的一個函式,如果eof 檔案號 為true,說明到達了檔案尾,就是到達了資料 不能再被讀出 的位置。adodb資料庫程式設計中,是一個屬性,比如rs.eof,如果為true表示遊標已經到達查詢的尾部,就是不能再讀出一條資料。在vb中,eof可以用來判...

在vb中怎麼把richtextbox1的內容儲存為文字格式的

jbp22d5f90f0a,你究竟是何方神聖?以jbp打頭的名字上百個,財富值794710,提問數122627,回答數0,提問永遠匿名,從不加分,很少採納,每次一提問就是幾十個問題一起發,舉報管理員也不受理,你是管理員他爹?這個肯定要用到檔案操作的相關知識了.vb如何把檔案內容顯示在文字框中?vb6...