常用算法

1 二分查找:

非递归

public static int biSearch(int []array,int a){
        int lo=0;
        int hi=array.length-1;
        int mid;
        while(lo<=hi){
            mid=(lo+hi)/2;
            if(array[mid]==a){
                return mid+1;
            }else if(array[mid]<a){
                lo=mid+1;
            }else{
                hi=mid-1;
            }
        }
        return -1;
    }

递归实现:
public static int sort(int []array,int a,int lo,int hi){
        if(lo<=hi){
            int mid=(lo+hi)/2;
            if(a==array[mid]){
                return mid+1;
            }
            else if(a>array[mid]){
                return sort(array,a,mid+1,hi);
            }else{
                return sort(array,a,lo,mid-1);
            }
        }
        return -1;
    }

快速排序:参考 https://www.cnblogs.com/coderising/p/5708801.html

堆排序:https://www.cnblogs.com/chengxiao/p/6129630.html

归并排序:https://www.cnblogs.com/chengxiao/p/6194356.html

posted @ 2019-05-18 15:03  Samuel-Leung  阅读(134)  评论(0编辑  收藏  举报