编写java程序,使用直接选择排序算法,将数组中的数据按照升序排序,并打印输出。
时间: 2023-05-13 09:06:51 浏览: 63
好的,以下是使用直接选择排序算法对数组进行升序排序的Java程序:
```java
public class SelectionSort {
public static void main(String[] args) {
int[] arr = {5, 2, 8, 3, 9, 1};
selectionSort(arr);
for (int i : arr) {
System.out.print(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;
}
}
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
}
```
输出结果为:1 2 3 5 8 9。