public class test {
public static void main(String[] args) {
int[] arr = {12, 13, 23, 12, 1, 2, 33, 1, 1, 0};
selectSort(arr);
}
static void selectSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int index = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[index] > arr[j]) {
index = j;
}
}
if (index != i) {
swap(arr, i, index);
}
}
printArr(arr);
}
static void swap(int[] arr, int pre, int after) {
if (arr[pre] > arr[after]) {
int temp = arr[pre];
arr[pre] = arr[after];
arr[after] = temp;
}
}
static void printArr(int[] arr) {
System.out.println("选择排序结果:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
|