#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;
}
}
}
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:
Post a Comment