Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit233f05d

Browse files
authored
Merge pull requestTheAlgorithms#652 from LesliePinto89/Development
Added Jump Search algorithm and JUnit test
2 parentsc2e2402 +6dcbaca commit233f05d

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
packagesrc.main.java.com.search;
2+
3+
publicclassJumpSearch {
4+
5+
/**
6+
* A jump search algorithm that finds the position of a key by moving over
7+
* fixed block ranges in a sorted array, and linear searches back on itself to
8+
* find it.
9+
*
10+
* Worst case time complexity: O(N^(1/2)) - square root n
11+
* Average case time complexity: O(N^(1/2)) - square root n
12+
* Worst case Space complexity: O(1)
13+
*
14+
* @param <T> This is any comparable type
15+
* @param arr This is the array where the element should be found
16+
* @param key This is the element to find in the array
17+
* @return The index of the key in the array
18+
*/
19+
public <TextendsComparable<T>>intfindIndex(Tarr[],Tkey) {
20+
returncheckCondition(arr,key,arr.length);
21+
}
22+
23+
/**
24+
* @param arrLength The array's length
25+
* @return The index position of the key in the array
26+
*/
27+
public <TextendsComparable<T>>intcheckCondition(Tarr[],Tkey,intarrLength) {
28+
intstep = (int)Math.floor(Math.sqrt(arrLength));// Find jump block
29+
intprevious =0;// Find block where element is / or not present
30+
31+
// Use ternary operator to find if step or array length is min value
32+
// and then minus the min value by one
33+
intminVal = (step <arrLength) ?step -1 :arrLength -1;
34+
35+
StringarrayMinValIndexString =arr[minVal].toString();
36+
intarrayMinValIndexInt =Integer.parseInt(arrayMinValIndexString);
37+
StringkeyValueString =key.toString();
38+
intkeyValueInt =Integer.parseInt(keyValueString);
39+
40+
// Increment next step and previous step in block to find range block
41+
while (arrayMinValIndexInt <keyValueInt) {
42+
previous =step;
43+
step += (int)Math.floor(Math.sqrt(arrLength));
44+
if (previous >=arrLength)
45+
return -1;
46+
minVal = (step <arrLength) ?step -1 :arrLength -1;
47+
arrayMinValIndexString =arr[minVal].toString();
48+
arrayMinValIndexInt =Integer.parseInt(arrayMinValIndexString);
49+
}
50+
// Get key position in linear search
51+
intposition =linearSearchBlock(arr,key,step,previous,keyValueInt,arrLength,minVal);
52+
returnposition;
53+
}
54+
55+
/**
56+
* @param step The next block index in the array
57+
* @param previous The previous block index in the array
58+
* @param keyValueInt The key in the format of an integer
59+
* @param minVal The minimum value of either next step or array length
60+
* @return The index position of the key in the array
61+
*/
62+
public <TextendsComparable<T>>intlinearSearchBlock(Tarr[],Tkey,intstep,intprevious,intkeyValueInt,
63+
intarrLength,intminVal) {
64+
65+
// Linear search starting from previous block forwards.
66+
StringarrayPositionString =arr[previous].toString();
67+
intarrayPositionValue =Integer.parseInt(arrayPositionString);
68+
while (arrayPositionValue <keyValueInt) {
69+
// If in next block or end of array length, key not in array
70+
if (previous ==minVal)
71+
return -1;
72+
previous++;
73+
// Update arrayPositionValue in iteration
74+
minVal = (step <arrLength) ?step -1 :arrLength -1;
75+
arrayPositionString =arr[previous].toString();
76+
arrayPositionValue =Integer.parseInt(arrayPositionString);
77+
78+
}
79+
// If the key is found
80+
if (arrayPositionValue ==keyValueInt)
81+
returnprevious;
82+
return -1;
83+
}
84+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
packagesrc.test.java.com.search;
2+
3+
importorg.junit.Test;
4+
importorg.junit.Assert;
5+
6+
importsrc.main.java.com.search.JumpSearch;
7+
8+
publicclassJumpSearchTest {
9+
10+
@Test
11+
publicvoidtestJumpSearch() {
12+
JumpSearchjumpSearch =newJumpSearch();
13+
14+
Integerarr[] = {11,15,16,29,36,40,42,52};
15+
intx =36;
16+
intindex =jumpSearch.findIndex(arr,x);
17+
Assert.assertEquals("Incorrect index",4,index);
18+
19+
IntegerarrTwo[] = {-210, -190, -180, -160, -130, -120, -100};
20+
x = -120;
21+
index =jumpSearch.findIndex(arrTwo,x);
22+
Assert.assertEquals("Incorrect index",5,index);
23+
24+
StringarrString[] = {"101","122","136","165","225","251","291"};
25+
StringstringX ="122";
26+
index =jumpSearch.findIndex(arrString,stringX);
27+
Assert.assertEquals("Incorrect index",1,index);
28+
}
29+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp