八大排序之选择排序—简单选择排序(Simple Selection Sort)

  public static void paixu(int a[],int n){
        for (int i=0; i<n; i++){
            for (int j=i+1; j<n; j++){
                if(a[i]>a[j]){
                    int t = a[i];
                    a[i] = a[j];
                    a[j] = t;
                }
            }
            for(int x=0; x<n; x++){
                System.out.print(a[x]+" ");
            }
            System.out.println();
        }
    }

    //简单选择排序的改进——二元选择排序
    public static void paixu1(int b[],int n){
        for(int i=0,j=n-1; i<j; i++,j--){
            int max = b[i], min = b[i], maxs = i, mins = i;
            for(int p=i; p<=j; p++){
                if(b[p]>max){
                    max = b[p];
                    maxs = p;
                }else if(b[p]<min){
                    min = b[p];
                    mins = p;
                }
            }
            System.out.println(max+"  "+min);
            if(b[maxs]>b[j]){
                int t1 = b[maxs];b[maxs] = b[j];b[j] = t1;
            }
            if(b[mins]<b[i]){
                int t2 = b[mins];b[mins] = b[i];b[i] = t2;
            }
            for(int x=0; x<n; x++){
                System.out.print(b[x]+" ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        int a[] = {3,2,5,8,4,7,6,9};
        paixu(a,8);
        int b[] = {3,2,5,8,4,7,6,9};
        paixu1(b,8);
    }

猜你喜欢

转载自blog.csdn.net/qq_40835338/article/details/81478708