PROGRAM FOR BINARY SEARCH IN ARRAY.
#include <iostream>
#include <conio.h>
using namespace std;
class Array
{
private:
int *Arr, Size, Num;
public:
Array(int Siz)
{
Arr = new int[Siz];
for (int I = 0; I < Siz; I++)
{
Arr[I] = 0;
}
Num = 0;
Size = Siz;
}
void Input()
{
cout << "Enter Size Of Array: ";
cin >> Num;
cout << "\nEnter Elements Of Array: " << endl;
for (int I = 0; I < Num; I++)
{
cin >> Arr[I];
}
}
void B_Search()
{
bool Find;
int Value, Beg = 0, Mid = 0, Count = 0, End = Num - 1;
cout << "\nEnter any Value you want to Search: ";
cin >> Value;
while (Beg <= End)
{
Mid = (Beg + End) / 2;
if (Arr[Mid] == Value)
{
Find = true;
///Count++; ///Count will count number of occurrences of searched value;
}
else if (Value < Arr[Mid])
End = Mid - 1;
else
Beg = Mid + 1;
}
if (!Find)
cout << "\nValue Not Find in Array...." << endl;
else
cout << "\nValue Found " << Count << " times in Array. " << endl;
}
};
int main()
{
Array Obj1(10);
Obj1.Input();
Obj1.B_Search();
getch();
return 0;
}
0 comments:
Post a Comment