public class SelectionSort {
public static void sort(int[] arr){
for (int i = 0; i < arr.length; i++) {
int k = i;
for (int j = i; j < arr.length; j++) {
if (arr[j] < arr[k]) {
k = j;
}
}
int temp = arr[k];
arr[k] = arr[i];
arr[i] = temp;
}
}
public static void main(String[] args) {
int[] arr = new int[] { 23, 98, 45, 2, 24, 64, 12, 1 };
sort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
System.out.print(" ");
}
}
}
网友评论