Skip to content

Commit 3fbd93d

Browse files
realDuYuanChaogithub-actions
andauthored
Added Jump Search (examplehub#57)
* add jump search * Formatted with Google Java Formatter Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 135c6a5 commit 3fbd93d

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.examplehub.maths;
2+
3+
/** Calculate 1! + 2! + 3! + ... n! */
4+
public class SumOfFactorial {
5+
public static int sum(int n) {
6+
return sum(1, n);
7+
}
8+
9+
public static int sum(int i, int n) {
10+
if (i == n) {
11+
return n;
12+
} else {
13+
return i + i * sum(i + 1, n);
14+
}
15+
}
16+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.examplehub.searches;
2+
3+
public class JumpSearch implements Search {
4+
@Override
5+
public int search(int[] numbers, int key) {
6+
int length = numbers.length;
7+
int steps = (int) Math.floor(Math.sqrt(length));
8+
int prev = 0;
9+
while (numbers[Math.min(steps, length) - 1] < key) {
10+
prev = steps;
11+
steps += (int) Math.floor(Math.sqrt(length));
12+
if (prev >= length) {
13+
return -1;
14+
}
15+
}
16+
while (numbers[prev] < key) {
17+
prev++;
18+
if (prev == Math.min(steps, length)) {
19+
return -1;
20+
}
21+
}
22+
if (numbers[prev] == key) {
23+
return prev;
24+
}
25+
return -1;
26+
}
27+
28+
@Override
29+
public <T extends Comparable<T>> int search(T[] array, T key) {
30+
return 0;
31+
}
32+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.examplehub.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class SumOfFactorialTest {
8+
@Test
9+
void testSum() {
10+
assertEquals(1, SumOfFactorial.sum(1));
11+
assertEquals(3, SumOfFactorial.sum(2));
12+
assertEquals(9, SumOfFactorial.sum(3));
13+
assertEquals(33, SumOfFactorial.sum(4));
14+
assertEquals(153, SumOfFactorial.sum(5));
15+
}
16+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.examplehub.searches;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import java.util.stream.IntStream;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
class JumpSearchTest {
10+
private Search search;
11+
12+
@BeforeEach
13+
void setup() {
14+
search = new JumpSearch();
15+
}
16+
17+
@Test
18+
void testLinearSearch() {
19+
int[] ints = IntStream.range(0, 10).toArray();
20+
for (int i = 0; i < ints.length; ++i) {
21+
assertEquals(i, search.search(ints, i));
22+
}
23+
assertEquals(-1, search.search(ints, 10));
24+
assertEquals(-1, search.search(ints, 100));
25+
assertEquals(-1, search.search(ints, -1));
26+
}
27+
}

0 commit comments

Comments
 (0)