Skip to content

Commit 51f4f47

Browse files
committed
add jump search algorithm
1 parent a69d652 commit 51f4f47

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

src/search/jumpSearch.cpp

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#include <iostream>
2+
#include <cmath>
3+
4+
struct searchStats {
5+
6+
int jumps = 0; // counting jumps
7+
int jumpSize = 0; // jump gap(size) sqrt(n)
8+
int linearSteps = 0; // counting linear back steps
9+
10+
};
11+
12+
13+
int jumpSearch(int arr[], int size, int item, searchStats &stats) {
14+
15+
// calculate jump size
16+
stats.jumpSize = sqrt(size);
17+
int currIndex = 0;
18+
19+
20+
// loop with size of array
21+
while(currIndex <= size){
22+
23+
// check if current element is equals to item
24+
if(arr[currIndex] == item) return currIndex;
25+
26+
// check if current elemnt is greater than item
27+
// then we will subtract currIndex by 1 each loop
28+
// to back to previous index, and inceremnt stats.linearSteps by 1
29+
else if(arr[currIndex] > item) {
30+
currIndex--;
31+
stats.linearSteps++;
32+
}
33+
34+
35+
// otherwise we will increment currIndex by stats.jumpSize,
36+
// jumpSize value is sqrt(size) ex. sqrt(10) =
37+
else{
38+
39+
currIndex += stats.jumpSize;
40+
stats.jumps++;
41+
42+
}
43+
44+
45+
}
46+
47+
// item not found !
48+
std::cout << "The element you looking for is not there !\n\n";
49+
return -1;
50+
51+
}
52+
53+
54+
int main(int argc, char *argv[]) {
55+
56+
57+
/*
58+
59+
* Jump search is a searching algorithm (best than linear search).
60+
* basically, the algorithm jump a specific range called jump size or gap.
61+
* The algorithm only works on sorted arrays !
62+
* How jump search works:
63+
*
64+
* 1. set jump size to sqrt(arrayLength).
65+
* 2. loop by the size of array
66+
* 3. check if array[currentIndex] eqauls searched item.
67+
* then we will return the index.
68+
*
69+
* 4. check if array[currentArray] greater than item
70+
* then we will subtract currentIndex by 1
71+
* to continue linear search for previous index
72+
*
73+
* 5. otherwise we will incerement current index by jump size for example 3.
74+
*
75+
* - when the function don't find the element it will return -1
76+
* - if the currentIndex is greater than item, we will subtract it by 1 (previous index)
77+
78+
*/
79+
80+
// initialize stats object
81+
searchStats stats;
82+
83+
int arr[] = {-15, 0, 3, 39, 40, 48, 49, 71, 83, 97};
84+
int arrlen = sizeof(arr) / sizeof(arr[0]);
85+
int elementIndex = jumpSearch(arr, arrlen, 71, stats);
86+
int element = arr[elementIndex];
87+
88+
// when the linearSearch() return -1
89+
// searchElemnet value will be randomize so we set it to -1 (element not found)
90+
(element > arr[arrlen] && elementIndex < 0) ? element = -1 : element;
91+
92+
93+
// output stats
94+
std::cout << "Index: " << elementIndex << std::endl;
95+
std::cout << "Element: " << element << std::endl;
96+
std::cout << "Jumps: " << stats.jumps << std::endl;
97+
std::cout << "Jump Size: " << stats.jumpSize << std::endl;
98+
std::cout << "Linear steps: " << stats.linearSteps << std::endl;
99+
100+
return 0;
101+
}

0 commit comments

Comments
 (0)