Call and Search Help

I need help finishing the calling and searching for the binary and recursive areas. I have linear done but I don't know how to do the other two because of some overlap issues I keep encountering.
Thanks in Advance.

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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228

#include <iostream>
#include <string>
#include <chrono>
#include <fstream>
#include <vector>

using namespace std;
using namespace std::chrono;

typedef std::chrono::time_point<system_clock> timer;
typedef double Second;

void TriggerTimer(timer&);
Second TimerCountInSecond(timer, timer);

bool LinearSearch(const vector<int>&, int);
bool BinarySearchRecursive(const vector<int>&, int, int, int);
bool BinarySearch(const vector<int>&, int);
bool ReadFileToVector(vector<int>&, string);
void TimingSearch(const vector<int>&, int, string);

int main()
{
    vector<string> fileNames = {    "data_10_sorted.txt",
                                    "data_2000_sorted.txt" ,
                                    "data_10000_sorted.txt" ,
                                    "data_100000_sorted.txt" };
    for (int i = 0; i < fileNames.size(); i++)
    {
        string fileName = fileNames[i];
        vector<int> sortedData;

        //Populate the vector with data from data file: 
        if (ReadFileToVector(sortedData, fileName) == true)
        {
            string searchType = "linear";
            int notExistItem = 7777777;
            int existItem = sortedData[0.8 * sortedData.size() - 1];
     
            cout << "+++++++++++++++++++++++++++++++++++++" << endl;
            cout << " Searching on " << fileName << endl;
            cout << "+++++++++++++++++++++++++++++++++++++" << endl;

            //0a. Timing LINEAR search for non-existing item:
            TimingSearch(sortedData, notExistItem, searchType);

            //0b. Timing LINEAR search for existing item:
            TimingSearch(sortedData, existItem, searchType);

            //1a. Timing BINARY search for non-existing item:
          
            //1b. Timing BINARY search for existing item:
         
            //2a. Timing BINARY RECURSIVE search for non-existing item:

            //2b. Timing BINARY RECURSIVE search for existing item:

       
        }
    }

	return 0;
}

/*******************************************************************
 *                       LinearSearch                              *
 * Called by: TimingSearch                                         *
 * Passed   : 2 arguments: a vector of numbers,                    *
 *            and the number to search for                         *
 * Purpose  : Determines if the number to search for is in the set *
 *            of stored numbers                                    *
 * Method   : Uses a linear search                                 *
 * Returns  : true (if the number is found) or false (if not found)*
 *******************************************************************/
bool LinearSearch(const vector<int> &numbers, int numToSearch)
{
    bool isFound = false;
    for (int i = 0; i < numbers.size(); i++)
    {
        if (numbers[i] == numToSearch)
        {
            isFound = true;
            break;
        }
    }

    return isFound;
}

/*******************************************************************
 *                       BinarySearchRecursive                     *
 * Called by: TimingSearch                                         *
 * Passed   : 4 arguments: a vector of numbers, the number to      *
 *            search for, the low index, and the high index        *
 * Purpose  : Determines if the number to search for is in the set *
 *            of stored numbers                                    *
 * Method   : Uses a binary search                                 *
 * Returns  : true (if the number is found) or false (if not found)*
 *******************************************************************/
bool BinarySearchRecursive(const vector<int> &numbers, int numToSearch, int low, int high) // High = numbers.size -1 Low= 0
{
    if (low > high)
    {
        return false;
    }

    int mid = (low + high) / 2;
    if (numbers[mid] < numToSearch) 
    {
        return BinarySearchRecursive(numbers, numToSearch, mid + 1, high);
    }
    else if (numbers[mid] > numToSearch) 
    {
        return BinarySearchRecursive(numbers, numToSearch, low, mid - 1);
    }
    else
    {
        return true;
    }
}

/*******************************************************************
 *                       BinarySearch                              *
 * Called by: TimingSearch                                         *
 * Passed   : 2 arguments: a vector of numbers,                    *
 *            and the number to search for                         *
 * Purpose  : Determines if the number to search for is in the set *
 *            of stored numbers                                    *
 * Method   : Uses a binary search                                 *
 * Returns  : true (if the number is found) or false (if not found)*
 *******************************************************************/
bool BinarySearch(const vector<int> &numbers, int numToSearch)
{
    bool isFound = false;
    int mid = 0;
    int low = 0;
    int high = numbers.size() - 1;

    while (high >= low) 
    {
        mid = (high + low) / 2;
        if (numbers[mid] < numToSearch) 
        {
            low = mid + 1;
        }
        else if (numbers[mid] > numToSearch) 
        {
            high = mid - 1;
        }
        else 
        {
            isFound = true;
            break;
        }
    }

    return isFound;
}

void TriggerTimer(timer& timeContainer)
{
    timeContainer = std::chrono::system_clock::now();
}

Second TimerCountInSecond(timer startTime, timer endTime)
{
    std::chrono::duration<Second> elapsed_seconds = endTime - startTime;
    return elapsed_seconds.count();
}

bool ReadFileToVector(vector<int> &numbers, string fileName)
{
    bool isReadGood = false;
    int number;
    fstream inFile;
    inFile.open(fileName, ios::in);
    if (inFile.fail())
    {
        cout << "File open failed." << endl;
    }
    else
    {
        do //Read the file until the end of the file
        {
            inFile >> number;
            numbers.push_back(number);

        } while (inFile.eof() == false);
        isReadGood = true;
    }

    inFile.close();

    return isReadGood;
}

/*******************************************************************
 *                       TimingSearch                              *
 * Called by: main                                                 *
 * Passed   : 3 arguments: a vector of numbers, the number to      *
 *            search for, and the search algorithm to use          *
 * Purpose  : Use a specific search algorithm to show seach        *
 *            runtime                                              *
 * Method   : Uses a search algorithm                              *
 * Returns  : None                                                 *
 *******************************************************************/
void TimingSearch(const vector<int> &sortedData, int searchItem, string searchType)
{
    timer start, end;
    bool isFound = false;    

    TriggerTimer(start);
    if (searchType == "linear")
    {
        isFound = LinearSearch(sortedData, searchItem);
    }
    
    //### binary and recursive search algorithm ###
   
    TriggerTimer(end);
    int toMicroSecond = 1000000;
    cout << "Search Item: " << searchItem << endl;
    cout << "Search Type: " << searchType << " search" << endl;
    cout << "Item found: " << isFound << endl;
    cout << "Elapsed time: " << TimerCountInSecond(start, end) * toMicroSecond << " microsecond(s)" << endl;
    cout << "=====================================" << endl;
}
Last edited on
@Happy2bhere,
other than the fact that you don't actually call either BinarySearch or BinarySearchRecursive from anywhere outside, what is your problem? What do you mean by "overlap issues".
Last edited on
1
2
3
4
5
6
do //Read the file until the end of the file
        {
            inFile >> number;
            numbers.push_back(number);

        } while (inFile.eof() == false);


The ReadFileToVector() also has a problem. It will add a spurious number to the end of the vector.

1
2
while (inFile >> number)
    numbers.push_back(number);

Last edited on
As you're displaying the elapsed time in microseconds, why not have TimerCount return the time in microseconds rather than seconds and then doing a calculation?

1
2
3
4
5
6
7
double TimerCountInMicro(timer startTime, timer endTime)
{
	return std::chrono::duration<double, std::micro>(endTime - startTime).count();
}

...
cout << "Elapsed time: " << TimerCountInMicro(start, end) << " microsecond(s)" << endl;


Topic archived. No new replies allowed.