程序中数组转为二进制文件
Problem statement: Write a c program that will take input a sorted array and to binary search to find the element to be searched. (Output proper prompt messages in case element not found).
问题陈述:编写一个c程序,该程序将输入经过排序的数组,并进行二进制搜索以找到要搜索的元素 。 (如果找不到元素,则输出正确的提示消息)。
Algorithm:
算法:
Binary search actually divides the entire array into two halves at each iteration and do the search in one half based on the searching element since the array is sorted.
二进制搜索实际上在每次迭代时将整个数组分为两半,并且由于对数组进行了排序,因此基于搜索元素将搜索进行了一半。
Let,
让,
Starting index=0
起始索引= 0
End index=n-1, where n is the array length
End index = n-1 ,其中n是数组长度
Middle index= (start + end index)/2= (n-1)/2, this middle index divides the array into two half
中间索引=(开始+结束索引)/ 2 =(n-1)/ 2 ,该中间索引将数组分为两半
If array[middle]==search value
如果array [middle] ==搜索值
We are done
我们完了
Else if array [middle] < search value
否则,如果数组[middle] <搜索值
Then we need to search in the upper half
然后我们需要在上半部分搜索
Else if array [middle]>search value
否则,如果数组[middle]>搜索值
Then we need to search in the lower half
然后我们需要搜索下半部分
These three cases need to be handled recursively to implement a binary search.
这三种情况需要递归处理以实现二进制搜索。
Clearly, these searching method is possible due to the sorted input array.
显然,由于排序的输入数组,这些搜索方法是可能的。
Function definition:
功能定义:
We can build up a recursive binSearch()function which will do the binary search.
我们可以建立一个递归的binSearch()函数来执行二进制搜索。
Function binSearch(input array,start index, end index,search value)
middle index=(start index + end index)/2;
//if search value found return position(1-indexed position)
IF array[middle index] == search value
return middle index+1;
//if search value>array[middle index], search in the segment
//(middle index+1,end index), i.e., the upper half &
//search until start index==end index
ELSE IF (search value>array[middle index] &start index!= end index)
return binSearch(input array,middle index+1,end index,search value);//recursive call
//if search value<array [middle index] search in the segment
//(start index, middle index-1), i.e., the lower half& search until start index== end index
ELSE IF(search value<array[middle index] &start index!= end index)
return binSearch(input array, start index,middle index-1,seach value);
//search value not found as start index == end index
ELSE
return 0;
END Function
Example & Explanation:
示例与说明:
Let the input array be:
2 5 8 9 12 13 16, length, n=7
Search element: 5
//In main function:
Make call binSearch(array, 0,7,5)
binSearch (array, 0, 7, 5)
middle index =(0+7)/2=3
array[3]!=5
array[3]>5&& 0<7
make callbinSearch(array, 0, 3-1)
binSearch(array, 0, 3-1)
middle index =(0+2)/2=1
array[1]==5
return (1+1) //position 2
Program terminated
用于排序数组的二进制搜索的C ++实现 (C++ implementation for binary search on sorted array)
#include <stdio.h>
#include <stdlib.h>
//binary search implementation using recursion
int binSearch(int* a,int first,int last,int t){
//find middle index
int mid=(first+last)/2;
//if data found return position(1-indexed position)
if(a[mid]==t)
return mid+1;
//if data > a[mid] search in the segment (mid+1,last)
else if(t>a[mid] & first!=last)//search until first!=last
return binSearch(a,mid+1,last,t);
//if data < a[mid] search in the segment (first,mid-1)
else if(t<a[mid] & first!=last)
return binSearch(a,first,mid-1,t);
else
return 0;
}
int main()
{
int n,t;
printf("enter number of array elements: ");
scanf("%d",&n);
int* a=(int*)(malloc(sizeof(int)*n));
printf("enter elements: \n");
//input array elements
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
printf("enter element to search\n");
scanf("%d",&t);
//output result
if(binSearch(a,0,n-1,t))
printf("\n%d found at position %d\n",t,binSearch(a,0,n-1,t));
else
printf("\n%d not found\n",t);
return 0;
}
Output (first run)
输出(首次运行)
enter number of array elements: 7
enter elements:
2 5 8 9 12 13 16
enter element to search
5
5 found at position 2
Output (second run)
输出(第二次运行)
enter number of array elements: 6
enter elements:
11 34 45 56 67 78
enter element to search
20
20 not found
翻译自: https://www.includehelp.com/c-programs/accept-sorted-array-and-do-search-using-binary-search.aspx
程序中数组转为二进制文件