查找算法的實現(C/C++實現)


存檔:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #define max 20
 4 typedef int keytype;
 5 #include "search.h"
 6 int main()
 7 {
 8     sstable st;
 9     keytype key;
10     int result,num;
11     init(st);
12     printf("***************************************\n");
13     printf("1.順序查找\n");
14     printf("2.折半查找\n");
15     printf("3.輸出表信息\n");
16     printf("4.退出\n");
17     printf("***************************************\n");
18     printf("請輸入你的選擇:\n");
19     scanf("%d",&num);
20     while(1)
21     {
22         switch(num)
23         {
24             case 1:
25                 printf("請創建順序查找表");
26                 create(st);
27                 printf("請輸入順序查找的關鍵字:");
28                 scanf("%d",&key);
29                 result=search_seq(st,key);
30                 if(result!=0)
31                     printf("在順序表里第%d個位置查找到了!\n",result);
32                 else
33                     printf("在順序表里沒有找到!\n");
34                 break;
35             case 2:
36                 printf("請創建遞增的折半查找表\n");
37                 create(st);
38                 printf("請輸入折半查找的關鍵字:");
39                 scanf("%d",&key);
40                 result=search_bin(st,key);
41                 if(result!=0)
42                     printf("在順序表里第%d個位置查找到了!\n",result);
43                 else
44                     printf("在順序表里沒有找到!\n");
45                 break;
46             case 3:
47                 print(st);
48                 break;
49             case 4:
50                 exit(0);
51                 break;
52             default:printf("輸入錯誤!\n"); 
53         }
54         printf("\n請重新輸入您的選擇:\n");
55         scanf("%d",&num);
56     }
57     return 0;
58 }
 1 typedef char infotype;
 2 typedef struct
 3 {
 4     keytype key;//keytype為關鍵字的數據類型 
 5     infotype other;//其他數據 
 6 }elemtype;//數據元素類型 
 7 typedef struct
 8 {
 9     elemtype *r;//基地址 
10     int length;//元素個數 
11 }sstable;//靜態查找表 
12 int init(sstable &l)//初始化靜態查找表,分配資源 
13 {
14     l.r=new elemtype[max];
15     if(!l.r)
16     {
17         printf("初始化錯誤!\n");
18         return 0;
19     }
20     l.length=0;
21     return 1;
22 }
23 void print(sstable l)//打印靜態查找表的內容 
24 {
25     for(int i=1;i<=l.length;i++)
26         printf("%d號關鍵字:%d\n",i,l.r[i].key);
27     printf("元素個數:%d\n",l.length);
28 }
29 int create(sstable &l)//創建表,對表中輸入數據 
30 {
31     l.length=0;
32     int k,i;
33     printf("請輸入int型關鍵字,以-1結束:\n");
34     scanf("%d",&k);
35     while(k!=-1)
36     {
37         l.r[l.length+1].key=k;//0號位置留空 
38         l.length++;
39         if(l.length>=max)
40             return 0;
41         scanf("%d",&k); 
42     }
43     printf("下標:");
44     for(i=1;i<=l.length;i++)
45         printf("%4d",i);
46     printf("\n");
47     printf("key :");
48     for(i=1;i<=l.length;i++)
49         printf("%4d",l.r[i].key);
50     printf("\n");
51     return 1;
52 }
53 int search_seq(sstable st,keytype key)//在順序表ST中順序查找其關鍵字等於key的數據元素 
54 {
55     //若找到,則函數值為該元素在表中的位置,否則為0 
56     st.r[0].key=key;
57     for(int i=st.length;i>=1;i--)//從后往前找 
58     {
59         if(st.r[i].key==key)
60         {
61             return i;
62         }
63     }
64     return 0;
65 }
66 int search_bin(sstable st,int key)//在有序表ST中折半查找其關鍵字等於key的數據元素
67 {
68     //若找到,則函數值為該元素在表中的位置,否則為0
69     int low=1;
70     int high=st.length;
71     int mid;
72     while(low<=high)
73     {
74         mid=(low+high)/2;
75         printf("折半查找的low值%d、high值%d、mid值%d\n",low,high,mid);
76         if(key==st.r[mid].key)
77             return mid;
78         else if(key<st.r[mid].key)
79             high=mid-1;
80         else
81             low=mid+1;
82     }
83     return 0;//表中不存在待查元素 
84 }

運行結果如下:

 


免責聲明!

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



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