ralphdc09

its101.md.wk.08.09.InterpolationSearch

Nov 15th, 2021 (edited)
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.70 KB | None | 0 0
  1. package its101.mdwk08_09;
  2.  
  3. import java.util.Arrays;
  4.  
  5. public class InterpolationSearch {
  6.  
  7.     //    Create function interpolationSearch()
  8.     public static int interpolationSearch(int[] array, int lo, int hi, int key) {
  9.  
  10.         if (lo <= hi && key >= array[lo] && key <= array[hi]) {
  11.  
  12.             // Probing position while keeping the distribution uniform
  13.             int mid = lo + (((hi - lo) / (array[hi] - array[lo])) * (key - array[lo]));
  14.             // If key or target is found return the index position
  15.             if (array[mid] == key) {
  16.                 return mid;
  17.             }
  18.             // If key is larger than mid point, key is in the right sub array
  19.             if (array[mid] < key) {
  20.                 return interpolationSearch(array, mid + 1, hi,
  21.                         key);
  22.             }
  23.             // If key is smaller than mid point, key is in the left sub array
  24.             if (array[mid] > key) {
  25.                 return interpolationSearch(array, lo, mid - 1,
  26.                         key);
  27.             }
  28.         }
  29.         //  Return -1 if key is not found in the array
  30.         return -1;
  31.     } //    End of function interpolationSearch()
  32.  
  33.     public static void main(String[] args) {
  34.  
  35. //        A
  36.         // Create array
  37.         int[] arrayA = {2, 3, 5, 7, 9};
  38.         //  Set hi (upper bound)
  39.         int hi = arrayA.length - 1;
  40.  
  41.         // Print array arrayA
  42.         System.out.println(Arrays.toString(arrayA));
  43.  
  44.         // Initiate element key1 to be search
  45.         int key1 = 7;
  46.         // Initiate the search result1 using the interpolationSearch() function
  47.         int result1 = interpolationSearch(arrayA, 0, hi, key1);
  48.  
  49.         // If-else statement for printing the result1
  50.         if (result1 != -1) {
  51.             System.out.println("Element " + key1 + " is present at index " + result1);
  52.         } else {
  53.             System.out.println("Element " + key1 + " is not present in the array");
  54.         }
  55.  
  56.         System.out.println();
  57.  
  58. //        B
  59.         // Create new array
  60.         int[] arrayB = {1, 4, 5, 8, 9};
  61.         //  Set hi (upper bound)
  62.         int hi2 = arrayB.length - 1;
  63.  
  64.         // Print array
  65.         System.out.println(Arrays.toString(arrayB));
  66.  
  67.         // Initiate element key2 to be search
  68.         int key2 = 2;
  69.         // Initiate the search result2 using the interpolationSearch() function
  70.         int result2 = interpolationSearch(arrayB, 0, hi2, key2);
  71.  
  72.         // If-else statement for printing the result1
  73.         if (result2 != -1) {
  74.             System.out.println("Element " + key2 + " is present at index " + result2);
  75.         } else {
  76.             System.out.println("Element " + key2 + " not found");
  77.         }
  78.     }
  79. }
  80.  
Add Comment
Please, Sign In to add comment