Binary Search Implementation

Nearly done with a project, and sadly I had missed the day involving the binary search. I have an array with 100 random integers. I then used selection sort on the random array, now I have to use binary search on the sorted array. I am having issue implementing it...maybe its written wrong, maybe I should of been a truck driver, IDK. I am trying to call it in the random fcn like I did with the sort fcn. Any help will be appreciated. Thanks. (first time poster).

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
  // Jonathan Rogers
// 11/23/15
// Project 4

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <time.h>
#include <fstream>



using namespace std;

int randomizer(int random[]);
void selectionSort(int random[], int arraySize);
int binary_search(int random[], int low, int high, int search);


int main(){


	const int arraySize = 100;
	int random[arraySize];

	randomizer(random);
	system("pause");

	
	


}

void selectionSort(int array[], int arraySize)
{
	

	for (int i = 0; i < arraySize; i++)
	{
		
		for (int j = i + 1; j < arraySize; j++)
		{
			if (array[j] > array[i])
			{
				int temp = array[i];
				array[i] = array[j];
				array[j] = temp;
			}
		}
	}
}

int binary_search(int random[], int low, int high, int search)
{
	int mid;
	
	if (low > high)
		return -1;

	while (low <= high){
		{
			 mid = (low + high) / 2;

			if (search == random[mid])
				return mid;
			else

				if (search < random[mid])
					return binary_search(random, low, mid - 1, search);
				else
					return binary_search(random, mid + 1, high, search);

		}

	} // end if
	return -1;
}// end binarySearch




int randomizer(int random[]){


	srand((unsigned)time(NULL));

	ofstream PRINT("P4Output.txt");
	const int arraySize = 100; //size of the array
	int high = 100; // high for random numbers
	int low = -100; // low for random numbers
	int size = 0;
	int out = 0;
	random[arraySize]; // giving the array random 100 elements
	int first = 0;
	int last = 0;
	int index = 0;
	int search = 0;




	cout << endl << "                    RANDOM:                                  " << endl;
	cout << " *************************************************" << endl << endl << endl;
	PRINT << endl << "                   RANDOM:                                  " << endl;
	PRINT << " ************************************************" << endl << endl << endl;

	for (int i = 0; i < 100; i++){ // Start of loop to insert 100 random values into array
		random[i] = rand() % (high - low + 1) + low; //Inserting random values into array

		cout << setw(5) << random[i]; // outputs the random integer with spacing.
		PRINT << setw(5) << random[i];

		if ((i + 1) % 10 == 0) {

			cout << endl << endl << endl; // end of print line for set of 10 values
			PRINT << endl << endl << endl;
		}// end of if statement

	} // End for-loop

	cout << "                  SELECTION SORT:                                  " << endl;
	cout << " *************************************************" << endl << endl << endl;
	PRINT << "                 SELECTION SORT:                                  " << endl;
	PRINT << " ************************************************" << endl << endl << endl;

	selectionSort(random, arraySize); // calling selection sort

	for (int j = 0; j < arraySize; j++){ // for-loop
		cout << setw(5) << random[j]; //outputs integers w/ spacing

		if ((j + 1) % 10 == 0){ // sets rows of ten
			cout << endl << endl;
		}
	}// end of for-loop


	cout << "                 BINARY SEARCH                    " << endl;
	cout << " *************************************************" << endl << endl << endl;
	PRINT << "                 BINARY SEARCH                    " << endl;
	PRINT << " *************************************************" << endl << endl << endl;

	binary_search(random, -100, 100 - 1, search);
	int choose = 0;

	for (int i = 0; i < arraySize; i++){

		while (choose != 101){

			cout << "Enter number to search for: ";
			cin >> choose;

			if (choose == search){
				cout << "Your number was found. " << endl;
			}
			else{
				cout << " Your number was not found. " << endl;

			}
		}
	}
	return out;

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int binary_search( const int random[], int low, int high, int search ) ; // const: make it const-correct
    
int binary_search( const int random[], int low, int high, int search ) { // const: make it const-correct

    if( low > high ) return -1;

    const int mid = ( low + high ) / 2;

    if( search == random[mid] ) return mid ; // found at position mid

    // the sequence is sorted on *descending order*
    // so, if search > random[mid], the searched for value,  if it is present, 
    // would be in the lower part of the sequence; would be in [ low, mid-1 ]
    else if( search > random[mid] ) return binary_search( random, low, mid-1, search );

    // if search < random[mid], the searched for value, if it is present would be in [ mid+1, high ]
    else return binary_search( random, mid+1, high, search );
}// end binarySearch 


To search for a particular value:
1
2
3
4
5
6
7
std::cout << "Enter number to search for: ";
std::cin >> choose;

const int pos = binary_search( random, 0, arraySize-1, choose ) ;

if( pos != -1 ) std::cout << choose << " was found at position " << pos << '\n' ;
else std::cout << choose << " was not found\n" ;
Appreciate the help. The Binary search works, and I REALLY appreciate the comments. Sometimes the brain gets scrambled after coding so long and those really helped!
Topic archived. No new replies allowed.