查找(课堂笔记)

Linear Search

boolean isInArray (int theArray[], int iSearch){
    int N= theArray.length;
    for(int i=0; i

时间复杂度:
O(N)


Sentinel Search

boolean isInsentinel(int [] theArray, int iSearch){
    int N= theArray.length;
    if(theArray[N-1]==iSearch){
        return true;
    }
    theArray[N-1]=iSearch;
    for(int i=0; ;i++){
        if(theArray[i]==iSearch){
            break;
        }
    }
    return (i< N-1);
}

应用于升序排列的数组上:

boolean isInSorted(int [] theArray, int iSearch){
    int N= theArray.length;
    if((theArray[N-1]iSearch)){
        return false;
    }
    if(theArray[N-1]==iSearch){
        return true;
    }
    theArray[N-1]=iSearch;
    int i;
    for(i=0; ; i++){
        if(theArray[i]>=iSearch){
            break;
        }
    return (i< N-1) && (theArray[i] ==iSearch);
}

时间复杂度:

O(N)


Binary Search

boolean isInBinary(int [] theArray, int iSearch){
    int lo=0;
    int hi=theArray.length-1;
    int mid=0;
    while(hi>=lo){
        mid=(lo+hi)/2;
        if(theArray[mid]==iSearch){
            return true;
        else if (theArray[mid]

时间复杂度:

O(logN)

你可能感兴趣的:(笔记,算法,c++)