排序(归并 快速 希尔)

《算法》书上的实现

import edu.princeton.cs.algs4.Stopwatch;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class MySort {
    public static void main(String[] args){
        int[]a =new int[100000000];
        Random r = new Random(47);
        for(int i = 0;i<a.length;i++){
            a[i] =r.nextInt(1000000000);
        }
        Stopwatch timer = new Stopwatch();
        //Sort3.sort(a,0,99);//希尔   2.315    33.489
        Sort2.sort(a);//快速  1.062   11.055 11.743(相同)
        //Sort1.sort1(a);//归并  1.344  14.088
        System.out.println(timer.elapsedTime());
//        int i = 0;
//        for(int n:a){
//            System.out.print(n+" ");
//            i++;
//            if(i%5==0)
//                System.out.println();
//        }
    }
}
归并排序
class Sort1{
    /*归并排序*/
    static int []tp;
    public static void sort(int[]a){
        tp = new int[a.length];
        sort(a,0,a.length-1);
    }
    //自顶向下
    private static void sort(int []a,int low,int high){
        if(high<=low) return;
        int mid = low+(high-low)/2;
        sort(a,low,mid);
        sort(a,mid+1,high);
        merge(a,low,mid,high);
    }
    //自底向上的归并
    public static void sort1(int[]a){
        int N = a.length;
        tp = new int[N];
        for(int sz = 1;sz<N;sz+=sz){
            for(int lo = 0;lo<N-sz;lo+=sz+sz){
                merge(a,lo,lo+sz-1,Math.min(N-1,lo+sz+sz-1));
            }
        }
    }
    private static void merge(int[]a,int low,int mid,int high){
        int i = low;
        int j = mid+1;
        for(int k = low;k<high+1;k++){
            tp[k]=a[k];
        }
        for(int k = low;k<high+1;k++){
            if(i>mid){a[k] = tp[j++];}
            else if(j>high){a[k] = tp[i++];}
            else if(les(tp[i],tp[j])){a[k] = tp[j++];}
            else a[k] = tp[i++];
        }
    }
    private static int compere(int a,int b){
        if(a>b) return 1;
        if(a==b) return 0;
        return -1;
    }
    private static boolean les(int a,int b){
        return compere(a,b)>0;
    }
}
快速排序
class Sort2{
    private static int comp(int a,int b){
        return new Integer(a).compareTo(b);
    }
    /*快速排序*/
    public static void sort(int[]a){
        sort(a,0,a.length-1);
    }
    private static void sort(int[]a,int low,int high){
        if(low>=high) return;
        if(high<=low+15) {Sort3.sort1(a,low,high+1,1);return;}//切分个数小于15时使用插入排序
        int j = partition(a,low,high);
        sort(a,low,j-1);
        sort(a,j+1,high);

//三向切分  重复元素多的情况
//        if(high<=low) return;
//        int lt = low,i = low+1,gt=high;
//        int v = a[low];
//        while(i<=gt){
//            int cmp = comp(a[i],v);
//            if(cmp<0) exch(a,lt++,i++);
//            else if(cmp>0) exch(a,i,gt--);
//            else i++;
//        }
//        sort(a,low,lt-1);
//        sort(a,gt+1,high);
    }
    private static int partition(int[]a,int lo,int hi){
        int i = lo;
        int j = hi+1;
        int v = a[lo];
        while(true){
            while(les(a[++i],v)) if(i==hi) break;
            while(les(v,a[--j])) if(j==lo) break;
            if(i>=j) break;
            exch(a,i,j);
        }
        exch(a,lo,j);
        return j;
    }
    private static int compere(int a,int b){
        if(a>b) return 1;
        if(a==b) return 0;
        return -1;
    }
    private static boolean les(int a,int b){
        return compere(a,b)>0;
    }
    private static void exch(int[]a,int low,int high) {
        int tp = a[low];
        a[low] = a[high];
        a[high] = tp;
    }
}
希尔排序

对于每个h,用插入排序将h个子数组(由h个相互独立的数组成的数组)独立排序

 class Sort3{
    /*希尔排序*/
    public static void sort(int[]a){
        int h = 1;
        int N = a.length;
        while(h<N/3) {h=3*h+1;}
        while(h>=1){
        	/*冒泡排序*/
            for(int i = h;i<N;i++){
                for(int j = i;j>=h&&les(a[j],a[j-h]);j-=h)
                    exch(a,j,j-h);
            }
            /*插入排序*/
            //sort1(a,0,a.length,h);
            h/=3;
        }
    }
    /*插入排序,可以将上面的while循环中的冒泡替换成插入排序*/
    public static void sort1(int[]a,int lo,int high,int h){
        for(int i = lo;i<high;i+=h){
            for(int j = i;j>lo&&les(a[j],a[j-h]);j-=h){
                exch(a,j,j-h);
            }
        }
    }
    private static int compere(int a,int b){
        if(a>b) return 1;
        if(a==b) return 0;
        return -1;
    }
    private static boolean les(int a,int b){
        return compere(a,b)>0;
    }
    private static void exch(int[]a,int low,int high) {
        int tp = a[low];
        a[low] = a[high];
        a[high] = tp;
    }
}

(未完待续)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值