选择排序

  1. 原理:从0索引处开始,依次和后面的元素进行比较,小的元素往前放,经过一轮比较厚,最小的元素就出现在了最小索引处。一共需要经过n-1轮外循环。
public static void main(String[] args) {

  int[] arr = {24, 69, 80, 57, 13};

  for (int i = 0; i < arr.length - 1; i++) {
    for (int j = i + 1; j < arr.length; j++) {
      if (arr[i] > arr[j]) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
      }
    }
  }

  System.out.println(Arrays.toString(arr));

}
Copyright © rootwhois.cn 2021-2022 all right reserved,powered by GitbookFile Modify: 2022-05-18 16:24:00

results matching ""

    No results matching ""