Skip to content

Instantly share code, notes, and snippets.

@dwin
Last active August 18, 2017 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dwin/70f3b13c671f40eefce6886bf7920922 to your computer and use it in GitHub Desktop.
Save dwin/70f3b13c671f40eefce6886bf7920922 to your computer and use it in GitHub Desktop.
Sequential vs Jump Search Algoritm for finding key digits in sorted numerical array
/*
* Dwin
*
*/
// Search ordered numerical array for specified numerical keys
public class Search {
static int[] arr = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 13, 14, 15, 16, 19, 21, 25, 28, 31, 34, 36, 37, 38, 43, 49, 52, 53, 55, 60, 66, 69, 72, 81, 89, 110, 115, 116, 123, 144, 167, 182, 199, 233, 302, 320, 377, 462, 510, 540, 610, 630, 641, 666, 671, 695, 713, 715, 732, 754, 769, 856, 875, 900, 905, 915};
static int[] key = {0, 16, 116, 302, 610, 666, 900};
public static void main(String[] args) {
long jumpTime = 0;
long seqTime = 0;
for (int l=1; l<7; l++) {
// Do jump search for each key with timing, inaccurate benchmark method
long startJump = 0;
for (int i=0; i<key.length; i++) {
startJump = System.nanoTime();
int result = JumpSearch(arr,key[i]);
jumpTime+= (System.nanoTime()-startJump);
System.out.print("Key: " + key[i] + " in arr at [" + result+"]. ");
}
System.out.println(l+"-Jump search done.");
// Do sequential search for each key with timing, inaccurate benchmark method
long startSeq = 0;
for (int i=0; i<key.length; i++) {
startSeq = System.nanoTime();
int result = Sequential(arr,key[i]);
System.out.print("Key: " + key[i] + " in arr at [" + result+"]. ");
seqTime+= (System.nanoTime()-startSeq);
}
System.out.println(l+"-Seq. search done.");
}
System.out.println("\nAvg of Jump Search times: (6 runs) "+(jumpTime/6)+" ns");
System.out.println("Avg of Sequential Search times: (6 runs) "+(seqTime/6)+" ns");
}
// Search array by jumping to certain points (determined by sq. root of arr.length) then searching from that point backward
public static int JumpSearch(int[] arr, int key) {
int step = (int) Math.floor(Math.sqrt(arr.length));
for (int i=1; i<arr.length ;i++) {
// If number at search position is greater than or equal to key start search backward from that point
// Search position is i * step size
if (arr[i*step] >= key) {
for (int ii=i*step; i*step<arr.length; ii--) {
if (arr[ii] == key) return ii;
}
}
// If incremented search position would be out of bounds search from current position to end
if ((i+1)*step >= arr.length) {
for (int p=i*step; p<arr.length; p++) {
if (arr[p] == key) return p;
}
}
}
// If key value not found return -1
return -1;
}
// Search array sequentially and return position of key found. If not found return -1.
public static int Sequential(int[] arr, int key) {
// Check positions from 0 through array length and return key value position
for (int i=0; i < arr.length; i++) {
if (arr[i] == key) return i;
}
// If key value not found return -1
return -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment