-
Notifications
You must be signed in to change notification settings - Fork 0
/
Indexed.cpp
217 lines (189 loc) · 5.98 KB
/
Indexed.cpp
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
#include <iostream>
// Unnecessary, but it's used to format the output of printIndex().
#include <iomanip>
using namespace std;
void printArray();
void sortArray();
void printIndex();
void createIndex(int sectionSize);
int indexedSearch(int value);
const int arrSize = 11;
int arr[arrSize] = {1, 15, 54, 92, 165, 296, 462, 512, 654, 789, 888};
int main()
{
// Filling the array with random numbers...
for (int i = 0; i < arrSize; i++)
arr[i] = rand() % 100;
cout << "Array:\n";
sortArray();
printArray();
cout << "Index:\n";
createIndex(3);
printIndex();
cout << "Enter A Number To Search For: ";
int x;
cin >> x;
cout << "\nThe Number At Index: " << indexedSearch(x);
return 0;
}
void printArray()
{
for (int i = 0; i < arrSize; i++)
cout << arr[i] << "\t";
cout << "\n";
}
void sortArray()
{
/**
* Selection Sort (any sorting algorithm can be used here).
*/
for (int i = 0; i < arrSize; i++)
{
int minPos = i;
for (int j = i; j < arrSize; j++)
if (arr[j] < arr[minPos])
minPos = j;
/**
* Swapping
*/
int temp = arr[i];
arr[i] = arr[minPos];
arr[minPos] = temp;
}
}
/**
* This structure is used to represent a section in the index.
* Each section has a `start` value and an `index` value.
* The `start` value is the first value of the corresponding section in the original array.
* The `index` value is the index of that value in the original array.
*/
struct Section
{
int start;
int index;
};
Section *sections;
int sectionsCount;
/**
* This function is used to create an index for an array, to speed up search operations.
* The index is divided into sections, where each section represents a segment of the original array.
* The size of these sections is determined by the `sectionSize` argument.
*/
void createIndex(int sectionSize)
{
/**********************************************************
* Example:
* ﹉﹉﹉﹉﹉
* arr = {1, 15, 54, 92, 165, 296, 462, 512, 654, 789, 888}
* ‾ ‾‾ ‾‾‾ ‾‾‾
* arrSize = 11
* sectionSize = 3
* sectionsCount = 11 / 3 = 3.6666 ≈ 4 Sections
*
* Index:
* ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
* ┣━ START INDEX ┃
* ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
* ┣━ 1 ............................ 0 ┃
* ┣━ 92 ............................ 3 ┃
* ┣━ 462 ............................ 6 ┃
* ┣━ 789 ............................ 9 ┃
* ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
**********************************************************/
/**
* This line calculates the number of sections
*/
sectionsCount = arrSize / sectionSize;
/**
* If the size of the original array is not a multiple of the section size,
* it means there are some elements at the end of the array that don't make up a full section.
* So we add one more section to group these elements.
*/
if (arrSize % sectionSize != 0)
sectionsCount++;
/**
* This line allocates memory for an array of `Section` structures.
* Each structure represents a section.
*/
sections = new Section[sectionsCount];
/**
* The for loop iterates over each section.
* For each section:
* - it sets the `start` to the first value of the corresponding segment in the original array.
* - it sets the `index` to the index of that value in the original array. (i * sectionSize)
*/
for (int i = 0; i < sectionsCount; i++)
{
sections[i].start = arr[i * sectionSize];
sections[i].index = i * sectionSize;
}
}
void printIndex()
{
cout << "+-----------+\n";
for (int i = 0; i < sectionsCount; i++)
cout << "| " << setw(2) << sections[i].start << " ... " << setw(2) << sections[i].index << " |\n";
cout << "+-----------+\n";
}
int indexedSearch(int value)
{
/**
* This variable will be used to iterate over the sections in the index.
*/
int sectionNo = 0;
/**
* This while loop continues to increment sectionNo as long as:
* - the sectionNo is less than sectionsCount. (to avoid going out of bounds)
* - the search value is greater than the `start` value of the current section.
* (because we aim to land on a section that starts with a `value` greater than the search value,
* then we search at the section before it)
*/
while (sectionNo < sectionsCount && value > sections[sectionNo].start)
sectionNo++;
/**
* This variable will be used to store the last index of the current section.
*/
int lastIndex;
/**
* Special Case:
*
* If the sectionNo is equal to sectionsCount, it means that the search value may be in the last section.
* So we set lastIndex to the size of the original array.
*/
if (sectionNo == sectionsCount)
{
lastIndex = arrSize;
}
/**
* Special Case:
*
* If (luckily) the search value is equal to the `start` value of the current section,
* we just return the `index` value of the current section.
*/
else if (value == sections[sectionNo].start)
{
return sections[sectionNo].index;
}
/**
* Otherwise, we set lastIndex to the `index` value of the current section.
* (Because the search will start from the beginning of the previous section.)
*/
else
{
lastIndex = sections[sectionNo].index;
}
/**
* Sequential Search (any search algorithm can be used here).
*
* Notice that the search starts from the beginning of the previous section:
* - i =sections[sectionNo - 1].index
*/
for (int i = sections[sectionNo - 1].index; i < lastIndex; i++)
if (arr[i] == value)
return i;
/**
* If the value is not found after all these steps, return -1.
* (This is a common convention to indicate that the searched value was not found.)
*/
return -1;
}