思路:
连续找到区间范围内最小值,然后排入到指定位置
时间复杂度为O(n^2)
代码
import java.util.Arrays; public class Client { private static void selectSort(int[] arr){ for (int i = 0; i < arr.length; i++) { int lowestIndex = i; for (int j = i + 1; j < arr.length; j++) { if(arr[j] < arr[lowestIndex]){ lowestIndex = j; } } if(lowestIndex != i){ int temp = arr[i]; arr[i] = arr[lowestIndex]; arr[lowestIndex] = temp; } } } public static void main(String[] args) { int[] arr= {4,5,55,3,22,4,6,7,43,7,89,0}; selectSort(arr); System.out.println(Arrays.toString(arr)); } }