Binary search problem of ordinal sequence

Binary search problem of ordinal sequence

Binary search is also called binary search. It is a more efficient search method. Compared with the O(n) time complexity of sequential search, its time complexity O(log 2 n) has a clear advantage.

But not all numbers can be searched by binary search method. Binary search requires that the given number series must be ordered, that is, stored in the computer in a sequential structure, and must be arranged in order according to the size of the keywords. Taking integer data as an example, the subscript that returns the element is found, and the specific implementation method is introduced below:

Before that, we need to store an ordered set of data in an array, and create an arr array to store several ordinal numbers to be stored. If we want to halve the array, we need to get the subscript of the middle element of the array, know the middle The subscript of the element, we have to know the subscript of the leftmost element and the subscript of the rightmost element of the array. Let the left subscript be left and the right subscript be right . Obviously left =0, right = the number of array elements-1, The number of array elements sz = sizeof(arr) / sizeof(arr[0]) , we then take the average of the left and right subscripts as the middle subscript mid , everything is ready, and then proceed to the next step.

First, we get mid = (left + right) , compare the element whose index is mid in the array with the number k to be searched :
1. The middle element of arr[mid] <k is
less than the number to be searched, indicating the number to be searched It is on the right side of the middle number, so the content on the left side of the middle number can be discarded. At this time, the array on the right side of the middle subscript is taken as the array to be operated in the next round, and left = mid +1 , right unchanged.

2. The middle element of arr[mid]> k is
greater than the number to be searched, indicating that the number to be searched is to the left of the middle number, so the content on the right of the middle number can be discarded. At this time, the array to the left of the middle subscript is taken as the next round. For the array of operations, let right = mid-1 and left unchanged.

. 3, ARR [MID] = k
In this case k is found, MID i.e. its subscript.

The above operation is just a round. When left and right are constantly changing, a loop is needed at this time. When left <right , the loop can be executed, but we should pay attention to it when executing the last loop. In the case of left = right = mid , the loop condition should be left <= right .

If k is not found after jumping out of the loop , this number is not found in the array.

#include<stdio.h>
int main()
{
    
    
   int k=7;  //要查找的数字
   int arr[]={
    
    1,2,3,4,5,6,7,8,9,10};
   int left,mid,right;
   int sz = sizeof(arr)/sizeof(arr[0]);
   left = 0;
   right = sz -1;
   while(left<=right)
   {
    
    
      //mid = (left+right)/2;  可能会有溢出风险
      mid = left + (right - left)/2;
      if(arr[mid] < k)
      {
    
    
         left = mid +1;
      }
      else if(arr[mid] > k)
      {
    
    
         right = mid -1;
      }
      else
      {
    
    
         printf("找到了,下标是%d\n",mid);
      }
   }
   if(left > right)
   {
    
    
      printf("找不到\n");
      break;
   }
 return 0;
}

Guess you like

Origin blog.csdn.net/weixin_52606524/article/details/112800207