Advertisement
supremeXD

Untitled

Feb 21st, 2022
872
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.32 KB | None | 0 0
  1. package search;
  2.  
  3. public class BinarySearch {
  4.  
  5.     // Contract
  6.     // Pred: exists such index "i" that values[i] <= x
  7.     //       and array "values" is sorted by non-increase
  8.     // Post: R = min index "i" that values[i] <= x
  9.     public static int iterativeBinarySearch(int x, int[] values) {
  10.         // R is in (-1, values.length - 1]
  11.         int l = -1, r = values.length - 1;
  12.         // R is in (l, r]
  13.         while (r - l > 1) {
  14.             // R is in (l, r] && r - l > 1
  15.             int m = (l + r) / 2;
  16.             // (R is in (l, m] || R is in (m, r]) && r - l > 1 && l < m < r
  17.             if (values[m] <= x) {
  18.                 // values[m] <= x && R is in (l, m] && r - l > 1 && l < m < r
  19.                 r = m;
  20.                 // R is in (l, r]
  21.             } else {
  22.                 // values[m] > x && R is in (m, r] && r - l > 1 && l < m < r
  23.                 l = m;
  24.                 // R is in (l, r]
  25.             }
  26.             // R is in (l, r]
  27.         }
  28.         // R is in (l, r] && r - l == 1 && R = r
  29.         return r;
  30.     }
  31.  
  32.     // Contract
  33.     // Pred: R is in (l, r] and r - l >= 1 and array "values" is sorted by non-increase
  34.     // Post: R = min index "i" that values[i] <= x
  35.     public static int recursiveBinarySearch(int x, int l, int r, int[] values) {
  36.         // R is in (l, r] && r - l >= 1
  37.         if (r - l == 1) {
  38.             // R is in (l, r] && r - l == 1 && R = r;
  39.             return r;
  40.         }
  41.         // R is in (l, r] && r - l > 1
  42.         int m = (l + r) / 2;
  43.         // (R is in (l, m] || R is in (m, r]) && r - l > 1 && l < m < r
  44.         if (values[m] <= x) {
  45.             // values[m] <= x && R is in (l, m] && r - l > 1 && l < m < r
  46.             return recursiveBinarySearch(x, l, m, values);
  47.         } else {
  48.             // values[m] > x && R is in (m, r] && r - l > 1 && l < m < r
  49.             return recursiveBinarySearch(x, m, r, values);
  50.         }
  51.     }
  52.  
  53.     // Pred: array "values" is sorted by non-increase
  54.     // Post: valuest.length == 1 -> 0
  55.     //       valuest[valuest.length - 1] > x -> values.length
  56.     //       else R = min index "i" that values[i] <= x
  57.     public static void main(String[] args) {
  58.         if (args.length <= 1) {
  59.             System.out.println(0);
  60.         } else {
  61.             int x = Integer.parseInt(args[0]);
  62.             int[] values = new int[args.length - 1];
  63.             for (int i = 0; i < values.length; i++) {
  64.                 values[i] = Integer.parseInt(args[i + 1]);
  65.             }
  66.             if (values[values.length - 1] > x) {
  67.                 System.out.println(values.length);
  68.             } else {
  69.                 System.out.println(iterativeBinarySearch(x, values));
  70.                 //System.out.println(recursiveBinarySearch(x, -1, values.length - 1, values));
  71.             }
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement