PDA

View Full Version : binary search



ssaammaa00
یک شنبه 30 آذر 1393, 18:02 عصر
سلام :لبخندساده:

من این کد رو واسه binary search نوشتم ولی درس کار نمیکنه میشه بگید مشکل کجاست؟؟

ممنون




#include<iostream>
using namespace std;


int binarysearch(int a[], int size, int x);


int main()
{


int x;
cin >> x;


const int size = 10;
int a[size] = { 1, 5, 83, 48, 500, 62, 72, 80, 98, 10 };


cout << binarysearch(a, size, x);


system("pause");
}


int binarysearch(int a[], int size, int x)
{
for (int i = 0;; i++)
{
if (a[i] == x)
{
return i;
break;
}
else if (x > a[i])
i = (10 + i) / 2;


else
i = i / 2;
}

}