Welcome Message

Hi, welcome to my website. This is a place where you can get all the questions, puzzles, algorithms asked in interviews and their solutions. Feel free to contact me if you have any queries / suggestions and please leave your valuable comments.. Thanks for visiting -Pragya.

January 28, 2018

Jump Search

package com.mylearnings;

public class JumpSearch {

public static void main(String[] args) {
// TODO Auto-generated method stub

int sortedInput[] = {5,6,12,15,18,25,45,65,70,75,78,80,82,83,85,88,90,98,100,105,107};
JumpSearch se =  new JumpSearch();
System.out.println(se.jumpSearch(sortedInput, 0, 4, 4, 85));
}

public int jumpSearch(int[] input , int start , int end, int jump , int search) {
//int mid = (end - start) /2;
if(end <= input.length) {
if((input[start] >= search) || (search < input[end])) {
for(int i =start ; i <= end ; i++) {
if(search == input[i]) {
return i;
}
}
}else {
return jumpSearch(input , end , end + jump, jump,  search);
}
}
return -1;
}

}

The most optimum value of jump would be sqrt(size of array)

References : 
http://theoryofprogramming.com/2016/11/10/jump-search-algorithm/

No comments: