八大排序(二)-------直接选择排序

个人觉得 直接选择是最简单的一种排序了

步骤

  1. 从待排序序列中,找到最小的元素;
  2. 如果最小元素不是待排序序列的第一个元素,将其和最后一个元素互换;
  3. 从余下的 N - 1 个元素中,找出关键字最小或最大的元素,重复(1)、(2)步,直到排序结束。

第一个数和第二个,第三个..最后一个数比较,  每次比较选出最大或最小的放在最后。内循环次数逐渐减少 

是不稳定的排序 时间复杂度也比较高 o(n2)

public class ZJXuanZhe {
    int[] arr = {15, 14, 26, 7, 9, 22, 5, 8, 11,17};
  
    public static void main(String[] args) {

        ZJXuanZhe xz = new ZJXuanZhe();
        xz.zjxuanzhe();

        xz.prin();



    }
    void zjxuanzhe(){
        int i = 0;
        int j = 0;
        int temp = 0;
        for(i=0;i<10;i++){
            for(j = i;j <10;j++){

                if(arr[i]>arr[j]){
                    temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;


                }
            }

        }

    }

    void prin(){
        for (int i = 0 ; i < 10 ; i++){
            System.out.println(arr[i]);
        }

    }

    void te(){
        int [] a = new int[11];
        //默认定义的就是0开始
        for (int i = 0 ;i<a.length;i++)
            System.out.println(a[i]);




    }

}

猜你喜欢

转载自www.cnblogs.com/cnng/p/12329580.html