选择排序

   public static void main(String[] args) {
        int[] a = {1, 4, 6, 2, 15, 24, 67, 45, 23, 99, 12, 8};
        selectSort(a);
        System.out.println(Arrays.toString(a));
    }

    public static void selectSort(int[] a) {
        int size = a.length;
        for (int i = 0; i < size - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < size; j++) {
                if (a[j] < a[minIndex]) {
                    minIndex = j;
                }
            }
            int tmp = a[i];
            a[i] = a[minIndex];
            a[minIndex] = tmp;
        }
    }

  

posted @ 2024-11-28 15:00  活出自己范儿  Views(2)  Comments(0Edit  收藏  举报