Re: Algorithm learning from scratch [1] Dichotomy search

#include<iostream>
#include<string>
using namespace std;


string search(int a[],int num)
{
	int low = 0;
	int high = sizeof(a)-1;
	while(low <= high)
	{
	int mid = (low + high)/2;
	int guess = a[mid];
	if(guess == num )
	{
		return "found";
	}
	if(guess > num)
	{
		high = mid - 1;
	}else{
		low = mid +1;
	}
	
	}
	return "Not found";
}


intmain()
{
	int b;
	cin >> b;
	int a[] =  {1,3,5,7,9,11};
	cout << search(a,b) <<endl;
	
 }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325941136&siteId=291194637