用java实现排序算法

public class Sort {


    private static int[] a = {2, 3, 1, 5, 4};
    public static void main(String args[]) {

        print(a);
        bubbleSort();
        print(a);

        bubbleSort2();
        print(a);
        selectSort();
        print(a);
        insertSort();
        print(a);
        mergeSort();
        print(a);
        shellSort();
        print(a);
        quickSort();
        print(a);
    }

//bubbleSort---------------------------
    private static void bubbleSort() {
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a.length - i - 1; j++) {
                if (a[j] > a[j + 1]) {
                    swap(j, j + 1);
                }
            }
        }

    }

    private static void bubbleSort2() {
        for (int i = a.length - 1; i > 1; i--) {
            for (int j = 0; j < i; j++) {
                if (a[j] > a[j + 1]) {
                    swap(j, j + 1);
                }
            }
        }

    }

//selectSort---------------------------------
    private static void selectSort() {
        for (int i = 0; i < a.length - 1; i++) {
            int min = i;
            for (int j = i + 1; j < a.length - 1; j++) {
                if (a[j] < a[min]) {
                    min = j;
                }
            }
            swap(i, min);
        }

    }

//insertSort------------------------------------
    private static void insertSort() {
        for (int i = 1; i < a.length; i++) {
            int temp = a[i];
            int j = i;
            while (j > 0 && a[j - 1] >= temp) {
                a[j] = a[j - 1];
                --j;
            }
            a[j] = temp;
        }

    }

    // mergeSort--------------------------------------
    private static void mergeSort() {
        int length = a.length;
        int[] temp = new int[length];
        recMergeSort(temp, 0, length - 1);

    }

    private static void recMergeSort(int[] temp, int lowerBound, int upperBound) {
        if (lowerBound == upperBound) {
            return;
        } else {
            int mid = (lowerBound + upperBound) / 2;
            recMergeSort(temp, lowerBound, mid);
            recMergeSort(temp, mid + 1, upperBound);
            merge(temp, lowerBound, mid + 1, upperBound);
        }
    }

    private static void merge(int[] temp, int lowPtr, int higPtr,
                              int upperBound) {
        int j = 0;
        int lowerBound = lowPtr;
        int mid = higPtr - 1;
        int n = upperBound - lowerBound + 1;
        while (lowPtr <= mid && higPtr <= upperBound) {
            if (a[lowPtr] < a[higPtr]) {
                temp[j++] = a[lowPtr++];
            } else {
                temp[j++] = a[higPtr++];
            } while (lowPtr <= mid) {
                temp[j++] = a[lowPtr++];
            } while (higPtr <= upperBound) {
                temp[j++] = a[higPtr++];
            }
            for (j = 0; j < n; j++) {
                a[lowerBound + j] = temp[j];
            }

        }

    }

    //shellSort----------------------------------------
    private static void shellSort() {
        int i, j;
        int temp;
        int h = 1;
        while (h <= a.length / 3) {
            h = h * 3 + 1;
        } while (h > 0) {
            for (i = h; i < a.length; i++) {
                temp = a[i];
                j = i;
                while (j > h - 1 && a[j - h] >= temp) {
                    a[j] = a[j - h];
                    j -= h;
                }
                a[j] = temp;
            }
            h = (h - 1) / 3;
        }
    }

    //quickSort--------------------------------------
    private static int partitionIt(int left, int right, int pivot) {
        int leftPtr = left - 1;
        int rightPtr = right ;
        while (true) {
            while ( a[++leftPtr] < pivot) {
                ;
            } while (rightPtr > left && a[--rightPtr] > pivot) {
                ;
            }
            if (leftPtr >= rightPtr) {
                break;
            } else {
                swap(leftPtr, rightPtr);
            }

        }
        return leftPtr;
    }

    private static void quickSort() {
        recQuickSort(0, a.length - 1);
    }

    private static void recQuickSort(int left, int right) {
        if (right - left <= 0) {
            return;
        } else {
            int pivot = a[right];
            int partition = partitionIt(left, right, pivot);
            recQuickSort(left, partition - 1);
            recQuickSort(partition+1, right);
        }
    }

    //----------------------------------------------
    private static void swap(int i, int j) {
        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;

    }

    private static void print(int[] a) {
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
        System.out.println();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值