用Java语言编写一个10位随机数的选择排序
时间: 2023-04-10 08:04:02 浏览: 131
以下是用Java语言编写一个10位随机数的选择排序的代码:
```java
import java.util.Random;
public class SelectionSort {
public static void main(String[] args) {
int[] arr = new int[10];
Random rand = new Random();
System.out.print("原始数组:");
for (int i = 0; i < arr.length; i++) {
arr[i] = rand.nextInt(100);
System.out.print(arr[i] + " ");
}
System.out.println();
selectionSort(arr);
System.out.print("排序后数组:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
public static void selectionSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
if (minIndex != i) {
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
}
}
```
阅读全文