欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C語言數(shù)據(jù)結(jié)構(gòu)之 折半查找實例詳解

 更新時間:2017年06月21日 09:34:34   投稿:lqh  
這篇文章主要介紹了C語言數(shù)據(jù)結(jié)構(gòu)之 折半查找實例詳解的相關(guān)資料,需要的朋友可以參考下

數(shù)據(jù)結(jié)構(gòu) 折半查找

實例代碼:

/* 
  名稱:折半查找  
  語言:數(shù)據(jù)結(jié)構(gòu)C語言版  
  編譯環(huán)境:VC++ 6.0 
  日期: 2014-3-26  
*/ 
 
#include <stdio.h> 
#include <malloc.h> 
#include <windows.h> 
 
 
#define N 11 // 數(shù)據(jù)元素個數(shù)  
 
typedef int KeyType; // 設(shè)關(guān)鍵字域為整型  
 
typedef struct // 數(shù)據(jù)元素類型  
{ 
  KeyType key;  // 關(guān)鍵字域  
  int others;   // 其它部分  
 }ElemType; 
 
 
// Search_Seq.h 靜態(tài)查找表的順序存儲結(jié)構(gòu)  
typedef struct 
{ 
  // 數(shù)據(jù)元素存儲空間基址,建表時按實際長度分配,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ù)來自全局數(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]; // 將全局數(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ù)來自全局數(shù)組r  
int Creat_Ord(SSTable *ST,int n) 
{ 
  int f; 
  f = Creat_Seq(ST,n);  //構(gòu)建一個靜態(tài)表 
  if( f ) //靜態(tài)表存在,則對其進行重建 
    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ū)間進行查找  
    else 
      low = mid + 1;   // 繼續(xù)在后半?yún)^(qū)間進行查找  
  } 
  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); // 由全局數(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ù). . .  
 
*/  

運行結(jié)果如下:

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

最新評論