diff --git a/6_Searching/binary_search_c++.cpp b/6_Searching/binary_search_c++.cpp new file mode 100644 index 0000000..2991611 --- /dev/null +++ b/6_Searching/binary_search_c++.cpp @@ -0,0 +1,39 @@ +#include +using namespace std; + +void binary_search(int ar[], int n, int x) // fucntion for binary search with three parametes as array,size of array,no to be searched +{ + int l = 0; + int r = n - 1; + while (l <= r) + { + int m = l + (r - l) / 2; + + // Check if x is present at mid + if (ar[m] == x) + { + cout << "Element is Present in the Array at index: " << m << "\n"; + return; + } + + // If x greater, ignore left half + if (ar[m] < x) + l = m + 1; + + // If x is smaller, ignore right half + else + r = m - 1; + } + cout << "Element is not Present in the Array\n"; // if element is not present in the array +} + +int main() // main method +{ + int arr[5] = {8, 16, 4, 78, 44}; // Creating an array of size 5 + int to_be_searched = 78; // the number to be searched + binary_search(arr, 5, to_be_searched); // function call for binary search + + int arr1[5] = {18, 6, 41, 8, 21}; // Creating an array of size 5 + int to_be_searched1 = 2; // the number to be searched + binary_search(arr1, 5, to_be_searched1); // function call for binary search +} diff --git a/6_Searching/linear_search_c++.cpp b/6_Searching/linear_search_c++.cpp new file mode 100644 index 0000000..74ed5d9 --- /dev/null +++ b/6_Searching/linear_search_c++.cpp @@ -0,0 +1,37 @@ +#include +using namespace std; + +void linear_search(int ar[], int n, int x) // fucntion for linear search with three parametes as array,size of array,no to be searched +{ + int found = 0; // flag variable + int index = -1; + for (int i = 0; i < n; i++) + { + if (ar[i] == x) // checking if element is equal to the no to be searched + { + found = 1; // updating the flag + index = i; // storing the index of the element found + break; // since the element is found therefore no need to traverse further thus exiting the loop + } + } + + if (found == 1) // if element is present in the array then flag status is 1 + { + cout << "Element is Present in the Array at index: "<