顺序表查找基础。

1、顺序查找

 特点:简单粗暴,效率较低。

2、二分查找

 如果顺序表已经有序,那么可以采用二分查找,其平均查找长度ASL= log(n+1) -1。

 特点:顺序表已经有序。

 样例程序如下:

 1 //二分搜索
 2 int binarySearch(int a[],int key, int lenArray)
 3 {
 4     int low=1, mid, high;
 5     high = lenArray;
 6     mid = (low+high)/2;
 7     while(low<=high)
 8     {
 9         if(a[mid]==key)
10             break;
11         else if(a[mid]>=key)
12             high = mid-1;
13         else
14             low = mid+1;
15         mid = (low+high)/2;
16     }
17     if(low>high)
18         return 0;
19     else
20         return mid;
21 }

  3、Fibonacci数列查找

   与二分查找相比,其能够应用于无序顺序表,平均性能比二分查找好。

   特点:运用Fibonacci数列的特点对顺序表进行分割,其与二分查找相比有两个优点:一是只有加减,没有除法,二是不要求顺序表有序。

   样例程序如下:

 1 //Fibonacci Search
 2 int FibonacciSearch(int a[],int key, int lenArray, int F[])
 3 {
 4     int i, k=0, low, high, mid;
 5     high = lenArray;
 6     low =1;
 7      /*网上很多找k的值为while(n > F[k]-1) k++;
 8      * 我觉得取得的k并不是最优的,k取得过大,会影响后面分割的次数.
 9      */
10     while(F[k] < lenArray)       
11         k++;
12     for(i=lenArray+1;i<=F[k];i++)
13         a[i] = a[lenArray];
14 
15     while(low<=high)
16     {
17         mid = low + F[k]-1;       //即: F[k] = mid - low +1;
18         if(a[mid] == key)
19             break;
20         else if(a[mid] > key)
21         {
22             high = mid -1;
23             k = k - 1;
24         }
25         else
26         {
27             low = mid +1;
28             k = k -2;            //注意:因为F[k]后半段的长度为F[k-2],因此k = k -2.
29         }
30     }
31     if(low > high)
32         return 0;
33     else
34         return mid;
35 }

  4、完整程序

 1 #include<stdio.h>
 2 #include<string.h>
 3 #define LENGTH 100
 4 int binarySearch(int [], int ,int );
 5 int FibonacciSearch(int [], int, int, int []);
 6 void createFibonacci(int [], int len);
 7 int main()
 8 {
 9     int a[LENGTH];
10     int F[LENGTH];
11     int i, key, index=0;
12     int arrLen;
13     printf("Input the length of array:");
14     scanf("%d",&arrLen);
15     printf("Input %d int data:",arrLen);
16     for(i=1;i<arrLen+1;i++)
17         scanf("%d",&a[i]);                         //从数组下标1开始
18     printf("Input the Key you search:");
19     scanf("%d",&key);
20     index = binarySearch(a,key, arrLen);
21     printf("binarySearch index at: %d\n",index);
22     //create Fibonacci Array
23     createFibonacci(F, arrLen);
24     index = FibonacciSearch(a, key, arrLen, F);
25     printf("FibonacciSearch index at: %d\n",index);
26     getchar();
27     getchar();
28 }
29 //二分搜索
30 int binarySearch(int a[],int key, int lenArray)
31 {
32     int low=1, mid, high;
33     high = lenArray;
34     mid = (low+high)/2;
35     while(low<=high)
36     {
37         if(a[mid]==key)
38             break;
39         else if(a[mid]>=key)
40             high = mid-1;
41         else
42             low = mid+1;
43         mid = (low+high)/2;
44     }
45     if(low>high)
46         return 0;
47     else
48         return mid;
49 }
50 void createFibonacci(int F[], int len)
51 {
52     int i;
53     //构造一个Fibonacci Array
54     F[0]=0;
55     F[1]=1;
56     for(i=2;i<len;i++)
57         F[i] = F[i-1]+F[i-2];
58 }
59 //Fibonacci Search
60 int FibonacciSearch(int a[],int key, int lenArray, int F[])
61 {
62     int i, k=0, low, high, mid;
63     high = lenArray;
64     low =1;
65      /*网上很多找k的值为while(n > F[k]-1) k++;
66      * 我觉得取得的k并不是最优的,k取得过大,会影响后面分割的次数.
67      */
68     while(F[k] < lenArray)       
69         k++;
70     for(i=lenArray+1;i<=F[k];i++)
71         a[i] = a[lenArray];
72 
73     while(low<=high)
74     {
75         mid = low + F[k]-1;       //即: F[k] = mid - low +1;
76         if(a[mid] == key)
77             break;
78         else if(a[mid] > key)
79         {
80             high = mid -1;
81             k = k - 1;
82         }
83         else
84         {
85             low = mid +1;
86             k = k -2;            //注意:因为F[k]后半段的长度为F[k-2],因此k = k -2.
87         }
88     }
89     if(low > high)
90         return 0;
91     else
92         return mid;
93 }

 

 posted on 2013-04-26 21:56  小小cfo  阅读(375)  评论(0编辑  收藏  举报