Code for Index Sequential Search
import java.util.*;
public class Index_search{
public static void main(String[] args) {
int i,a=0;
Scanner in =new Scanner(System.in);
int arr[] = {1,2,3,4,5,6,7,8,9,10};
System.out.println("Please enter the number to be find: ");
int num= in.nextInt();
System.out.println("Please enter the steps to left");
int steps = in.nextInt();
in.close();
for ( i = 0; i < arr.length; ) {
if (num == arr[i] ) {
System.out.println("Number found in the first one. "+i);
a=1;
break;
}
else if(num<arr[i]){
break;
}
i=i+steps;
}
if (a==0) {
for (int j = (i-steps)+1; j < i; j++) {
if (num==arr[j]) {
System.out.println("Found at index: "+j);
break;
}
}
}
}
}
The output:
Comments
Post a Comment
THANK YOU FOR COMMENTING !
PLEASE SUBSCRIBE TO OUR WEBSITE FOR LATEST UPDATES