java常用排序算法极简汇总

public static int[] bubbleSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
        return array;
  }
public static int[] selectSort(int[] ints) {
        for (int i = 0; i < ints.length - 1; i++) {
            for (int j = i + 1; j < ints.length; j++) {
                if (ints[i] > ints[j]) {
                    int temp = ints[i];
                    ints[i] = ints[j];
                    ints[j] = temp;
                }
            }
        }
        return ints;
 }

public static int[] insertSort(int[] array) {
        int current, preIndex;
        for (int i = 0; i < array.length - 1; i++) {
            current = array[i + 1];
            preIndex = i;
            while (preIndex >= 0 && array[preIndex] > current) {
                array[preIndex + 1] = array[preIndex];
                preIndex--;
            }
            array[preIndex + 1] = current;
        }
        return array;
 }
public static void quickSort(int[] array, int low, int hight) {
        if (low >= hight)
            return;
        int i = low;
        int j = hight;
        int KeyValue = array[low];
        while (i < j) {
            while (i < j && array[j] > KeyValue) {
                j--;
            }
            while (i < j && array[i] <= KeyValue) {
                i++;
            }
            if (i < j) {
                int temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }

        }
        array[low] = array[i];
        array[i] = KeyValue;
        quickSort(array, low, i - 1);
        quickSort(array, i + 1, hight);
 }
public static int[] wrapQuickSort(int[] array) {
        quickSort(array, 0, array.length - 1);
        return array;
 }
 public static int[] shellSort(int[] array) {
        int len = array.length;
        while (len != 0) {
            len = len / 2;
            for (int i = 0; i < len; i++) {
                for (int j = i + len; j < array.length; j += len) {
                    int k = j - len;
                    int temp = array[j];
                    while (k >= 0 && temp < array[k]) {
                        array[k + len] = array[k];
                        k -= len;
                    }
                    array[k + len] = temp;
                }
            }
        }
        return array;
 }
public static int[] heapSort(int[] array){
        for(int i=array.length/2-1;i>=0;i--){
            adjustHeap(array,i,array.length);
        }
        for(int j=array.length-1;j>0;j--){
            int temp=array[0];
            array[0] = array[j];
            array[j] = temp;
            adjustHeap(array,0,j);
        }
        return array;

 }
public static void adjustHeap(int[] array,int i,int length){
        int temp = array[i];
        for(int k=i*2+1;k<length;k=k*2+1){
            if(k+1<length && array[k]<array[k+1]){
                k++;
            }
            if(array[k] >temp){
                array[i] = array[k];
                i = k;
            }else{
                break;
            }
        }
        array[i] = temp;
 }
public static int[] getArrayRandomInt() {
        int[] array = new int[20];
        Random random = new Random(Calendar.getInstance().getTimeInMillis());
        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(100) + 1;
            for (int j = 0; j < i; j++) {
                if (array[i] == array[j]) {
                    i--;
                    break;
                }
            }
        }
        return array;
 }
public static void addSeparatorPrintArray(int[] array) {
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
            if (i == array.length - 1)
                System.out.println();
        }
}
public static void main(String[] args) {
        System.out.println("Hello World!");
        int[] array1 = getArrayRandomInt();
        int[] array2 = getArrayRandomInt();
        int[] array3 = getArrayRandomInt();
        int[] array4 = getArrayRandomInt();
        int[] array5 = getArrayRandomInt();
        int[] array6 = getArrayRandomInt();
        System.out.println("---bubbleSort---");
        addSeparatorPrintArray(array1);
        addSeparatorPrintArray(bubbleSort(array1));
        System.out.println("---selectSort---");
        addSeparatorPrintArray(array2);
        addSeparatorPrintArray(selectSort(array2));
        System.out.println("---insertSort---");
        addSeparatorPrintArray(array3);
        addSeparatorPrintArray(insertSort(array3));
        System.out.println("---quickSort---");
        addSeparatorPrintArray(array4);
        addSeparatorPrintArray(wrapQuickSort(array4));
        System.out.println("---shellSort---");
        addSeparatorPrintArray(array5);
        addSeparatorPrintArray(shellSort(array5));
        System.out.println("---heapSort---");
        addSeparatorPrintArray(array6);
        addSeparatorPrintArray(heapSort(array6));
 }

Hello World!
---bubbleSort---
76 73 59 61 66 64 48 24 6 91 39 72 99 3 25 55 81 32 51 84 
3 6 24 25 32 39 48 51 55 59 61 64 66 72 73 76 81 84 91 99 
---selectSort---
28 60 39 66 91 95 30 43 72 3 18 40 87 45 53 77 63 36 9 6 
3 6 9 18 28 30 36 39 40 43 45 53 60 63 66 72 77 87 91 95 
---insertSort---
55 52 32 43 83 47 92 73 75 17 72 66 61 86 23 56 57 12 58 67 
12 17 23 32 43 47 52 55 56 57 58 61 66 67 72 73 75 83 86 92 
---quickSort---
81 92 3 9 70 98 83 87 10 63 62 5 91 53 61 64 96 68 27 100 
3 5 9 10 27 53 61 62 63 64 68 70 81 83 87 91 92 96 98 100 
---shellSort---
4 76 95 12 97 6 30 55 82 40 75 22 94 53 81 2 68 1 42 73 
1 2 4 6 12 22 30 40 42 53 55 68 73 75 76 81 82 94 95 97 
---heapSort---
30 64 13 78 43 48 49 27 15 76 80 70 66 20 33 19 57 37 34 39 
13 15 19 20 27 30 33 34 37 39 43 48 49 57 64 66 70 76 78 80 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值