《算法技术手册》相关笔记-第五章查找


顺序查找

顺序查找(线性查找),是最简单的查询算法。它通过穷举来寻找集合C中的单独目标元素t。

顺序查找在元素的类型上限制最少。唯一的要求就是必须有一个匹配函数来决定是否目标元素和集合中的当前元素匹配。

算法详解

最好情况平均情况最坏情况
O(1)O(n)O(n)


代码实现

    public boolean sequentialSearch(T[] collection,T t){
        for (int i=0;i<collection.length;i++){
            if(collection[i].equals(t)) return true;
        }
        return false;
    }
结论
对于无序小集合来说,顺序查找是最容易实现的并且总体上很有效率。


二分查找

算法详解

最好情况平均情况最坏情况
O(1)O(log n)O(log n)


代码实现

    /**
     * collections必须是有序的
     *
     * @param  collections, t
     * @return boolean
     * @exception
     *
     * @author sunyx
     * @date 2016/3/3
     * @since JDK 1.8
     */
    public boolean search(int[] collections,int t){
        //确定查询范围
        int low=0;
        int hight=collections.length-1;
        while(low<=hight){
            //找到中枢点
            int ix=(hight-low)/2+low;
            //中枢值满足查询条件,返回真
            if(cmp(t,collections[ix])==0) return true;
            else if(cmp(t,collections[ix])<0) hight=ix-1;//如果查询条件小于中枢值,查询中枢值左边的值
            else low=ix+1;//查询条件大于中枢值,查询中枢值右边的值
        }
        return false;
    }

    public int cmp(int v1,int v2){
        return v1-v2;
    }


基于散列的查找

算法详解

最好情况平均情况最坏情况
O(1)O(1)O(n)


代码实现

int BUCKETNUM;

    class Entry{
        String value;
        Entry next;
    }

    class Bucket{
        //String key;
        int size;
        Entry head;
    }
    /**
     * 创建一个散列表
     *
     * @param  size, collection
     * @return com.syx.th5.th5_3.Bucket[]
     * @exception
     *
     * @author sunyx
     * @date 2016/3/4
     * @since JDK 1.8
     */
    public Bucket[] loadTable(int size,String[] collection){
        this.BUCKETNUM =size;
        Bucket[] buckets=new Bucket[BUCKETNUM];
        for (int i = 0; i <collection.length ; i++) {
            int h=hash(collection[i]);//获取该元素的散列值
            if(buckets[h]==null){
                buckets[h]=new Bucket();
                buckets[h].size=1;
                buckets[h].head=new Entry();
                buckets[h].head.value=collection[i];
            }else{
                Entry entry=new Entry();
                entry.value=collection[i];
                entry.next=buckets[h].head;
                buckets[h].size+=1;
                buckets[h].head=entry;
            }
        }
        return buckets;
    }

    public boolean search(Bucket[] buckets,String value){
        int h=hash(value);
        Entry entry=buckets[h].head;
        while (entry!=null){
            if(value.equals(entry.value)) return true;
            entry=entry.next;
        }
        return false;
    }

    /**
     * 关键
     * hash产生的值 将影响到数据在散列表中的分布
     *
     * @param  value
     * @return int
     * @exception
     *
     * @author sunyx
     * @date 2016/3/4
     * @since JDK 1.8  
     */
    public int hash(String value){
        
        int key=value.length()%BUCKETNUM;
        return key>=BUCKETNUM?BUCKETNUM-1:key;
    }


二叉查找树

[学习红黑树相关知识,之后更新]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值