By Aalok Kumar
Hello Developers, An algorithm is a procedure for solving a problem, based on a sequence of some actions. I've implemented Binary Search Algorithm in C++.
In my program, there is a function int binary_Search(vector arr, int key).
This function returns an integer, which is the index of a searched key, -1 if not found.
Now coming to the explanation of code,
It repeatedly searches for the key in the array by dividing the search interval into two halves.
If the key is less than the value at the midpoint of the interval, then it searches in the lower half interval and vice-versa.
Here is the sample output:
Enter the size of the array: 6 Enter elements of the array: 1 2 3 4 5 6 Enter element to search: 5 Element 5 found at index 4. Enter the size of the array: 10 Enter elements of the array: 2 5 7 9 10 13 18 25 44 51 Enter element to search: 13 Element 13 found at index 5. Enter the size of the array: 5 Enter elements of the array: 5 8 11 15 16 Enter element to search: 3 Element 3 not found in the array.
Note: Array must be sorted.
Submitted by Aalok Kumar (Aaloks766626)
Download packets of source code on Coders Packet
Comments