1.快速排序

1.冒泡排序

基本思想:(从小到达排序)-把最大的不断向右边移动    时间复杂度:O(N2)

public static void sort(int[] arrays){
   for(int i=0;i<arrays.length;i++){
            for(int j=0;j<arrays.length-1-i;j++){//减去i是因为前i个已经
                if(arrays[j]>arrays[j+1]){
                    int temp=arrays[j];
                    arrays[j]=arrays[j+1];
                    arrays[j+1]=temp;
          } } } } }

时间复杂度:O(N2)

空间复杂度:O(1)

2.快速排序

    public void quickSort(int[] arrays,int left,int right){
        if(left<right){
            int l=left;
            int r=right;
            int temp=arrays[l];//分界点

            while(l<r){
                while(l<r&&arrays[r]>temp){
                    r--;
                }
                if(l<r){
                    arrays[l++]=arrays[r];
                }

                while(l<r&&arrays[l]<temp){
                    l++;
                }
                if(l<r){
                    arrays[r--]=arrays[l];
                }
            }
            arrays[l]=temp;
            quickSort(arrays,left,l-1);
            quickSort(arrays,l+1,right);
        }
    }

 

时间复杂度:最坏:O(n2)   最好:O(nlogn) 

空间复杂度:最坏: O(n)     最好:O(logn)

2.1随机化快速排序 

由于快速排序在输入为顺序的时候时间复杂度是O(n2),为了使的计算与输入没有关系,有一种随机化快速排序的算法

public void randomQuickSort(int[] arrays,int left,int right){
        Random random=new Random();
        int randomIndex=arrays[random.nextInt()%(right-left)+left];
        int temp=arrays[randomIndex];
        arrays[randomIndex]=arrays[left];
        arrays[left]=temp;
        quickSort(arrays,left,right);
}

分析这种随机化选分点的方法可以避免顺序输入的问题,因为不会每次的分点都是实在顺序的基础上选择的

但是对于全部输入都是一样的情况,随机化快速排序也无能为力

2.2三数取中快速排序 

计算mid的下标

mid=left+(right-left)/2;

选arrays[left]、array[right]、array[mid]中第二大的数字为分界点,这种方法比随机化快速排序要好,除非输入的序列是完全一样的,否则一定不会出现顺序序列的情况

public void threepointQuickSort(int[] arrays,int left,int right){
        int mid=left+(right-left)/2;
        if(arrays[mid]>arrays[right]){
            int temp=arrays[mid];
            arrays[left]=arrays[right];
            arrays[right]=temp;
        }

        if(arrays[left]>arrays[right]){
            int temp=arrays[left];
            arrays[left]=arrays[right];
            arrays[right]=temp;
        }

        if(arrays[left]<arrays[mid]){
            int temp=arrays[left];
            arrays[left]=arrays[mid];
            arrays[mid]=temp;
        }
        quickSort(arrays,left,right);
}

2.3三数取中+插入排序

代码略----分割到一定程度之后使用快速排序,参考文献中说数量比较小的时候,插入排序的效率会比快速排序的效率高

2.4忽略与中轴相等数字的快速排序

    public void threePointQuickUpdate(int arrays[],int left,int right) {
        if (left < right) {
            int l = left + 1;
            int r = right;
            int move = left;
            int temp = arrays[left];

            while (l <= r) {
                if (arrays[l] < temp) {
                    swap(arrays, l++, move++);
                } else if (arrays[l] > temp) {
                    swap(arrays, l, r--);
                } else
                    l++;
            }

            threePointQuickUpdate(arrays, left, move - 1);
            threePointQuickUpdate(arrays, r+1, right);
        }
    }

 

参考文献

http://blog.csdn.net/insistgogo/article/details/7785038

 

  

 

posted @ 2017-03-08 13:06  疯狂的肉包  阅读(199)  评论(0编辑  收藏  举报