...99范围内的整数,分别用选择和冒泡法将数据按从大到小递减的顺序排列...
发布网友
发布时间:2024-10-06 18:02
我来回答
共3个回答
热心网友
时间:2024-10-08 05:02
Private Sub Form_click()
Dim A(1 To 10)
Randomize
For I = 1 To 10
A(I) = Int(Rnd * 90) + 10
Next I
For I = 1 To 9 '选择法
p = I
For J = I + 1 To 10
If A(J) < A(I) Then
p = J
End If
Next J
tem = A(I)
A(I) = A(p)
A(p) = tem
Next I
For I = 1 To 10
A(I) = A(11 - I)
Next I
For I = 1 To 9 '冒泡法
For J = 1 To 10 - I
If A(J) > A(J + 1) Then
tem = A(I)
A(I) = A(p)
A(p) = tem
End If
Next J
Next I
For I = 1 To 10
A(I) = A(11 - I)
Next I
End Sub
这就好了 你试试哦
热心网友
时间:2024-10-08 05:03
这是用Java写的:
import java.util.Scanner;
public class randomSortTest {
int[] ran = new int[99];
public static void main(String[] args) {
randomSortTest rs = new randomSortTest();
rs.randomNumber(10, 99, 99);
System.out.println("是否进行排序? <Y/N>");
String YND = "";
YND = new Scanner(System.in).next();
if (YND.equalsIgnoreCase("Y")) {
do {
System.out.println("升序/降序? <R/D>");
YND = new Scanner(System.in).next();
if (YND.equalsIgnoreCase("R") || YND.equalsIgnoreCase("D")) {
break;
}
} while (true);
if (YND.equalsIgnoreCase("R")) {
rs.ranSortRise();
System.out.println("升序后结果:");
} else if (YND.equalsIgnoreCase("D")) {
rs.ranSortDrop();
System.out.println("降序后结果:");
}
} else {
System.out.println("未结果:");
}
for (int x : rs.ran) {
System.out.println(x);
}
}
public void randomNumber(int rangeStat, int rangeEnd, int number) {
int x = 0;
for (int i = 0; i < number;) {
x = (int) (Math.random() * (rangeEnd + 1));
if (x < rangeStat) {
continue;
}
ran[i] = x;
i++;
}
}
public void ranSortRise() {
int ranSize = ran.length;
for (int k = 0, conversion = 0; k < ranSize; k++) {// 升序
for (int j = 0; j < (ranSize - 1); j++) {
if (ran[j] > ran[j + 1]) {
conversion = ran[j];
ran[j] = ran[j + 1];
ran[j + 1] = conversion;
}
}
}
}
public void ranSortDrop() {
int ranSize = ran.length;
for (int k = 0, conversion = 0; k < ranSize; k++) {// 降序
for (int j = 0; j < (ranSize - 1); j++) {
if (ran[j] < ran[j + 1]) {
conversion = ran[j];
ran[j] = ran[j + 1];
ran[j + 1] = conversion;
}
}
}
}
}
热心网友
时间:2024-10-08 05:00
在窗体上添加一个按钮Command1 ,然后把下面代码复制到窗体代码中
Private Const N = 100 'N个,可以修改N等于多少
Private Sub Command1_Click()
Dim i As Integer
Dim a(1 To N) As Integer
For i = 1 To N
Randomize
a(i) = Int(Rnd * 90 + 10)
Next
Call MaoPaoSort(a)
Cls
For i = 1 To N
If i Mod 10 = 1 Then Print '每行打印10个数据
Print a(i);
Next
End Sub
'冒泡排序函数
Sub MaoPaoSort(a() As Integer)
Dim i As Integer
Dim j As Integer
Dim temp As Integer
For i = LBound(a()) To UBound(a()) - 1
For j = LBound(a()) To UBound(a()) - 1
If a(j) > a(j + 1) Then
temp = a(j)
a(j) = a(j + 1)
a(j + 1) = temp
End If
Next
Next
End Sub