排序

因为课程实验的关系要写排序,心血来潮看看自己能写多少个。再到后来在维基上看看有哪些排序,不知不觉写了十多个(有些排序纯碎在卖萌)。。

现在一次过贴个代码先,以后再分类。分别有冒泡排序、选择排序、插入排序、归并排序、快速排序、鸡尾酒排序(摇晃排序或双向冒泡排序)、堆排序、奇偶排序、梳排序、臭皮匠排序(Stooge Sort)、Bogo Sort、 Bozo Sort和希尔排序。

import java.util.Random;
import java.util.Arrays;

public class Sort{
    private static void swap(int[] A, int i, int j){
        int tmp = A[i];
        A[i] = A[j];
        A[j] = tmp;
    }

    public static void bubbleSort(int[] A){
        boolean unsorted = true;
        int top = A.length-1;
        while(unsorted){
            unsorted = false;
            for(int i = 0; i < top; i ++){
                if(A[i] > A[i+1]){
                    swap(A, i, i+1);
                    unsorted = true;
                }
            }
            top --;
        }
    }

    public static void selectionSort(int[] A){
        for(int i = 0; i < A.length-1; i ++){
            int k = i;
            for(int j = i+1; j < A.length; j ++){
                if(A[j] < A[k])k = j;
            }
            if(k != i){
                swap(A, i, k);
            }
        }
    }

    public static void insertionSort(int[] A){
        for(int i = 1; i < A.length; i ++){
            for(int j = i-1; j >= 0 && A[j] > A[j+1]; j --){
                swap(A, j, j+1);
            }
        }
    }

    public static void mergeSort(int[] A){
        mergeSortDo(A, 0, A.length-1);
    }

    private static void mergeSortDo(int[] A, int low, int high){
        if(low < high){
            int mid = (low + high) / 2;
            mergeSortDo(A, low, mid);
            mergeSortDo(A, mid+1, high);
            merge(A, low, mid, high);
        }    
    }

    private static void merge(int[] A, int low, int mid, int high){
        int i = low, j = mid + 1, k = 0;
        int[] tmp = new int[high - low + 1];
        while(i <= mid && j <= high){
            if(A[i] < A[j]){
                tmp[k ++] = A[i ++];
            }
            else{
                tmp[k ++] = A[j ++];
            }
        }
        while(i <= mid){
            tmp[k ++] = A[i ++];
        }
        while(j <= high){
            tmp[k ++] = A[j ++];
        }
        System.arraycopy(tmp, 0, A, low, tmp.length);
    }

    public static void quickSort(int[] A){
        quickSortDo(A, 0, A.length-1);
    }

    private static void quickSortDo(int[] A, int low, int high){
        if(low < high){
            int key = partition(A, low, high);
            quickSortDo(A, low, key-1);
            quickSortDo(A, key+1, high);
        }
    }

    private static int partition(int[] A, int low, int high){
        int key = low;
        Random rand = new Random();
        int k = rand.nextInt(high-low+1) + low;
        swap(A, k, low);
        int x = A[key];
        for(int i = low+1; i <= high; i ++){
            if(A[i] < x){
                key ++;
                if(i != key){
                    swap(A, i, key);
                }
            }
        }
        swap(A, low, key);
        return key;
    }

    public static void shakerSort(int[] A){
        boolean unsorted = true;
        int top = A.length-1;
        int bottom = 0;
        while(unsorted){
            unsorted = false;
            for(int i = bottom; i < top; i ++){
                if(A[i] > A[i+1]){
                    swap(A, i, i+1);
                    unsorted = true;
                }
            }
            top --;
            for(int i = top; i > bottom; i --){
                if(A[i] < A[i-1]){
                    swap(A, i, i-1);
                    unsorted = true;
                }
            }
            bottom ++;
        }
    }

    public static void heapSort(int[] A){
        makeHeap(A);
        for(int i = A.length-1; i > 0; i --){
            swap(A, 0, i);
            siftDown(A, i, 0);
        }
    }

    private static void siftDown(int[] A, int len, int i){
        while(2*i+1 <= len-1){
            i = 2*i+1;
            if(i+1 <= len-1 && A[i] < A[i+1]) i ++;
            if(A[(i-1)/2] < A[i])
                swap(A, (i-1)/2, i);
            else
                break;
        }
    }

    private static void makeHeap(int[] A){
        //max heap
        for(int i = (A.length-1-1)/2; i >= 0; i --){
            siftDown(A, A.length, i);
        }
    }

    public static void oddEvenSort(int[] A){
        boolean unsorted = true;
        while(unsorted){
            unsorted = false;
            for(int i = 1; i < A.length-1; i += 2){
                if(A[i] > A[i+1]){
                    swap(A, i, i+1);
                    unsorted = true;
                }
            }
            for(int i = 0; i < A.length-1; i += 2){
                if(A[i] > A[i+1]){
                    swap(A, i, i+1);
                    unsorted = true;
                }
            }
        }
    }

    public static void combSort(int[] A){
        int gap = A.length;
        boolean unsorted = true;
        while(gap > 1 || unsorted){
            if(gap > 1)    {
                gap = (int)(gap/1.3);        //The best rate is 1.3
            }
            unsorted = false;
            for(int i = 0; i+gap < A.length; i ++){
                if(A[i] > A[i+gap]){
                    swap(A, i, i+gap);
                    unsorted = true;
                }
            }
        }
    }

    public static void stoogeSort(int[] A){
        stoogeSortDo(A, 0, A.length-1);
    }

    private static void stoogeSortDo(int[] A, int low, int high){
        if(A[low] > A[high])
            swap(A, low, high);
        if(high-low+1 >= 3){
            int split = (high-low+1) / 3;
            stoogeSortDo(A, low, high - split);
            stoogeSortDo(A, low + split, high);
            stoogeSortDo(A, low, high - split);
        }
    }

    public static void bogoSort(int[] A){
        while(!inOrder(A))
            shuffle(A);
    }

    private static boolean inOrder(int[] A){
        for(int i = 0; i < A.length-1; i ++){
            if(A[i] > A[i+1])return false;
        }
        return true;
    }

    private static void shuffle(int[] A){
        Random rand = new Random();
        for(int i = 0; i < A.length; i ++){
            int tmp = rand.nextInt(A.length);
            swap(A, i, tmp);
        }
    }

    public static void bozoSort(int[] A){
        Random rand = new Random();
        while(!inOrder(A)){
            int i = rand.nextInt(A.length);
            int j = rand.nextInt(A.length);
            swap(A, i, j);
        }
    }

    public static void shellSort(int[] A){
        int gap = 1;
        while(gap < A.length/3)
            //by Knuth
            gap = gap * 3 + 1;
        for(; gap >= 1; gap /= 3){
            for(int i = gap; i < A.length; i ++){
                for(int j = i-gap; j >= 0 && A[j] > A[j+gap]; j -= gap){
                    swap(A, j, j+gap);
                }
            }
        }
    }
}
View Code

posted @ 2013-10-20 14:51  7hat  阅读(214)  评论(0编辑  收藏  举报