java 数组排序

package com.pl;

import java.util.Arrays;

public class array_3 {
    public static void main(String[] args) {
        //数组扩容
        int[] a = {10, 20, 30, 40, 50};
        int[] newA = new int[a.length * 2];

//        for (int i = 0; i < a.length; i++) {
//            newA[i] = a[i];
//        }

//        //newA = new int[6]; // (a.length * 2) + 1
//        System.arraycopy(a, 0, newA, 0, a.length);  //复制数组
//        a = newA;

        a = Arrays.copyOf(a, a.length + 2);

        for (int i = 0; i < a.length; i++) {
            System.out.printf("\n" + a[i]);
        }

        System.out.printf("\n================冒泡排序====================");

        //冒泡排序
        int[] ax = {10, 20, 30, 40, 50, 90, 100};
        for (int i = 1; i < ax.length; i++) {  //轮数
            for (int x = 0; x < ax.length - i; x++) {  //次数  i1=下标
                if (ax[x + 1] > ax[x]) {  //从大到小>   从小到大<
                    int temp = ax[x + 1];
                    ax[x + 1] = ax[x];
                    ax[x] = temp;
                }
            }
        }
        for (int i = 0; i < ax.length; i++) {
            System.out.printf("\n" + ax[i]);
        }

        System.out.printf("\n================选择排序====================");
        //选择排序
        int[] ah = {10, 20, 30, 40, 50, 90, 100, 20, 30, 40, 50};
        for (int i = 0; i < ah.length - 1; i++) {  //轮数
            for (int x = i + 1; x < ah.length; x++) {  //次数  i1=下标
                if (ah[x] > ah[i]) {  //从大到小>   从小到大<
                    int temp = ah[x];
                    ah[x] = ah[i];
                    ah[i] = temp;
                }
            }
        }
        for (int i = 0; i < ah.length; i++) {
            System.out.printf("\n" + ah[i]);
        }

        System.out.printf("\n================JDK 排序====================");
        //JDK 排序只能从小到大
        int[] ahf = {10, 20, 30, 40, 50, 90, 100, 20, 30, 40, 50};
        Arrays.sort(ahf);
        for (int i = 0; i < ahf.length; i++) {
            System.out.printf("\n" + ahf[i]);
        }

        
    }
}

你可能感兴趣的:(算法,排序算法,java)