質問編集履歴
1
初めての投稿で。コードの貼り方がわからなかったので、なおしました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,4 +1,428 @@
|
|
1
|
+
```ここに言語を入力
|
2
|
+
|
3
|
+
/**
|
4
|
+
|
5
|
+
* Searching Project
|
6
|
+
|
7
|
+
* See the Assignment 12 document (CSCI112_A12.pdf) for details.
|
8
|
+
|
9
|
+
*
|
10
|
+
|
11
|
+
* Author: N
|
12
|
+
|
13
|
+
*/
|
14
|
+
|
15
|
+
#include <iostream>
|
16
|
+
|
17
|
+
#include <ctime>
|
18
|
+
|
19
|
+
#include <cmath>
|
20
|
+
|
21
|
+
#include <chrono>
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
using namespace std;
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
//Prototypes
|
30
|
+
|
31
|
+
void duplicate(int[], int[], int);
|
32
|
+
|
33
|
+
void printArray(int[], int);
|
34
|
+
|
35
|
+
int linearSearch(int[], int, int);
|
36
|
+
|
37
|
+
int iterativeBinarySearch(int[], int, int);
|
38
|
+
|
39
|
+
int recursiveBinarySearch(int[], int, int,int);
|
40
|
+
|
41
|
+
int jumpSearch(int[], int, int);
|
42
|
+
|
43
|
+
void insertionSort(int[], int);
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
/**
|
50
|
+
|
51
|
+
* Main Function.
|
52
|
+
|
53
|
+
*/
|
54
|
+
|
55
|
+
int main() {
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
//int LENGTH = 200000;
|
60
|
+
|
61
|
+
//int LENGTH = 150000;
|
62
|
+
|
63
|
+
//int LENGTH = 100000;
|
64
|
+
|
65
|
+
//int LENGTH = 75000;
|
66
|
+
|
67
|
+
int LENGTH = 50000;
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
int numbers[LENGTH]; //a single array
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
srand((int)time(0)); //random numbers in the range of 1 through LENGTH*2
|
76
|
+
|
77
|
+
for(int i = 0; i < LENGTH; i++) {
|
78
|
+
|
79
|
+
numbers[i] = rand() % (LENGTH*2) + 1;
|
80
|
+
|
81
|
+
}
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
int value = numbers[rand() % LENGTH + 1]; //randomly choose a number to search
|
86
|
+
|
87
|
+
cout << "value: " << value;
|
88
|
+
|
89
|
+
cout << endl;
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
auto start = chrono::system_clock::now(); //Gets the start time
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
linearSearch(numbers,LENGTH, value);
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
/**
|
102
|
+
|
103
|
+
* Linear Search Algorithm Calculates the total duration
|
104
|
+
|
105
|
+
*/
|
106
|
+
|
107
|
+
chrono::duration<double> totalTime = chrono::system_clock::now() - start; //Calculates the total duration (now - start)
|
108
|
+
|
109
|
+
cout << "Clock stopped." << endl;
|
110
|
+
|
111
|
+
cout << "Finished in linearSearchNumber " << totalTime.count() << " seconds" << endl;
|
112
|
+
|
113
|
+
cout << endl;
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
cout << "Sorting..." << endl;
|
120
|
+
|
121
|
+
insertionSort(numbers, LENGTH);
|
122
|
+
|
123
|
+
cout << "Done." << endl;
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
LENGTH = sizeof(numbers)/4; //Calculates the length of the array
|
128
|
+
|
129
|
+
start = chrono::system_clock::now();
|
130
|
+
|
131
|
+
iterativeBinarySearch(numbers, LENGTH, value);
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
/**
|
136
|
+
|
137
|
+
* Iterative Binary Search Calculates the total duration
|
138
|
+
|
139
|
+
*/
|
140
|
+
|
141
|
+
totalTime = chrono::system_clock::now() - start; //Calculates the total duration (now - start)
|
142
|
+
|
143
|
+
cout << "Clock stopped." << endl;
|
144
|
+
|
145
|
+
cout << "Finished in iterativeBinarySearch " << totalTime.count() << " seconds" << endl;
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
start = chrono::system_clock::now();
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
recursiveBinarySearch(numbers, 0, LENGTH - 1, value);
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
/**
|
158
|
+
|
159
|
+
* Recursive Binary Search Calculates the total duration
|
160
|
+
|
161
|
+
*/
|
162
|
+
|
163
|
+
cout << "Clock stopped." << endl;
|
164
|
+
|
165
|
+
cout << "Finished in recursiveBinarySearch " << totalTime.count() << " seconds" << endl;
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
start = chrono::system_clock::now();
|
170
|
+
|
171
|
+
//Calculates the length of the array
|
172
|
+
|
173
|
+
jumpSearch(numbers, LENGTH, value);
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
/**
|
178
|
+
|
179
|
+
* Jump Search Calculates the total duration
|
180
|
+
|
181
|
+
*/
|
182
|
+
|
183
|
+
cout << "Clock stopped." << endl;
|
184
|
+
|
185
|
+
cout << "Finished in jumpSearch " << totalTime.count() << " seconds" << endl;
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
return 0;
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
void insertionSort(int a[], int length) {
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
for(int i = 1; i < length; i++) {
|
200
|
+
|
201
|
+
int value = a[i];
|
202
|
+
|
203
|
+
int j = i-1;
|
204
|
+
|
205
|
+
while(j >= 0 && a[j] > value) {
|
206
|
+
|
207
|
+
a[j+1] = a[j];
|
208
|
+
|
209
|
+
j--;
|
210
|
+
|
211
|
+
}
|
212
|
+
|
213
|
+
a[j+1] = value;
|
214
|
+
|
215
|
+
}
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
/**
|
222
|
+
|
223
|
+
* Linear Search Algorithm
|
224
|
+
|
225
|
+
*/
|
226
|
+
|
227
|
+
int linearSearch(int a[], int length, int searchValue) {
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
for(int i = 0; i < length; i++) {
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
if(a[i] == searchValue) {
|
236
|
+
|
237
|
+
return i; //The index we found it at
|
238
|
+
|
239
|
+
}
|
240
|
+
|
241
|
+
}
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
return -1;
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
/**
|
254
|
+
|
255
|
+
* Binary Search Algorithm
|
256
|
+
|
257
|
+
*/
|
258
|
+
|
259
|
+
int iterativeBinarySearch(int a[], int length, int searchValue) {
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
int lowBoundary = 0;
|
266
|
+
|
267
|
+
int highBoundary = length - 1;
|
268
|
+
|
269
|
+
while(highBoundary >= lowBoundary) {
|
270
|
+
|
271
|
+
int middle = (highBoundary + lowBoundary) / 2; //Calculate middle index
|
272
|
+
|
273
|
+
if(searchValue == a[middle]) {
|
274
|
+
|
275
|
+
return middle; //Return the index we found it at
|
276
|
+
|
277
|
+
}
|
278
|
+
|
279
|
+
else if(searchValue > a[middle]) {
|
280
|
+
|
281
|
+
lowBoundary = middle + 1; //Raise the low boundary
|
282
|
+
|
283
|
+
}
|
284
|
+
|
285
|
+
else {
|
286
|
+
|
287
|
+
highBoundary = middle - 1; //Lower the high boundary
|
288
|
+
|
289
|
+
}
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
}
|
294
|
+
|
295
|
+
return -1;
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
}
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
/**
|
304
|
+
|
305
|
+
* Recursive Binary Search Algorithm
|
306
|
+
|
307
|
+
*/
|
308
|
+
|
309
|
+
int recursiveBinarySearch(int array[], int first, int last, int value) {
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
if(first > last) {
|
314
|
+
|
315
|
+
return -1; //First base Case
|
316
|
+
|
317
|
+
}
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
int middle = (first + last) / 2; //Calculate the middle position.
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
if(array[middle] == value) {
|
326
|
+
|
327
|
+
return middle; //Second base case
|
328
|
+
|
329
|
+
}
|
330
|
+
|
331
|
+
else if (array[middle] < value) {
|
332
|
+
|
333
|
+
return recursiveBinarySearch(array, middle + 1, last, value); //Recursive case (Search upper partition/upper half)
|
334
|
+
|
335
|
+
}
|
336
|
+
|
337
|
+
else {
|
338
|
+
|
339
|
+
return recursiveBinarySearch(array, first, middle - 1, value); //Recursive case (Search lower partition/lower half)
|
340
|
+
|
341
|
+
}
|
342
|
+
|
343
|
+
}
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
/**
|
350
|
+
|
351
|
+
* Jump Search Algorithm
|
352
|
+
|
353
|
+
*/
|
354
|
+
|
355
|
+
int jumpSearch(int a[], int length, int searchValue) {
|
356
|
+
|
357
|
+
int previous = 0;
|
358
|
+
|
359
|
+
int jump = (int) sqrt(length);
|
360
|
+
|
361
|
+
while(a[(jump < length ? jump : length-1)-1] < searchValue) {
|
362
|
+
|
363
|
+
previous = jump;
|
364
|
+
|
365
|
+
jump += jump;
|
366
|
+
|
367
|
+
if(jump >= length) {
|
368
|
+
|
369
|
+
break;
|
370
|
+
|
371
|
+
}
|
372
|
+
|
373
|
+
}
|
374
|
+
|
375
|
+
while(a[previous] < searchValue) {
|
376
|
+
|
377
|
+
previous += 1;
|
378
|
+
|
379
|
+
if(previous == (jump < length ? jump : length)) {
|
380
|
+
|
381
|
+
return -1;
|
382
|
+
|
383
|
+
}
|
384
|
+
|
385
|
+
}
|
386
|
+
|
387
|
+
return a[previous] == searchValue ? previous : -1;
|
388
|
+
|
389
|
+
}
|
390
|
+
|
391
|
+
|
392
|
+
|
393
|
+
/**
|
394
|
+
|
395
|
+
* Simply prints the current values in the array.
|
396
|
+
|
397
|
+
*/
|
398
|
+
|
399
|
+
void printArray(int a[], int length) {
|
400
|
+
|
401
|
+
cout << "Current values in the array: " << endl;
|
402
|
+
|
403
|
+
for(int i = 0; i < length; i++) {
|
404
|
+
|
405
|
+
cout << a[i] << " ";
|
406
|
+
|
407
|
+
}
|
408
|
+
|
409
|
+
cout << endl;
|
410
|
+
|
411
|
+
}
|
412
|
+
|
413
|
+
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
|
418
|
+
|
419
|
+
```ここに言語を入力
|
420
|
+
|
421
|
+
コード
|
422
|
+
|
423
|
+
```
|
424
|
+
|
1
|
-
### 前提・実現したいこと
|
425
|
+
```### 前提・実現したいこと
|
2
426
|
|
3
427
|
今学校の課題でC++を習ってます、Linear Search、(Iterative) Binary Search、(Recursive) Binary Search、
|
4
428
|
|
@@ -60,423 +484,11 @@
|
|
60
484
|
|
61
485
|
### 該当のソースコード
|
62
486
|
|
63
|
-
/**
|
64
|
-
|
65
|
-
* Searching Project
|
66
|
-
|
67
|
-
* See the Assignment 12 document (CSCI112_A12.pdf) for details.
|
68
|
-
|
69
|
-
*
|
70
|
-
|
71
|
-
|
487
|
+
```ここに言語を入力
|
72
|
-
|
73
|
-
|
488
|
+
|
74
|
-
|
75
|
-
#include <iostream>
|
76
|
-
|
77
|
-
#include <ctime>
|
78
|
-
|
79
|
-
#include <cmath>
|
80
|
-
|
81
|
-
#include <chrono>
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
using namespace std;
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
//Prototypes
|
90
|
-
|
91
|
-
void duplicate(int[], int[], int);
|
92
|
-
|
93
|
-
void printArray(int[], int);
|
94
|
-
|
95
|
-
int linearSearch(int[], int, int);
|
96
|
-
|
97
|
-
int iterativeBinarySearch(int[], int, int);
|
98
|
-
|
99
|
-
int recursiveBinarySearch(int[], int, int,int);
|
100
|
-
|
101
|
-
int jumpSearch(int[], int, int);
|
102
|
-
|
103
|
-
void insertionSort(int[], int);
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
/**
|
110
|
-
|
111
|
-
* Main Function.
|
112
|
-
|
113
|
-
*/
|
114
|
-
|
115
|
-
int main() {
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
//int LENGTH = 200000;
|
120
|
-
|
121
|
-
//int LENGTH = 150000;
|
122
|
-
|
123
|
-
//int LENGTH = 100000;
|
124
|
-
|
125
|
-
//int LENGTH = 75000;
|
126
|
-
|
127
|
-
int LENGTH = 50000;
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
int numbers[LENGTH]; //a single array
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
srand((int)time(0)); //random numbers in the range of 1 through LENGTH*2
|
136
|
-
|
137
|
-
for(int i = 0; i < LENGTH; i++) {
|
138
|
-
|
139
|
-
numbers[i] = rand() % (LENGTH*2) + 1;
|
140
|
-
|
141
|
-
}
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
int value = numbers[rand() % LENGTH + 1]; //randomly choose a number to search
|
146
|
-
|
147
|
-
cout << "value: " << value;
|
148
|
-
|
149
|
-
cout << endl;
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
auto start = chrono::system_clock::now(); //Gets the start time
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
linearSearch(numbers,LENGTH, value);
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
/**
|
162
|
-
|
163
|
-
* Linear Search Algorithm Calculates the total duration
|
164
|
-
|
165
|
-
*/
|
166
|
-
|
167
|
-
chrono::duration<double> totalTime = chrono::system_clock::now() - start; //Calculates the total duration (now - start)
|
168
|
-
|
169
|
-
cout << "Clock stopped." << endl;
|
170
|
-
|
171
|
-
cout << "Finished in linearSearchNumber " << totalTime.count() << " seconds" << endl;
|
172
|
-
|
173
|
-
cout << endl;
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
cout << "Sorting..." << endl;
|
180
|
-
|
181
|
-
insertionSort(numbers, LENGTH);
|
182
|
-
|
183
|
-
cout << "Done." << endl;
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
LENGTH = sizeof(numbers)/4; //Calculates the length of the array
|
188
|
-
|
189
|
-
start = chrono::system_clock::now();
|
190
|
-
|
191
|
-
iterativeBinarySearch(numbers, LENGTH, value);
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
/**
|
196
|
-
|
197
|
-
* Iterative Binary Search Calculates the total duration
|
198
|
-
|
199
|
-
*/
|
200
|
-
|
201
|
-
totalTime = chrono::system_clock::now() - start; //Calculates the total duration (now - start)
|
202
|
-
|
203
|
-
cout << "Clock stopped." << endl;
|
204
|
-
|
205
|
-
cout << "Finished in iterativeBinarySearch " << totalTime.count() << " seconds" << endl;
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
start = chrono::system_clock::now();
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
recursiveBinarySearch(numbers, 0, LENGTH - 1, value);
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
/**
|
218
|
-
|
219
|
-
* Recursive Binary Search Calculates the total duration
|
220
|
-
|
221
|
-
*/
|
222
|
-
|
223
|
-
cout << "Clock stopped." << endl;
|
224
|
-
|
225
|
-
cout << "Finished in recursiveBinarySearch " << totalTime.count() << " seconds" << endl;
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
start = chrono::system_clock::now();
|
230
|
-
|
231
|
-
//Calculates the length of the array
|
232
|
-
|
233
|
-
jumpSearch(numbers, LENGTH, value);
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
/**
|
238
|
-
|
239
|
-
* Jump Search Calculates the total duration
|
240
|
-
|
241
|
-
*/
|
242
|
-
|
243
|
-
cout << "Clock stopped." << endl;
|
244
|
-
|
245
|
-
cout << "Finished in jumpSearch " << totalTime.count() << " seconds" << endl;
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
return 0;
|
250
|
-
|
251
|
-
}
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
void insertionSort(int a[], int length) {
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
for(int i = 1; i < length; i++) {
|
260
|
-
|
261
|
-
int value = a[i];
|
262
|
-
|
263
|
-
int j = i-1;
|
264
|
-
|
265
|
-
while(j >= 0 && a[j] > value) {
|
266
|
-
|
267
|
-
a[j+1] = a[j];
|
268
|
-
|
269
|
-
|
489
|
+
コード
|
270
|
-
|
271
|
-
|
490
|
+
|
272
|
-
|
273
|
-
a[j+1] = value;
|
274
|
-
|
275
|
-
}
|
276
|
-
|
277
|
-
}
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
/**
|
282
|
-
|
283
|
-
* Linear Search Algorithm
|
284
|
-
|
285
|
-
*/
|
286
|
-
|
287
|
-
int linearSearch(int a[], int length, int searchValue) {
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
for(int i = 0; i < length; i++) {
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
if(a[i] == searchValue) {
|
296
|
-
|
297
|
-
return i; //The index we found it at
|
298
|
-
|
299
|
-
}
|
300
|
-
|
301
|
-
}
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
return -1;
|
306
|
-
|
307
|
-
}
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
/**
|
314
|
-
|
315
|
-
* Binary Search Algorithm
|
316
|
-
|
317
|
-
*/
|
318
|
-
|
319
|
-
int iterativeBinarySearch(int a[], int length, int searchValue) {
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
int lowBoundary = 0;
|
326
|
-
|
327
|
-
int highBoundary = length - 1;
|
328
|
-
|
329
|
-
while(highBoundary >= lowBoundary) {
|
330
|
-
|
331
|
-
int middle = (highBoundary + lowBoundary) / 2; //Calculate middle index
|
332
|
-
|
333
|
-
if(searchValue == a[middle]) {
|
334
|
-
|
335
|
-
return middle; //Return the index we found it at
|
336
|
-
|
337
|
-
}
|
338
|
-
|
339
|
-
else if(searchValue > a[middle]) {
|
340
|
-
|
341
|
-
lowBoundary = middle + 1; //Raise the low boundary
|
342
|
-
|
343
|
-
}
|
344
|
-
|
345
|
-
|
491
|
+
```
|
346
|
-
|
347
|
-
highBoundary = middle - 1; //Lower the high boundary
|
348
|
-
|
349
|
-
}
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
}
|
354
|
-
|
355
|
-
return -1;
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
}
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
/**
|
364
|
-
|
365
|
-
* Recursive Binary Search Algorithm
|
366
|
-
|
367
|
-
*/
|
368
|
-
|
369
|
-
int recursiveBinarySearch(int array[], int first, int last, int value) {
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
if(first > last) {
|
374
|
-
|
375
|
-
return -1; //First base Case
|
376
|
-
|
377
|
-
}
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
int middle = (first + last) / 2; //Calculate the middle position.
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
if(array[middle] == value) {
|
386
|
-
|
387
|
-
return middle; //Second base case
|
388
|
-
|
389
|
-
}
|
390
|
-
|
391
|
-
else if (array[middle] < value) {
|
392
|
-
|
393
|
-
return recursiveBinarySearch(array, middle + 1, last, value); //Recursive case (Search upper partition/upper half)
|
394
|
-
|
395
|
-
}
|
396
|
-
|
397
|
-
else {
|
398
|
-
|
399
|
-
return recursiveBinarySearch(array, first, middle - 1, value); //Recursive case (Search lower partition/lower half)
|
400
|
-
|
401
|
-
}
|
402
|
-
|
403
|
-
}
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
/**
|
410
|
-
|
411
|
-
* Jump Search Algorithm
|
412
|
-
|
413
|
-
*/
|
414
|
-
|
415
|
-
int jumpSearch(int a[], int length, int searchValue) {
|
416
|
-
|
417
|
-
int previous = 0;
|
418
|
-
|
419
|
-
int jump = (int) sqrt(length);
|
420
|
-
|
421
|
-
while(a[(jump < length ? jump : length-1)-1] < searchValue) {
|
422
|
-
|
423
|
-
previous = jump;
|
424
|
-
|
425
|
-
jump += jump;
|
426
|
-
|
427
|
-
if(jump >= length) {
|
428
|
-
|
429
|
-
break;
|
430
|
-
|
431
|
-
}
|
432
|
-
|
433
|
-
}
|
434
|
-
|
435
|
-
while(a[previous] < searchValue) {
|
436
|
-
|
437
|
-
previous += 1;
|
438
|
-
|
439
|
-
if(previous == (jump < length ? jump : length)) {
|
440
|
-
|
441
|
-
return -1;
|
442
|
-
|
443
|
-
}
|
444
|
-
|
445
|
-
}
|
446
|
-
|
447
|
-
return a[previous] == searchValue ? previous : -1;
|
448
|
-
|
449
|
-
}
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
/**
|
454
|
-
|
455
|
-
* Simply prints the current values in the array.
|
456
|
-
|
457
|
-
*/
|
458
|
-
|
459
|
-
void printArray(int a[], int length) {
|
460
|
-
|
461
|
-
cout << "Current values in the array: " << endl;
|
462
|
-
|
463
|
-
for(int i = 0; i < length; i++) {
|
464
|
-
|
465
|
-
cout << a[i] << " ";
|
466
|
-
|
467
|
-
}
|
468
|
-
|
469
|
-
cout << endl;
|
470
|
-
|
471
|
-
}
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
492
|
|
481
493
|
### 試したこと
|
482
494
|
|