那我介绍直接插入排序法: Private Sub Form_Click() Dim a As Variant,i As Integer,j As Integer,temp As Integer a=Array(10,20,35,45,65,21) '用Array函数为变体变量赋值 For i=2 To 6 temp=a(i) j=i-1 Do Until temp<a(j) '第i个元素与第j个元素进行比较 a(j+1)=a(j) j=j-1 Loop a(j+1)=temp Next i Print "按从小到大排序的结果为:" For i=0 To 5 print a(i); Next i End Sub
冒泡排序法:(注意,把所给的数值赋予到定义的数组中) private sub command1_click() str="" for i = 1 to 5 for j=1 to 10-i if a(j)<a(j+1) then temp = a(j) a(j)=a(j+1) a(j+1)=temp end if next j next i for i = 1 to 10 str+str & Cstr(a(i)) & "," next i label2.caption = str end sub 注意:n个数字学要n-1遍扫描,第i遍的时候,比较了n-i遍