-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch.cpp
More file actions
111 lines (92 loc) · 2.21 KB
/
Search.cpp
File metadata and controls
111 lines (92 loc) · 2.21 KB
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
//I Ayne Delgado have not used any code other than my own (or that in the textbook) for this project.
//I also have not used any function or data-structure from the Standard-Template Library.
//I understand that any violation of this disclaimer will result in a 0 for the project.
//implementation of search header
#include "Search.h"
Search::~Search()
{
if(array!=nullptr)
{
delete[] array;
}
array = nullptr;
}
void Search::set_seed(int seed)
{
srand(seed);
}
int Search::getSize() const
{
return aSize;
}
void Search::init_array()
{
assert(aSize>0);
for(unsigned i = 0; i< aSize; i++)
{
array[i]=rand()/DIVIDER;
}
}
void Search::init_sorted_array()
{
assert(aSize>0);
array[0]= rand() % SORT_DIVIDER;
for(unsigned i =0; i<aSize; i++)
{
array[i+1] = array[i] + rand() % SORT_DIVIDER;
}
}
bool Search::sequential_find(int num)
{
for(int i=0; i<aSize; i++)
{
if(array[i] == num)
return true;
}
return false;
}
bool Search::iterative_binary_find(int num)
{
if(aSize==0)
return false;
int start = 0;
int last = aSize;
int mid = (aSize/ARRAY_DIVIDER) -1;
while(start <=last)
{
if(array[mid]== num)
return true;
else if(num > array[mid])
{
start = mid+1;
mid = start + ((last - start)/ARRAY_DIVIDER);
}
else if(num< array[mid])
{
last = mid-1;
mid = start + ((last - start)/ARRAY_DIVIDER);
}
}
return false;
}
bool Search::recursive_binary_find(int num)
{
int first =0;
return recursive_search_helper(num, first, aSize, array);
}
bool Search::recursive_search_helper(int num, int first, int size, int* theArray) //NEED CONSTS
{
int mid;
if(size==0)
return false;
else
{
mid=first + size/ARRAY_DIVIDER;
if(num==theArray[mid])
return true;
else if(num<theArray[mid])
return recursive_search_helper(num, first, size/ARRAY_DIVIDER, theArray);
else
return recursive_search_helper(num, mid+1, (size-1)/ARRAY_DIVIDER, theArray);
}
}