Pages

💡 Now You Can Get your Assignments Sollution here... .💡 100% plagiarism Free Service...💡 Responsibility, punctuality and on-time delivery... 💡 Furnishing you with experts... 💡 experienced in your subject 100% privacy... 💡 helping you with the highest efficiency , professionalism Affordable and budget-friendly costs that do not make a hole in your wallet

Binary search

#include<iostream>
using namespace std;
void main()
{
int arr[10],val=0;
for (int i = 0; i < 10; i++)
{
arr[i]=rand()%500;

}
int temp = arr[0];
for (int i = 0; i < 10; i++)
{
for (int j = i; j < 10; j++)
{

if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for (int i = 0; i < 10; i++)
{
cout << arr[i] << "\n";

}
cout << "enter value to search:";
cin >> val;
int s=0, m, e=9;
m = (s + e) / 2;
while (s != e)
{

if (val > arr[m])
{
s = m + 1;
m = (s + e) / 2;
}
if (val < arr[m])
{
e = m;
m = (s + e) / 2;
}
if (val == arr[m] || s == e)
{
cout << "value found\n";
break;
}
}
}

No comments: