Java大神们~急!!! java编程:构造一个包含若干随机数的整数数组并将他们排序,随机数的范围
发布网友
发布时间:2022-05-02 04:15
我来回答
共1个回答
热心网友
时间:2023-10-09 10:53
public class Demo {
public static void main(String[] args) {
int a[] = random(5, 10);
System.out.print("排序前\t");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+"\t");
}
System.out.println("\n");
selectsort(a);
System.out.print("排序后\t");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+"\t");
}
}
public static int[] random(int n, int max) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = (int) (Math.random() * max);
}
return a;
}
public static void selectsort(int arr[]) {
for (int j = 0; j < arr.length - 1; j++) {
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i + 1]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
}
}