java的哈希排序

代码实现:
   
   
public class Insertion {
    @SuppressWarnings({ "rawtypes", "unchecked" })
    private static boolean less(Comparable v, Comparable m){
        return v.compareTo(m) < 0;
    }
    @SuppressWarnings({ "unused", "rawtypes" })
    private static void exch(Comparable [] a, int i, int j){
        Comparable t = a[i];
        a[i] = a[j];
        a[j] = t;
    }
    @SuppressWarnings({ "unused", "rawtypes" })
    private static void show(Comparable [] a){
        System.out.println("排序之后:");
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
    }
    @SuppressWarnings("rawtypes")
    public static boolean isSorted(Comparable [] a){
        //测试数组元素是否有序
        for (int i = 1; i < a.length; i++) 
            if (less(a[i], a[i-1])) 
                return false;
        return true;
    }
    @SuppressWarnings("rawtypes")
    public static void sort(Comparable [] a){
        int N = a.length;
        int h = 1;
        while (< N/3)
            h = 3*+ 1;
        while (>= 1) {
            for (int i = h; i < N; i++) {
                for (int j = i; j >= h && less(a[j], a[j-h]); j-=h) {
                    exch(a, j, j-h);
                }
            }
            h = h/3;
        }
    }
    @SuppressWarnings("rawtypes")
    public static void main(String[] args) {
        //Comparable[] a = {49,38,65,97,76,13,27,49,78,34,12,64,1,8};
        Comparable[] a = {49,38};
        //String [] a = {"s","o","r","t","e","x","a","m","p","l","e"};
        System.out.println("排序之前:");
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i]+" ");
        }
        System.out.println();
        sort(a);
        assert isSorted(a);
        show(a);
    }
}  
    
    
排序之前:
s o r t e x a m p l e 
排序之后:
a e e l m o p r s t x   
算法分析:
  1. 平均时间复杂度:希尔排序的时间复杂度和其增量序列有关系,这涉及到数学上尚未解决的难题;不过在某些序列。
  2. 中复杂度可以为O(n1.3)。
  3. 空间复杂度:O(1)  。
  4. 稳定性:不稳定。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半路笙歌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值