Implemantation of Binary Search in c++


Hey Folks!I am new to c++.As self motivated project i decided to do a binary search?
This program works for key elements which are in the list provide by the user.

But the problem starts when the key elements is not in the list.It gives me an error stating that "binary.exe stopped working!".

I use Dev CPP.I also tried debugging with visual c++ .It gave an 'Unhandled excepton!Stack overflow!'
Please can anyone check my code below?
Also I welcome any criticism on the efficiency of my code!
Iam new to C++ .So iam keen on learning!Please tell me also if there is better way to write this program (without using stl)

I am SORRY if this is silly problem! I Did it without consulting a manual :)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
 #include <iostream>
#include <cstdlib>
using namespace std;
void search_binary(int *a,int key,int n)
{   int low=0,high = n;
	int b[13];
	for(int i=0;i<n;i++)
	{
		b[i]=a[i];
	}
	int mid = (high - low)/2;

	if(key==a[mid])
	cout<<"Ha!Found "<<key<<endl;
	
	else if(key < a[mid]){
	
	n = (mid-low)/2;
	search_binary(a,key,n);
    }
	else if(key>a[mid])
	{
	n=(high-mid)/2;
	search_binary(a,key,n);
	}
	else if(!key)
	{
	cout<<"BOO!Not found\n";
	}


}  

void get_binary()
{  int a[13],n,i,key;
	cout<<"Please Enter the number of elements?(Keep it less than 13!)\n";
	cin>>n;
	cout<<"Enter The Elements\n";
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	cout<<"Please enter the element to be searched\n";
	cin>>key;
	search_binary(a,key,n);
	
}

int main()
{   get_binary();
	return 0;
}
Last edited on
Line 26 says "if key is equal to 0" but you usually are not searching for 0.
Last edited on
Here are few problems I found
0. tiny problem: you declare i twice. it will waste some memories. And I dont think you should pass array a by reference since you are not changing a anyway.

1. array b is kind of wasting of space because you are not using it.

2. problem with line 23, assume you have 123456789 10 11 12 13, and you want to find 12, when you execute program once, mid is (13-0)/2 = 6, n will be (13 - 6)/2 =3 which does not make any sense because you should search array[7] and above. Same concept with line 11/18

3. you should tell user the input should be in ascending order, otherwise if they type random number, your if statement in search function will not make any sense.

4. You miss the base case for recursive search function. Look at line 19/24, it will keep get new value of n to infinite. This kind of error is called overflow.

5. so when you have your base case, like when n < 0 || n>12, that means you checked entire array a, you can cout line 28. line 26 does not make sense to my knowledge.

Again, I am a learner just like you. Taking consideration that they might be wrong.

I am learning c++ just like you(my primary language is java), but I do not what to do. You seems like have idea about what to do and how to implement that. If you dont mind, can you message me a private message, so we can work together, to make some simple but interesting programs to improve our c++ skills?
Last edited on
Topic archived. No new replies allowed.