Binary Search Help.

I've got the logic right and the algo seems to work fine. Now, that's epic for me :D But..

I want to add 2 more features to this code:

1) Exit on pressing 'x'. For now I'm using -1 to exit.
2) Print a message "Element not found" when an element not present in the array is searched for. Yeah, I'm not able to figure that out :|
Well, Here goes my code:

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
53
54
55
56
57
58
59
60
61
#include<iostream>
using namespace std;

int BinSearch(int data[], int numElements, int searchKey);   // Prototype

int main()
{
	int data[] = {1, 4, 5, 6, 9, 14, 21, 23, 28, 31, 35, 42, 46, 50, 53, 57, 62, 63, 65, 74, 79, 89, 95};
	int numElements = 23;		// Size of the Array is 23!!!
	int searchKey;
	int found;

	for(int i = 0; i < numElements; i++)
		cout << data[i] << ", ";
	cout << endl;

	for(;;)
	{
	cout << "Enter search key: (-1 to exit) ";    // I want to exit when I press 'x'. Please let me know.
	cin >> searchKey;
	
	if(searchKey == -1)
	{
		cout << "Exiting\n";
		break;
	}

	found = BinSearch(data, numElements,searchKey);
	
	cout << searchKey << " is in position " << found << endl;
	}

}

int BinSearch(int data[], int numElements, int searchKey)	
{
	int position, mid;
	int beg = 0;
	int end = numElements;

	// Algorithm Begins.
	for(int i = beg; i <= end;)
	{
		mid = (beg+end)/2;

		if(data[mid] == searchKey)
		{	
			return mid;
		}
		else if(data[mid] > searchKey)
		{
			end = mid - 1;
		}
		else if(data[mid] < searchKey)
		{
			beg = mid + 1;
		}
	}
	

}


Thanks,
--Sid
Last edited on
Hi Sid TheBeginner,

This is generally a bad idea


for(;;)

This is better:

1
2
3
4
5
char QuitValue = 'a'; //purposely initialised to something invalid
while (QuitValue != 'x') {
//do stuff

}


you might have to ask a separate question about quitting as well as getting the search key.





Hey TheIdeasMan,
Thanks for stopping by..

I got it running here it is .But....

It goes on asking for a value until -1 is typed in. I want to exit using 'x' Can you help me with that?

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
53
54
55
56
57
58
#include<iostream>
using namespace std;

int BinSearch(int data[], int numElements, int searchKey);		// Prototype

int main()
{
	int data[] = {1, 4, 5, 6, 9, 14, 21, 23, 28, 31, 35, 42, 46, 50, 53, 57, 62, 63, 65, 74, 79, 89, 95};
	int numElements = 23;				 // Size of the Array is 23!!!
	int searchKey;
	int found;

	for(int i = 0; i < numElements; i++)
		cout << data[i] << ", ";
	cout << endl;

	for(;;)
	{
	cout << "Enter search key: (-1 to exit) ";	// I want to quit when I press 'x'. Please let me know.
	cin >> searchKey;
	
	if(searchKey == -1)
	{
		cout << "Exiting\n";
		break;
	}
	found = BinSearch(data, numElements,searchKey);

	if(found == -1)
		cout << "Not Found!\n";
	
	else cout << searchKey << " is in position " << found << endl;
	}

}

int BinSearch(int data[], int numElements, int searchKey)	
{
	int mid;
	int beg = 0;
	int end = numElements;

	// Algorithm Begins.
	while(beg <= end)
	{
		mid = (beg+end)/2;

		if(data[mid] == searchKey)
			return mid;

		else if(data[mid] > searchKey)
			end = mid - 1;

		else if(data[mid] < searchKey)
			beg = mid + 1;
	}
	return -1;
}



Thanks,
--Sid
Last edited on
Sid

The answer to your question was the purpose of my last post:

1
2
3
4
5
6
char QuitValue = 'a'; //purposely initialised to something invalid
while (QuitValue != 'x') {
//do stuff

}


and this

you might have to ask a separate question about quitting as well as getting the search key.


Was the other clue.

Another clue is that you put questions about the search key and quitting inside the while loop I gave you .


Topic archived. No new replies allowed.