查找算法

查找算法是典型的常用算法,查找算法對綜合效率要求比較高,常用的查找算法有很多種,本文主要介紹順序查找和折半查找(二分查找),更多的查找算法還請小夥伴們自行研究。

頭文件:

/***************************************************************************************************** 
 *Copyright:Yue Workstation 
 * 
 *FileName:Seek.h 
 * 
 *Function:查找相關數據定義和函數聲明 
 * 
 *Author:Abel Lee 
 * 
 *CreateOn:2012-2-9 
 * 
 *Log:2012-2-9 由Abel Lee創建 
 *****************************************************************************************************/ 

#ifndef SEEK_H 
#define SEEK_H 

#include "global.h" 

typedef struct SeekNode 
{ 
    ElemType *elem; 
    int length; 
}SSTable; 

int CreateTable(SSTable *ST, int n); 
void PrintTable(SSTable ST); 
int SequentialSearch(SSTable ST,ElemType key); 
void DestroyTable(SSTable *ST); 
int SearchBin(SSTable ST, ElemType key); 

#endif

源文件:

/***************************************************************************************************** 
 *Copyright:Yue Workstation 
 * 
 *FileName:Queue.c 
 * 
 *Function:排序基本算法 
 * 
 *Author:Abel Lee 
 * 
 *CreateOn:2012-2-9 
 * 
 *Log:2011-2-9 由Abel Lee創建 
 *****************************************************************************************************/ 

#include "../inc/Seek.h" 

/**************************************************************************************************** 
 *Function Name: CreateTable 
 * 
 *Function: 創建一個表 
 * 
 *Parameter:   ST:表頭指針 
 *             n:表長度 
 * 
 *Return Value:成功返回0,失敗返回-1 
 * 
 *Author:Abel Lee 
 * 
 *Log:2011-2-9 
 ***************************************************************************************************/ 
int CreateTable(SSTable *ST, int n) 
{ 
    int i = 0; 

    printf("Creating...\n"); 
    if(ST == NULL) 
    { 
        perror("ST is NULL!"); 
        return -1; 
    } 

    ST->elem = (ElemType *)malloc(n+1); 

    for(i = 1; i <= n; i++) 
    { 
        ST->elem[i] = i; 
    } 
    ST->elem[0] = 0; 
    ST->length = n; 

    printf("Create SSTable Success!\n"); 
    return 0; 
} 

/**************************************************************************************************** 
 *Function Name: PrintTable 
 * 
 *Function: 打印一個表中的內容 
 * 
 *Parameter:   ST:表頭指針 
 * 
 *Return Value:無 
 * 
 *Author:Abel Lee 
 * 
 *Log:2011-2-9 
 ***************************************************************************************************/ 
void PrintTable(SSTable ST) 
{ 
    int i = 0; 

    for(i = 1; i <= ST.length; i++) 
    { 
        printf("%d-->",ST.elem[i]); 
    } 

    return; 
} 

/**************************************************************************************************** 
 *Function Name: DestroyTable 
 * 
 *Function: 銷燬一個表 
 * 
 *Parameter:   ST:表頭指針 
 * 
 *Return Value:無 
 * 
 *Author:Abel Lee 
 * 
 *Log:2011-2-9 
 ***************************************************************************************************/ 
void DestroyTable(SSTable *ST) 
{ 
    return ; 
} 

/**************************************************************************************************** 
 *Function Name:SequentialSearch 
 * 
 *Function:順序查表法 
 * 
 *Parameter:   ST:查找對象 
 *             key:關鍵字 
 * 
 *Return Value:成功返回0,失敗返回-1 
 * 
 *Author:Abel Lee 
 * 
 *Log:2011-2-9 
 ***************************************************************************************************/ 
int SequentialSearch(SSTable ST,ElemType key) 
{ 
    int i = 0; 

    for(i = 1; i < ST.length; i++) 
    { 
        if(ST.elem[i] == key) 
        { 
            return i; 
        } 
    } 
    return 0; 
} 

/**************************************************************************************************** 
 *Function Name:SequentialSearch 
 * 
 *Function: 折半查表法,針對於有序表 
 * 
 *Parameter:   ST:查找對象 
 *             key:關鍵字 
 * 
 *Return Value:成功返回0,失敗返回-1 
 * 
 *Author:Abel Lee 
 * 
 *Log:2011-2-9 
 ***************************************************************************************************/ 
int SearchBin(SSTable ST, ElemType key) 
{ 
    int low = 1; 
    int high = ST.length; 
    int mid = 0; 

    while(low <= high) 
    { 
        mid = (low + high)/2; 
        if(key == ST.elem[mid]) 
        { 
            return mid; 
        } 
        else if(key < ST.elem[mid]) 
        { 
            high = mid - 1; 
        } 
        else 
        { 
            low = mid + 1; 
        } 
    } 

    return 0; 
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章