Forwarded from ❥͢ ❈↡< C++ > برمجة (❥ツ)
إ₰...👨🏻💻CODE👩🏻💻...₰❥
#Binary_Search_Algorithm
خـوارزمــية البــحث الـثــنائـي:-
#include <iostream>
using namespace std;
int BinarySearch(int array[], int Size, int SearchValue)
{
int low = 0;
int high = Size - 1;
int mid ;
while (low <= high)
{
mid = (low + high) / 2;
if (SearchValue == array[mid])
{
return mid;
}
else if (SearchValue > array[mid])
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return -1;
}
int main()
{
int a[] ={11, 32, 43, 54, 65, 76, 87, 98};
int value;
cout << "enter an integer :" << endl;
cin >> value;
int result = BinarySearch(a, 8, value);
if (result >= 0)
{
cout << "the number " << a[result] << " was found at the element with index " << result << endl;
}
else
{
cout << "the number " << value << " was not found " << endl;
}
}
•┈┈┈•❈••✦✾✦••❈•┈┈┈•
❥➺┊ @barrmaja↷
•┈┈┈•❈••✦✾✦••❈•┈┈┈•
إ₰...Output....₰❥
enter an integer :
3
the number 3 was not found [Program finished]
-*-*-*-*-*-*-*-*-*-*-*-*-*
enter an integer :
98
the number 98 was found at the element with index 7
[Program finished]
#Binary_Search_Algorithm
خـوارزمــية البــحث الـثــنائـي:-
#include <iostream>
using namespace std;
int BinarySearch(int array[], int Size, int SearchValue)
{
int low = 0;
int high = Size - 1;
int mid ;
while (low <= high)
{
mid = (low + high) / 2;
if (SearchValue == array[mid])
{
return mid;
}
else if (SearchValue > array[mid])
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return -1;
}
int main()
{
int a[] ={11, 32, 43, 54, 65, 76, 87, 98};
int value;
cout << "enter an integer :" << endl;
cin >> value;
int result = BinarySearch(a, 8, value);
if (result >= 0)
{
cout << "the number " << a[result] << " was found at the element with index " << result << endl;
}
else
{
cout << "the number " << value << " was not found " << endl;
}
}
•┈┈┈•❈••✦✾✦••❈•┈┈┈•
❥➺┊ @barrmaja↷
•┈┈┈•❈••✦✾✦••❈•┈┈┈•
إ₰...Output....₰❥
enter an integer :
3
the number 3 was not found [Program finished]
-*-*-*-*-*-*-*-*-*-*-*-*-*
enter an integer :
98
the number 98 was found at the element with index 7
[Program finished]