topic:
Write a function to implement a binary search of an integer ordered array , and find out the corresponding subscript of the number you are looking for in the array .
=========================================================================
Ideas:
general idea:
(1) Custom function part:
(1).
parameter:
int arr[] -- the first address of the array
int k -- the number to find in the array
int sz -- array length
Define the left and right subscripts ;
(2).
use binary search ;
(2) Main function part:
Define an ordered array , set the value to find , and find the number of elements in the array .
Call a custom function .
Determine the return value of the custom function and print the corresponding situation .
Custom function part:
first step:
(1). Setting of formal parameters :
int arr[] -- the first address of the array ;
int k -- the number to find in the array ;
int sz -- the length of the array .
(2). Define the left and right subscripts -- left and right
Implementation code:
#include <stdio.h> int binary_search(int arr[], int k, int sz)//形参 { int left = 0; //左下标 int right = sz - 1; //右下标 } int main() { return 0; }
Realize the picture:
Step two:
Use binary search method :
(1). Use while loop .
(2). Generate the intermediate value subscript mid :
int mid = left + (right - left) / 2;
left is the smaller side , (right - left) is the difference between the two , (right - left)/2 is half the difference ,
left + (right - left) / 2 , that is, the smaller side plus half of the difference ,
At this time, both sides are the same , and either side is the average value ( middle value )
(3).
If the middle value is less than the value you are looking for ,
Discard the middle value and all numbers to the left of the middle value , and adjust the left subscript left : left = mid + 1 .
If the middle value is greater than the value you are looking for ,
Discard the middle value and all numbers to the right of the middle value , and adjust the right subscript right : right = mid - 1 .
If the middle value is equal to the value you are looking for ,
Returns the intermediate value subscript mid .
(4). Return -1 if not found .
Implementation code:
int binary_search(int arr[], int k, int sz)//形参 { int left = 0; //左下标 int right = sz - 1; //右下标 //使用while循环 while (left <= right) { //生成中间值下标 mid : int mid = left + (right - left) / 2; //二分查找: if (arr[mid] < k)//中间值 小于 要找的值 { left = mid + 1; //舍弃中间值和中间值左边的所有数, //调整 左下标left :left = mid + 1。 } else if (arr[mid] > k)//中间值 大于 要找的值 { right = mid - 1; //舍弃中间值和中间值右边的所有数, //调整 右下标right :right = mid - 1。 } else if (arr[mid] == k)//中间值 等于 要找的值 { return mid; //返回中间值下标mid。 } } if (left > right) { return -1; //找不到则返回-1 } }
Realize the picture:
Main function part:
(1). Define an ordered array , set the value to be searched , and find the number of elements in the array .
(2). Call the custom function .
(3). Judge the return value of the custom function and print the corresponding situation .
Implementation code:
#include <stdio.h> int binary_search(int arr[], int k, int sz)//形参 { int left = 0; //左下标 int right = sz - 1; //右下标 //使用while循环 while (left <= right) { //生成中间值下标 mid : int mid = left + (right - left) / 2; //二分查找: if (arr[mid] < k)//中间值 小于 要找的值 { left = mid + 1; //舍弃中间值和中间值左边的所有数, //调整 左下标left :left = mid + 1。 } else if (arr[mid] > k)//中间值 大于 要找的值 { right = mid - 1; //舍弃中间值和中间值右边的所有数, //调整 右下标right :right = mid - 1。 } else if (arr[mid] == k)//中间值 等于 要找的值 { return mid; //返回中间值下标mid。 } } if (left > right) { return -1; //找不到则返回-1 } } int main() { int arr[] = { 1,2,3,4,5,6,7,8,9,10 }; //定义有序数组 int k = 7; //设置要查找的值 int sz = sizeof(arr) / sizeof(arr[0]); //求出数组元素个数 // 整个数组大小 / 单个数组元素大小 = 数组元素个数 //调用自定义函数: int ret = binary_search(arr, k, sz); //ret接收返回的下标 //判断自定义函数的返回值,打印相应的情况: if (ret == -1) //未找到,返回-1 { printf("找不到\n"); } else { printf("找到了,下标是:%d\n", ret); } return 0; }
Realize the picture:
Final code and implementation effect
Final code:
#include <stdio.h> int binary_search(int arr[], int k, int sz)//形参 { int left = 0; //左下标 int right = sz - 1; //右下标 //使用while循环 while (left <= right) { //生成中间值下标 mid : int mid = left + (right - left) / 2; //二分查找: if (arr[mid] < k)//中间值 小于 要找的值 { left = mid + 1; //舍弃中间值和中间值左边的所有数, //调整 左下标left :left = mid + 1。 } else if (arr[mid] > k)//中间值 大于 要找的值 { right = mid - 1; //舍弃中间值和中间值右边的所有数, //调整 右下标right :right = mid - 1。 } else if (arr[mid] == k)//中间值 等于 要找的值 { return mid; //返回中间值下标mid。 } } if (left > right) { return -1; //找不到则返回-1 } } int main() { int arr[] = { 1,2,3,4,5,6,7,8,9,10 }; //定义有序数组 int k = 7; //设置要查找的值 int sz = sizeof(arr) / sizeof(arr[0]); //求出数组元素个数 // 整个数组大小 / 单个数组元素大小 = 数组元素个数 //调用自定义函数: int ret = binary_search(arr, k, sz); //ret接收返回的下标 //判断自定义函数的返回值,打印相应的情况: if (ret == -1) //未找到,返回-1 { printf("找不到\n"); } else { printf("找到了,下标是:%d\n", ret); } return 0; }
Realize the effect: