我被要求用一些特定的條件編寫一個二進制搜索程序。我必須傳遞下限,上限,指向數組的指針和搜索元素。我寫的程序給了我警告。我無法糾正程序中的錯誤。請指出我錯在哪裏。 #include <stdio.h>
int BinarySearch(int , int , int *, int);
int main()
{
int n, i, a[20], h, l, x, r=0;
這段代碼有什麼問題?無法使用二分查找搜索連續數組中缺失的數字。 a = [1,2,3,4,5,7,8]
lent = len(a)
beg =0
end = lent-1
while beg < end:
mid = (beg + end)/2
if (a[mid]-a[beg])==(mid - beg):
beg = mid + 1
els
對於有序排列的int數組,BS算法返回鍵的前任。例如,給定數組{3,5,7,9,11},BS(15)= 11,BS(9)= 7。當前輩不存在於陣列(例如BS(3))。我認爲解決方案是二進制搜索算法的變體,但我無法得到正確的解決方案。這是我到目前爲止,這是不正確的: int BinarySearch5(vector<int> &data, int left, int right, int key)
我有一個少數重複元素的數組,我想找到重複元素的索引這最接近數組的末尾。 #include<iostream>
uisng namespace std;
int main()
{
int arr[15]={1,2,3,4,5,6,7,8,8,8,9,10,11,12,13}; // 8 is repeating 3 times
// lets find the index of