【查找算法】基於比較的查找算法(順序查找、對分查找、分塊查找)


1、順序查找:O(n)

//1、順序查找
int SequentialSearch(int *array, int n, int key)
{
    int i=0;
    while( i < n  &&  array[i] != key)
    {
        ++i;
    }
    if (array[i] == key)
    {
        cout<<"Find: ";
        cout<<i;
        return i;
    }
    else 
    {
        cout<<"Not Find!";
        return -1;
    }
}

int SequentialSearchAdvanced(int *array, int n, int key)
{
    int i=0;
    while(array[i] != key)
    {
        ++i;
    }
    if (i < n)
    {
        cout<<"Find: ";
        cout<<i;
        return i;
    }
    else 
    {
        cout<<"Not Find!";
        return -1;
    }
}
View Code

2、對分查找:

前提:從小到大有序排列

時間復雜度:O(log2n)

//2、對分查找
int BinarySearch(int *array, int n, int key)
{
    int left=0, right=n-1, mid;
    while (left<=right)
    {
        mid = (left+right)/2;
        if (key < array[mid])
        {
            right = mid-1;
        } 
        else if( key > array[mid])
        {
            left = mid+1;
        }
        else
        {
            cout<<"Successful Search!"<<endl;
            cout<<mid<<endl;
            return mid;
        }
    }
    cout<<"Search failure!"<<endl;
    return -1;

}
View Code

3、分塊查找:又稱索引順序查找,這是順序查找的一種改進方法,用於在分塊有序表中進行查找 。

主表:存儲數據的表,長度n;

索引表:將主表分塊,每塊長s,找出每塊中的關鍵字最大值,並且保存該塊中所有數據在主表中的索引

(1)分塊:將n個數據元素“按塊有序”划分為m塊。

每一塊中的結點不必有序,但塊與塊之間必須“按塊有序”;即第1塊中任一元素的關鍵字都必須小於第2塊中任一元素的關鍵字;而第2塊中任一元素又都必須小於第3塊中的任一元素。每個塊中元素不一定是有序的。

(2)根據查找值,和索引表中關鍵字(每塊中的最大關鍵字)比較,通過對分查找/順序查找,找到該值所在的塊范圍;

(3)在相應塊中,找到該值在主表中的位置。

平均查找長度ASL<=O(log2(n/s))+s/2  (先對分查找,或順序查找)

//3、分塊查找
//索引表:key記錄每塊的最大關鍵值,link記錄該塊數據在主表中的起始位置
//建立4個塊
struct index
{
    int key;
    int link;
}indexChart[4];

//array:主表  n:主表長度  indexChart:索引表  BlockNumber:塊個數  key:要查找的關鍵字
int IndexSearch(int *array, int n, index indexChart[], int BlockNumber, int key)
{
    int indexNumber = 0;
    int BlockStart = 0, BlockEnd = 0;
    //查找所在塊
    while(indexNumber<BlockNumber  &&  key>indexChart[indexNumber].key)
    {
        ++indexNumber;
    }
    if (indexNumber >= BlockNumber)
    {
        cout<<"Search failure!"<<endl;
        return -1;
    }
    //查找所在主表位置
    else
    {
        BlockStart = indexChart[indexNumber].link;
        if (BlockStart == BlockNumber-1) 
        {
            BlockEnd = n;
        }
        else
        {
            BlockEnd = indexChart[indexNumber+1].link -1;
        }
        int j = BlockStart;
        while(key != array[j] && j<=BlockEnd)
        {
            ++j;
        }
        if (key == array[j])
        {
            cout<<"Successful Search!"<<endl;
            cout<<j<<endl;
            return j;
        }
        else
        {
            cout<<"Search failure!"<<endl;
            return -1;
        }
    }
}
void IndexSearchSample()
{
    int a[] = {1,2,5,6,7,8,9,10,11,12,13,14,15,16,17,18};
    for (int i=0; i<4; i++)
    {
        indexChart[i].link = i*4;
        indexChart[i].key = a[i*4+3];        
    }
    int i = IndexSearch(a,16,indexChart,4,13);
}
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM