排序

快排

public class Solution {
    public void quickSort(int[] a, int left, int right) {
        int index = partition(a, left, right);
        if(left < index - 1) quickSort(a, left, index - 1);
        if(index + 1 < right) quickSort(a, index + 1, right);
    }

    private int partition(int[] a, int left, int right) {
        int pivot = a[left];
        while(left < right) {
            while(left < right && a[right] >= pivot)
                right--;
            if(left < right)
                a[left++] = a[right];
            while(left < right && a[left] <= pivot)
                left++;
            if(left < right)
                a[right--] = a[left];
        }
        a[left] = pivot;
        return left;
    }
}

归并排序

public class MergeSort {
    //递归版
    public void mergeSortRecursion(int[] a, int left, int right) {
        if(left < right) {
            int mid = (left + right) / 2;
            mergeSort(a, tmpArray, left, mid);
            mergeSort(a, tmpArray, mid+1, right);
            merge(a, tmpArray, left, mid, right);
        }
    }
    //非递归版
    public void mergeSortIteriation(int[] a, int len) {
        int left, mid, right;
        for(int i=1; i2) {
            left = 0;
            while(left + i < len) {
                mid = left + i - 1;
                right = mid + i >= len ? len-1, mid+i;
                merge(a, left, mid, right);
                left = right + 1;
            }
        }
    }

    private void merge(int[] a, int left, int mid, int right) {
        int len = right - left + 1;
        int[] tmpArray = new int[len];
        int leftPos = left, rightPos = mid + 1, tmpPos = 0;
        while(leftPos <= mid && rightPos <= right) {
            if(a[leftPos] <= a[rightPos]) {
                tmpArray[tmpPos++] = a[leftPos++];
            }
            else {
                tmpArray[tmpPos++] = a[rightPos++];
            }
        }

        while(leftPos <= mid) {
            tmpArray[tmpPos++] = a[leftPos++];
        }

        while(rightPos <= right) {
            tmpArray[tmpPos++] = a[rightPos++];
        }

        for(int i=left; i<=right; i++) {
            a[i] = tmpArray[i-left];
        }
    }
}

堆排序

public class HeapSort {
    private static void heapSort(int[] a) {
        for(int i=(a.length-1-1)/2; i>=0; i--) {
            perDown(a, i, a.length);
        }

        for(int i=a.length-1; i>0; i--) {
            int tmp = a[i];
            a[i] = a[0];
            a[0] = tmp;
            perDown(a, 0, i);
        }
    }

    private static void perDown(int[] a, int i, int n) {
        int tmp = a[i], child = 2 * i + 1;
        while(child < n) {
            if(child != n - 1 && a[child] < a[child + 1])
                child++;
            if(tmp < a[child]) {
                a[i] = a[child];
            }
            else {
                break;
            }
            i = child;
            child = 2 * i + 1;
        }
        a[i] = tmp;
    }
}

冒泡排序

public class BubbleSort {
    public static void bubbleSort(int[] a) {
        if(a == null || a.length < 2) return;
        for(int i=a.length-1; i>=0; i--) {
            for(int j=0; jif(a[j] > a[j+1]) {
                    int tmp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = tmp;
                }
            }
        }
    }
}

你可能感兴趣的:(基础算法)