Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. package interpolation;
  2.  
  3. // Driver method
  4. public static void main(String[] args)
  5. {
  6. Scanner s = new Scanner(System.in);
  7. int arr[] = new int[] {1,5,6,9,15};
  8. int search;
  9. int index=0;
  10. int count= 0;
  11. System.out.println("Enter Number To Search: ");
  12. search = s.nextInt();
  13.  
  14.  
  15. int low =0;
  16. int position;
  17. int high = (arr.length-1);
  18.  
  19. while(low <= high && search >=arr[low] && search <= arr[high])
  20. {
  21. position = low + (((high-low) / (arr[high]-arr[low]))*(search-arr[low]));
  22. // count++;
  23. if(arr[position]==search)
  24. {
  25. index = position;
  26. }
  27. if(arr[position] < search)
  28. {
  29. low = position+1;
  30. }
  31. else
  32. {
  33. high = position-1;
  34. }
  35. count++;
  36. }
  37.  
  38. if(index != -1)
  39. {
  40. System.out.println("Number is Present at Index: "+index);
  41. }
  42. else
  43. {
  44. System.out.println("Number is not present");
  45. }
  46. System.out.println(" "+count);
  47.  
  48.  
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement