using namespace std;
void bubble_sort(vector<int>&);
int main()
{
// binary Search Variables and vector
int beg, end, mid, item;
string choose = "y";
vector<int> binNums;
//Bubble sort variables and vector
int num, val;
vector<int> bbSort;
//Bubble sort block
cout << "Enter the number of elements : ";
cin >> num;
for (int i = 0; i < num; i++) {
cout << "Enter the element " << i + 1 << ": ";
cin >> val;
bbSort.push_back(val);
}
bubble_sort(bbSort);
cout << "List of sorted elements: " << endl;
for (int i = 0; i < num; i++) {
cout << bbSort[i] << " ";
}
cout << endl;
//Binary search block
binNums = bbSort;
while (choose == "y")
{
cout << "\nEnter Item you want to Search: ";
cin >> item;
beg = 1;
end = num;
mid = (beg + end) / 2;
while (beg <= end && binNums[mid] != item)
{
if (binNums[mid]<item)
beg = mid + 1;
else
end = mid - 1;
mid = (beg + end) / 2;
}
if (binNums[mid] == item)
{
cout << "\nElement found at location : " << mid + 1 << endl;
}
else
{
cout << "Element not found";
}
cout << "Do you want to search for another Item(y OR n)? ";
cin >> choose;
}
return 0;
}
void bubble_sort(vector<int>& a)
{
for (int i = a.size(); i > 0; i--)
{
for (int j = 0, k = 1; k < i; j++, k++)
{
if (a[j] > a[k])
{
int swap = a[j];
a[j] = a[k];
a[k] = swap;
}
}
}
}