Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std; //gumamit naak std ffs ;<
- int binarysearch(const int, int array[], int, int, int);
- int main(){
- const int size = 5;
- int array[size] = {2, 3, 4, 10, 40};
- int search = 10; //search in array
- int searchindex = binarysearch(size, array, search, 0, 0); //added 00 as tally numbers for recursive looping
- if (searchindex != -1){
- cout << "Element " << array[searchindex] << " present at index " << searchindex;
- }
- else {
- cout << "Element is not present in array";
- }
- return 0;
- }
- int binarysearch(const int size, int array[], int search, int h, int i){
- if (h < size){ //condition else search value isn't on any array index
- int middle = (((size -h) /2) +i); //an tally kanina para adto dd ha calculations
- if (array[middle] == search){
- return middle;
- }
- else if (array[middle] < search){
- h = middle; //increase tally
- i = middle;
- if (h == 4){ //deja vu, parihas reason han interative kanina
- h++;
- }
- return binarysearch(size, array, search, h, i); //return with new tally numbers
- }
- else if (array[middle] > search){
- h = middle;
- if (h == 1){ //same reason as line 39
- h = 4;
- }
- else if (h == 0){ //same reason as line 44
- h = 5;
- }
- return binarysearch(size, array, search, h, i);
- }
- }
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement