public class SelectionSort {
public static void main(String[] args) {
int[] array = { 64, 25, 12, 22, 11 };
System.out.println("Original array: " + Arrays.toString(array));
selectionSort(array);
System.out.println("Sorted array: " + Arrays.toString(array));
}
public static void selectionSort(int[] array) {
int n = array.length;
// One by one move boundary of unsorted array
for (int i = 0; i < n - 1; i++) {
// Find the minimum element in unsorted array
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (array[j] < array[minIndex]) {
minIndex = j;
}
}
// Swap the found minimum element with the first element
int temp = array[minIndex];
array[minIndex] = array[i];
array[i] = temp;
}
}
}
java 选择排序
最新推荐文章于 2024-11-15 18:38:47 发布