基本排序算法

    @Test
    public void addition_isCorrect() {
        assertEquals(4, 2 + 2);
        int[] b = {49, 38, 65, 97, 76, 23, 17};
//        InsetSort(b);
//        ShellInset(b);
//        SelectSort(b);
//        SelectSort2(b);
//        BuildingHeap(b);
//        HeapSort(b);
//        bubbleSort(b);
//        bubbleSort1(b);
//        bubbleSort2(b);
//        quickSort(b,0,b.length-1);
//        quickSort1(b,0,b.length-1,3);
//        System.out.println();
        for (int i = 0;i a[i]) {
                int j = i - 1;
                int x = a[i];
                a[i] = a[i - 1];
                while (j >= 0 && x < a[j]) {
                    a[j + 1] = a[j];
                    j--;
                }
                a[j + 1] = x;
            }
        }
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + "  ");
        }
    }

    private void ShellInsertSort(int[] a, int d) {

        int len = a.length;
        for (int i = d; i < len; i++) {
            if (a[i - d] > a[i]) {
                int j = i - d;
                int x = a[i];
                a[i] = a[i - d];
                while (j >= 0 && x < a[j]) {
                    a[j + d] = a[j];
                    j = j - d;
                }
                a[j + d] = x;
            }
        }
    }

    private void ShellInset(int[] a) {
        int d = a.length / 2;
        while (d >= 1) {
            ShellInsertSort(a, d);
            d = d / 2;
        }
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + "  -->  ");
        }
    }

    //选择排序 时间复杂度O(n^2)
    private void SelectSort(int[] a) {
        int len = a.length;
        for (int i = 0; i < len; i++) {
            int k = i;
            for (int j = i + 1; j < len; j++) {
                if (a[k] > a[j]) {
                    k = j;
                }
            }
            if (k != i) {
                int temp = a[i];
                a[i] = a[k];
                a[k] = temp;
            }
        }
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + "  --->  ");
        }
    }

    // 选择排序 改进方法,二元排序 
    private void SelectSort2(int[] a){
        int len = a.length;
        int i, j, min, max ,tmp;
        for (i = 0; i <= len/2; i ++) { // i 跑 n / 2 趟排序就会排序完成
            min = i ;max = i; // 先将 min 和 max 都指向待排序的第一个元素
            for (j = i+1; j < len - i; j++) {
                if (a[j] < a[min]) {
                    min = j;

                }
                if (a[j] > a[max]) {
                    max = j;
                    continue;
                }
            }

            int mintemp = a[min];
            System.out.println();
            a[min] = a[i];
            a[i] = mintemp;

            if (max ==i){
                int maxtemp = a[len-i-1];
                a[len-i-1] = a[min];
                a[min] = maxtemp;
            } else {
                tmp = a[len-i-1];
                a[len-i-1] = a[max];
                a[max] = tmp;
            }
        }
        for (int ii = 0; ii < a.length; ii++) {
            System.out.print( "  " +a[ii] + "    ");
        }
    }
    void HeapAdjust(int H[],int s, int length)
    {
        int tmp  = H[s];
        int child = 2*s+1; //左孩子结点的位置。(i+1 为当前调整结点的右孩子结点的位置)
        while (child < length) {
            if(child+1 = 0; --i) {
            HeapAdjust(H,i,length);
        }
        for (int ii = 0; ii < H.length; ii++) {
            System.out.print( "  " +H[ii] + "    ");
        }
    }
    //时间复杂度也为:O(nlogn )
    void HeapSort(int H[])
    {
        //初始堆
        BuildingHeap(H);
        int length = H.length;
        //从最后一个元素开始对序列进行调整
        for (int i = length - 1; i > 0; --i)
        {
            //交换堆顶元素H[0]和堆中最后一个元素
            int temp = H[i]; H[i] = H[0]; H[0] = temp;
            //每次交换堆顶元素和堆中最后一个元素之后,都要对堆进行调整
            HeapAdjust(H,0,i);
        }
        System.out.println();
        for (int ii = 0; ii < H.length; ii++) {

            System.out.print( "  " +H[ii] + "    ");
        }
    }

    void bubbleSort(int[] a){
        int len = a.length;
        for (int i = 0;ia[j+1]){
                    int tmp = a[j];
                    a[j] = a[j+1];
                    a[j+1]= tmp;
                }
            }
        }
        for (int ii = 0; ii < a.length; ii++) {
            System.out.print( "  " +a[ii] + "    ");
        }
    }
    void bubbleSort1(int[] a){
        int i = a.length-1;
        while (i>0){
            int pos = 0;
            for (int j =0;ja[j+1]){
                    pos = j;
                    int tmp = a[j];
                    a[j]= a[j+1];
                    a[j+1]= tmp;
                }
            }
            i = pos;
        }
        for (int ii = 0; ii < a.length; ii++) {
            System.out.print( "  " +a[ii] + "    ");
        }
    }
    void bubbleSort2(int[] a){
        int low =0;
        int hight = a.length-1;
        int j ,tmp;
        while (lowa[j+1]){
                    tmp = a[j];
                    a[j]=a[j+1];
                    a[j+1]= tmp;
                }
            }
            hight--;
            for (j=hight;j>low;j--){
                if (a[j]=tmp){
                    r--;
                }
                a[l]=a[r];
                while (lk){
            int l = left;
            int r = right;
            int tmp = a[l];
            while (l=a[l]){
                    l++;
                }
                a[r]= a[l];
            }
            a[l]=tmp;

            quickSort1(a,left,l-1,k);
            quickSort1(a,l+1,right,k);
        }

        for (int i = 1;ia[i]){
                int j = i-1;
                int temp = a[i];
                while (j>=0&&a[j]>temp){
                    a[j+1]=a[j];
                    j--;
                }
                a[j+1]= temp;
            }
        }
    }


你可能感兴趣的:(基本排序算法)