給你一個(gè)最簡(jiǎn)單的冒泡排序代碼:
創(chuàng)新互聯(lián)公司-專(zhuān)業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性?xún)r(jià)比東興網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式東興網(wǎng)站制作公司更省心,省錢(qián),快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋東興地區(qū)。費(fèi)用合理售后完善,十年實(shí)體公司更值得信賴(lài)。
將三個(gè)數(shù)放到一個(gè)數(shù)組中。
a(0)=val(text1.text):a(1)=val(text2.text):a(2)=val(text3.text)
dim flag as Boolean,temp as Integer
for i = 0 to 2
flag = true
for j = 2 to 1
if a(j)a(j-1) then
temp = a(j-1)
a(j-1) = a(j)
a(j) = temp
flag = false
end if
next j
if flag then Exit For
next i
text4.text=a(0):text5.text=a(1):text6.text=a(2)
vb2010寫(xiě)的:
Private?Sub?Button7_Click(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?Button7.Click
Dim?str?As?String?=?""
Dim?a(30)?As?Integer
Dim?temp?As?Integer
str?=?str??"生成的30個(gè)隨機(jī)數(shù)字是?"??vbCrLf
For?i?=?1?To?30
a(i)?=?Int(Rnd()?*?50?+?1)
str?=?str??a(i)??"?"
If?i?Mod?10?=?0?Then
str?=?str??vbCrLf
End?If
Next
str?=?str??vbCrLf
For?i?=?1?To?30
For?j?=?1?To?30?-?i
If?a(j)??a(j?+?1)?Then
temp?=?a(j?+?1)
a(j?+?1)?=?a(j)
a(j)?=?temp
End?If
Next?j
Next?i
str?=?str??"排序后的數(shù)字是?"??vbCrLf
For?i?=?1?To?30
str?=?str??a(i)??"?"
If?i?Mod?10?=?0?Then
str?=?str??vbCrLf
End?If
Next
TextBox6.Text?=?str
End?Sub
運(yùn)行結(jié)果:
生成的30個(gè)隨機(jī)數(shù)字是
36 27 29 15 16 39 1 39 41 36
3 21 44 40 19 49 44 3 48 19
27 39 3 30 24 15 32 33 14 14
排序后的數(shù)字是
49 48 44 44 41 40 39 39 39 36
36 33 32 30 29 27 27 24 21 19
19 16 15 15 14 14 3 3 3 1
Private Sub Command1_Click()
Dim a(1 To 100) As Integer
Dim i As Integer, j As Integer, k As Integer
For i = 1 To 100 '給數(shù)組a一百個(gè)元素賦值,并換每行十個(gè)數(shù)字輸出來(lái)窗體上
a(i) = Int(Rnd * 101)
k = k + 1
Print Tab((k - 1) * 5); a(i);
If k = 10 Then k = 0: Print
Next i
For i = 100 To 2 Step -1 '用冒泡排序法對(duì)數(shù)組進(jìn)行排序
For j = 1 To i - 1
If a(j) a(j + 1) Then
t = a(j): a(j) = a(j + 1): a(j + 1) = t
End If
Next j
Next i
For i = 1 To 100 '輸出排好序的數(shù)組
k = k + 1
Print Tab((k - 1) * 5); a(i);
If k = 10 Then k = 0: Print
Next i
End Sub