二分查找c++實現


二分查找的算法原理較為簡單,在此給出c++代碼實現,以及代碼中遇到的問題,以及解決方案:

 1 # include "iostream"
 2 using namespace std;
 3 
 4 //template <class, T>
 5 int Binary_search( const int vector[] , int value)//采用泛型的方式
 6 {
 7     //T* low = vector[0];
 8     //T* high = sizeof(vetor) / sizeof(vetor)+vector[0]-1;
 9     int low = 0;//獲取最低位
10     int high = sizeof(vector) / sizeof(vector[0])-1;//這里顯示的vector
11     cout << "vector:" << sizeof(vector) << endl;//這里sizeof vector等於=4,並不是4*13等於32???思考這是為什么
12     cout<<";vector[0]:" << sizeof(vector[0]) << endl;
13     cout << "high:" << high << endl;
14     //T *middle = (low + high) / 2;
15     int middle;
16     while (low <= high)
17     {
18         middle = (low + high) / 2;
19         if (vector[middle] > value)
20         {
21             high = middle - 1;
22         }
23         if (vector[middle] < value)
24         {
25             low = middle + 1;
26         }
27         if (vector[middle] == value)
28         {
29             return middle;
30         }
31     }
32 }
33 int main()
34 {
35     const int a[] = { 1, 2, 5, 8, 13, 19, 25, 38, 40, 47, 69, 48, 90 };
36     cout <<"a:" <<sizeof(a) << endl;
37     cout<<Binary_search(a, 5)<<endl;
38     system("pause");
39     return 0;
40 }

其中,while(){}代碼段實現了二分查找的原理,但程序的運行結果並不正確。

我們知道通過 :sizeof(a)/sizeof(a[0])可以得到數組a的長度,但是經過參數傳遞,我們以為sizeof(vector)/sizeof(vector[0]) == sizeof(a)/sizeof(a[0])!!!但實際上,這是錯誤的!!!。數組不等於指針!!!。數組a傳遞給 vector,我們以為的是:將數組a的內容復制到數組vector,但實際的處理過程是:vector僅僅是一個指針,指向了數組a這塊區域,但我們並不能通過這個指針vector來獲得這塊區域的大小。而sizeof(vector)也就成了我們得到的是指針變量的大小!!!,而不是指針所指向區域的大小!!!!。這是很重要的區別。

但是二分查找明顯要求我們得到查找數據的長度。那么我們該如何得到這個長度呢?

一種方法是我們在主程序中先得到數據的長度,將長度作為一個函數參數進行傳遞。但這種方式真的很蠢(個人覺得真的很蠢!!!,因為這樣不能體現算法本身的封裝性)。那么有沒有其他方法呢?

如果我們采用泛型加引用的方式呢?

 1 # include "iostream"
 2 using namespace std;
 3 
 4 template <class T>
 5 int Binary_search( const T & vector ,const  T value)//采用泛型的方式
 6 {
 7     int low = 0;
 8     int high = sizeof(vector) / sizeof(vector[0])-1;
 9     cout << "high:" << high << endl;
10     int middle;
11     while (low <= high)
12     {
13         middle = (low + high) / 2;
14         if (vector[middle] > value)
15         {
16             high = middle - 1;
17         }
18         if (vector[middle] < value)
19         {
20             low = middle + 1;
21         }
22         if (vector[middle] == value)
23         {
24             return middle;
25         }
26     }
27     
28 }
29 int main()
30 {
31     const int a[] = { 1, 2, 5, 8, 13, 19, 25, 38, 40, 47, 69, 48, 90 };
32     cout <<"a:" <<sizeof(a) << endl;
33     //const int value = 5;
34     cout<<Binary_search(a,5)<<endl;//無法執行
35     system("pause");
36     return 0;
37 }

我們的本意是:忽略傳遞數組的類型,采用泛型的思想設計程序,但是程序報錯:

錯誤 1 error C2782: “int Binary_search(const T &,const T)”: 模板 參數“T”不明確 c:\users\kb409\desktop\c++\binary search\binary search\binary search.cpp 34 1 Binary search

由於泛型編程掌握的並不是很好,所以並不知道這樣做錯在哪里,如果有大神知道,請給我留言!

最后,將代碼改成了下面這個樣子,程序通過:

 1 # include "iostream"
 2 using namespace std;
 3 
 4 template <class T>
 5 int Binary_search( const T & vector ,const  int value)//采用泛型的方式
 6 {
 7     int low = 0;
 8     int high = sizeof(vector) / sizeof(vector[0])-1;
 9     cout << "high:" << high << endl;
10     int middle;
11     while (low <= high)
12     {
13         middle = (low + high) / 2;
14         if (vector[middle] > value)
15         {
16             high = middle - 1;
17         }
18         if (vector[middle] < value)
19         {
20             low = middle + 1;
21         }
22         if (vector[middle] == value)
23         {
24             return middle;
25         }
26     }
27     
28 }
29 int main()
30 {
31     const int a[] = { 1, 2, 5, 8, 13, 19, 25, 38, 40, 47, 69, 48, 90 };
32     //cout <<"a:" <<sizeof(a) << endl;
33     //const int value = 5;
34     cout<<Binary_search(a,90)<<endl;//可以執行
35     system("pause");
36     return 0;
37 }

所做的改變僅僅是將第五行const T value改變成了 int value ,鑒於目前知識水平有限嗎,對模板的使用並不是很熟練,在后續中逐漸弄明白這個問題

 


免責聲明!

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



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