新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C語言中怎么實(shí)現(xiàn)折半查找
這篇文章將為大家詳細(xì)講解有關(guān)C語言中怎么實(shí)現(xiàn)折半查找,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
站在用戶的角度思考問題,與客戶深入溝通,找到仲巴網(wǎng)站設(shè)計(jì)與仲巴網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名與空間、虛擬主機(jī)、企業(yè)郵箱。業(yè)務(wù)覆蓋仲巴地區(qū)。
數(shù)據(jù)結(jié)構(gòu) 折半查找
實(shí)例代碼:
#include#include #include #define N 11 // 數(shù)據(jù)元素個數(shù) typedef int KeyType; // 設(shè)關(guān)鍵字域?yàn)檎?nbsp; typedef struct // 數(shù)據(jù)元素類型 { KeyType key; // 關(guān)鍵字域 int others; // 其它部分 }ElemType; // Search_Seq.h 靜態(tài)查找表的順序存儲結(jié)構(gòu) typedef struct { // 數(shù)據(jù)元素存儲空間基址,建表時按實(shí)際長度分配,0號單元留空 ElemType *elem; int length; // 表長度 }SSTable; ElemType r[N]={ {05,1},{13,2},{19,3},{21,4}, {37,5},{56,6},{64,7},{75,8}, {80,9},{88,10},{92,11} }; // 數(shù)據(jù)元素(以教科書P219的數(shù)據(jù)為例),全局變量 // 靜態(tài)查找表(順序表和有序表)的基本操作(7個) // 構(gòu)造一個含n個數(shù)據(jù)元素的靜態(tài)順序查找表ST(數(shù)據(jù)來自全局?jǐn)?shù)組r) int Creat_Seq(SSTable *ST,int n) { int i; (*ST).elem = (ElemType *)calloc(n+1, sizeof(ElemType)); // 動態(tài)生成n+1個數(shù)據(jù)元素空間(0號單元不用) if(!(*ST).elem) return 0; for( i = 1; i <= n; i++) *((*ST).elem+i) = r[i-1]; // 將全局?jǐn)?shù)組r的值依次賦給ST (*ST).length = n; return 1; } // 重建靜態(tài)查找表為按關(guān)鍵字非降序排序 void Ascend(SSTable *ST) { int i, j, k; for(i = 1; i < (*ST).length; i++) { k = i; (*ST).elem[0] = (*ST).elem[i]; // 待比較值存[0]單元 for(j = i+1; j <= (*ST).length; j++) //從中找到第i小的值 if ((*ST).elem[j].key < (*ST).elem[0].key) { k=j; (*ST).elem[0]=(*ST).elem[j]; } if(k != i) // 有更小的值則交換 { (*ST).elem[k]=(*ST).elem[i]; (*ST).elem[i]=(*ST).elem[0]; } } } // 構(gòu)造一個含n個數(shù)據(jù)元素的靜態(tài)按關(guān)鍵字非降序查找表ST, // 數(shù)據(jù)來自全局?jǐn)?shù)組r int Creat_Ord(SSTable *ST,int n) { int f; f = Creat_Seq(ST,n); //構(gòu)建一個靜態(tài)表 if( f ) //靜態(tài)表存在,則對其進(jìn)行重建 Ascend(ST); return f; } // 銷毀表ST int Destroy(SSTable *ST) { free((*ST).elem); (*ST).elem = NULL; (*ST).length = 0; return 1; } // 在有序表ST中折半查找其關(guān)鍵字等于key的數(shù)據(jù)元素。若找到,則函數(shù) // 值為該元素在表中的位置,否則為0。 int Search_Bin(SSTable ST,KeyType key) { int low, high, mid; low = 1; // 置區(qū)間初值 high = ST.length; while(low <= high) { mid = (low + high) / 2; if(key == ST.elem[mid].key) // 找到待查元素 return mid; else if(key < ST.elem[mid].key) high = mid - 1; // 繼續(xù)在前半?yún)^(qū)間進(jìn)行查找 else low = mid + 1; // 繼續(xù)在后半?yún)^(qū)間進(jìn)行查找 } return 0; // 順序表中不存在待查元素 } // 按順序?qū)T的每個元素調(diào)用函數(shù)Visit()一次且僅一次。 int Traverse(SSTable ST,void(*Visit)(ElemType)) { ElemType *p; int i; p = ++ST.elem; // p指向第一個元素,第0個元素沒有用 for(i = 1; i <= ST.length; i++) Visit( *p++ ); return 1; } void print(ElemType c) // Traverse()調(diào)用的函數(shù) { printf("(%d %d) ", c.key, c.others); } int main() { SSTable st; int i; KeyType s; Creat_Ord(&st, N); // 由全局?jǐn)?shù)組產(chǎn)生非降序靜態(tài)查找表st Traverse(st,print); // 順序輸出非降序靜態(tài)查找表st printf("\n請輸入待查找值的關(guān)鍵字: "); scanf("%d", &s); i = Search_Bin(st, s); // 折半查找有序表 if( i ) print(st.elem[i]); else printf("沒找到.\n"); Destroy(&st); system("pause"); return 0; } /* 輸出效果: (5 1) (13 2) (19 3) (21 4) (37 5) (56 6) (64 7) (75 8) (80 9) (88 10) (92 11) 請輸入待查找值的關(guān)鍵字: 75 (75 8) 請按任意鍵繼續(xù). . . */
運(yùn)行結(jié)果如下:
關(guān)于C語言中怎么實(shí)現(xiàn)折半查找就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
網(wǎng)頁標(biāo)題:C語言中怎么實(shí)現(xiàn)折半查找
分享網(wǎng)址:http://fisionsoft.com.cn/article/gggpgs.html