冒泡排序 vb
发布网友
发布时间:2022-06-06 02:00
我来回答
共1个回答
热心网友
时间:2022-07-12 07:05
Option
Explicit
Public
Const
N
=
10
Public
A(N-1)。
以下是form代码:
Private
Sub
Command1_Click()
Dim
I
As
Integer
Print
"显示排序前的A元素"
For
I
=
0
To
N
-
1
A(I)
=
InputBox("请输入数据:")
Print
A(I);
Space(2);
Next
I
End
Sub
Private
Sub
Command2_Click()
Dim
I,
J,
T
As
Integer
For
J
=
0
To
N
-
1
For
I
=
J
To
N
-
1
If
Val(A(I))
<=
Val(A(J))
Then
T
=
A(I)
A(I)
=
A(J)
A(J)
=
T
End
If
Next
I
Next
J
Print
Print
Print
"显示排序后的A元素:"
For
I
=
0
To
N
-
1
Print
A(I);
Next
I
Print
End
Sub
--------------------------------
Private
Sub
Command2_Click()
Dim
I,
J,
T
As
Integer
For
J
=
0
To
N
-
2
For
I
=
0
To
N
-
2
-
J
If
Val(A(I))
>
Val(A(I
+
1))
Then
'这里加val函数,比较大小!
T
=
A(I)
A(I)
=
A(I
+
1)
A(I
+
1)
=
T
End
If
Next
I
Next
J
Print
Print
Print
"显示排序后的A元素:"
For
I
=
0
To
N
-
1
Print
A(I);
Next
I
Print
End
Sub