码迷,mamicode.com
首页 > 其他好文 > 详细

二分查找(折半查找)

时间:2016-10-19 01:41:06      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:

递归实现:

 

 1 #include<iostream>
 2 #include<cstdlib>
 3 #include<cstdio>
 4 #include<cmath>
 5 #include<cstring>
 6 #include<algorithm>
 7 using namespace std;
 8 int a[100]={1,2,3,5,12,12,12,15,29,55};//数组中的数(由小到大)
 9 int k;//要找的数字
10 int found(int x,int y)
11 {
12     int m=x+(y-x)/2;
13     if(x>y)//查找完毕没有找到答案,返回-1
14         return -1;
15     else
16     {
17         if(a[m]==k)
18             return m;//找到!返回位置.
19         else if(a[m]>k)
20             return found(x,m-1);//找左边
21          else
22             return found(m+1,y);//找右边
23     }
24 }
25 int main()
26     {
27         cin>>k;//输入要找的数字c语言把cin换为scanf即可
28         cout<<found(0,9);//从数组a[0]到a[9]c语言把cout换为printf即可
29         return 0;
30     }

 

 1 int binSearch(const int *Array,int start,int end,int key)
 2 {
 3         int left,right;
 4         int mid;
 5         left=start;
 6         right=end;
 7         while(left<=right)
 8              
 9         {
10                     mid=(left+right)/2;
11                     if(key==Array[mid])  return mid;
12                     else if(key<Array[mid]) right=mid-1;
13                     else if(key>Array[mid]) left=mid+1;
14                  
15         }
16         return -1;
17 }

 

 1 int bsearchWithoutRecursion(int array[],int low,int high,int target)
 2 {
 3     //这个函数有问题,
 4     //1)若查找的target在high处;
 5     //2)若查找的数大于array数值中最大值,也会出问题;
 6     while(low<=high)
 7         {
 8             int mid=(low+high)/2;
 9             if(array[mid]>target)
10                 high=mid-1;
11             elseif(array[mid]<target)
12             low=mid+1;
13             else
14                 return mid;
15         }
16     return-1;
17 }

 

 1 int BinSearch(SeqList *R,int n,KeyType K)
 2 {
 3     //在有序表R[0..n-1]中进行二分查找,成功时返回结点的位置,失败时返回-1
 4     int low=0,high=n-1,mid;//置当前查找区间上、下界的初值
 5     while(low<=high)
 6         {
 7             if(R[low].key==K)
 8                 return low;
 9             if(R[high].key==k)
10                 return high;    //当前查找区间R[low..high]非空
11             mid=low+((high-low)/2);
12                 /*使用(low+high)/2会有整数溢出的问题
13                 (问题会出现在当low+high的结果大于表达式结果类型所能表示的最大值时,
14                     这样,产生溢出后再/2是不会产生正确结果的,而low+((high-low)/2)
15                     不存在这个问题*/
16                 if(R[mid].key==K)
17                     return mid;//查找成功返回
18                           if(R[mid].key<K)
19                               low=mid+1//继续在R[mid+1..high]中查找
20                                   else
21                                       high=mid-1;//继续在R[low..mid-1]中查找
22         }
23     if(low>high)
24         return -1//当low>high时表示所查找区间内没有结果,查找失败
25 }

 

 

We basically ignore half of the elements just after one comparison.
1) Compare x with the middle element.
2) If x matches with middle element, we return the mid index.
3) Else If x is greater than the mid element, then x can only lie in right half subarray after the mid element. So we recur for right half.
4) Else (x is smaller) recur for the left half.

Following is Recursive implementation of Binary Search.

http://quiz.geeksforgeeks.org/binary-search/

 

 1 int binarysearch(int *arr, int s, int e, int key){
 2     while (s <= e) {
 3         int mid = (s+e)/2;
 4         if(key < arr[mid]){
 5             e = mid -1;
 6         }else if (key > arr[mid]) {
 7             s = mid + 1;
 8         }else {
 9             return mid;
10         }
11     }
12     return -1;
13 }
14 
15 int binarySearch(int arr[], int l, int r, int x)
16 {
17    if (r >= l)
18    {
19         int mid = l + (r - l)/2;
20 
21         // If the element is present at the middle itself
22         if (arr[mid] == x)  return mid;
23 
24         // If element is smaller than mid, then it can only be present
25         // in left subarray
26         if (arr[mid] > x) return binarySearch(arr, l, mid-1, x);
27 
28         // Else the element can only be present in right subarray
29         return binarySearch(arr, mid+1, r, x);
30    }
31 
32    // We reach here when element is not present in array
33    return -1;
34 }

 

  1. ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, last)中的第一个大于等于值val的位置。  
  2. ForwardIter upper_bound(ForwardIter first, ForwardIter last, const _Tp& val)算法返回一个非递减序列[first, last)中的第一个大于值val的位置。
 1 // lower_bound/upper_bound example
 2 #include <iostream>     // std::cout
 3 #include <algorithm>    // std::lower_bound, std::upper_bound, std::sort
 4 #include <vector>       // std::vector
 5 
 6 int main () {
 7   int myints[] = {10,20,30,30,20,10,10,20};
 8   std::vector<int> v(myints,myints+8);           // 10 20 30 30 20 10 10 20
 9 
10   std::sort (v.begin(), v.end());                // 10 10 10 20 20 20 30 30
11 
12   std::vector<int>::iterator low,up;
13   low=std::lower_bound (v.begin(), v.end(), 20); //          ^
14   up= std::upper_bound (v.begin(), v.end(), 20); //                   ^
15 
16   std::cout << "lower_bound at position " << (low- v.begin()) << \n;
17   std::cout << "upper_bound at position " << (up - v.begin()) << \n;
18 
19   return 0;
20 }

 

二分查找(折半查找)

标签:

原文地址:http://www.cnblogs.com/guxuanqing/p/5975313.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!