Binary search method (binary search method)

Binary search

1. The advantage is that the number of comparisons is relatively small, and the number you want to find can be quickly found, which takes less time.
2. The disadvantage is that this method requires that the table to be looked up is ordered, and it is difficult to insert and delete.

1. Use a one-dimensional array as an example:

For example, a one-dimensional array a [10] = {2,5,7,8,9,15,20); find the position of key = 15 in this array. (Because it is an example, this array is ordered. If you encounter an unordered array, you can sort it first, but in fact, the sort of kung fu, it is estimated that it can be found after traversing it. So it is not recommended to order in It will be more time-consuming when using it), Insert picture description here
as shown in the figure, record the smallest data as low, the largest data as high, and the middle value as mid; (if mid is not an integer, it must be rounded)
use two points When searching by method, first compare the value of the key to be searched with mid. For example, in this example, 8 and 15 are compared, 8 is smaller than 15, so the range is reduced to [8, 20]. Inside the interval. Then continue to use the above method in this interval;
c language code is as follows:

#include<stdio.h>
int search(int a[],int n,int key)
{
   int low=0;
   int high=n-1,mid;
   while(low<=high)
   {
      mid=(low+high)/2;
      if(key==a[mid]);
      	return mid;
      if(key>a[mid])
      		low=mid+1;
      	else high=mid-1;
   }
return -1;
}

int main()
{
   int a[]={2,5,7,8,9,15,20};
   printf("%d",search(a,7,15));
   return 0;
}

2. Use two-dimensional array to search in half.

Since a two-digit array is used for searching, it means that each row and column of the two-dimensional array is ordered.
Example: There is a 3 * 5 two-digit array. Each row and each column of it is incremented, as shown in the figure. Insert picture description here
Because each row is incremented, we initially use the number 9 in the upper right corner as a benchmark, we will need to find Compares the number with the number 9, if it is larger than 9, then the column of the number 9 is unchanged, and the number of rows is increased by one, which is equivalent to jumping to the number 12. Then compare the number to be found with the number 12, if it is smaller than , Then the number of rows of the number 12 remains unchanged, the number of columns -1. Then repeat the above process, you can find.

Note: The conditions for the end of the loop: 1 is to find the number you want, and terminate. 2 is not found, terminated, there are two possibilities when the number is not found, the number of rows has been increased, exceeded the array boundary or the number of columns has been reduced, exceeded the boundary.
The complete code is as follows:

#include<stdio.h>
int main()
{
  int n,m,key,i,j;
  int a[10][10];
  scanf("%d %d",&n,&m);
  for(i=1;i<=n;i++)
    for(j=1;j<=m;j++)
      scanf("%d",&a[i][j]);
  scanf("%d",&key);
  i=1;j=m;   //将与key作比较的点选为右上角
  while(i<=n&&j>=1)
  {
    if(a[i][j]==key)
    {
      printf("%d %d",i,j);
      return 0;
    }
    if(a[i][j]<key)
        i++;
    else  j--;
  }
  printf("-1");    //找不到则输出-1
  return 0;
  }
Published 10 original articles · Likes2 · Visits 217

Guess you like

Origin blog.csdn.net/dfwef24t5/article/details/104140669