Advertisement
binibiningtinamoran

Searches.java

Jun 1st, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.59 KB | None | 0 0
  1. public class Searches {
  2.  
  3.     // finds the first location of a target
  4.     public static int linearSearchIterative(int[] numbers, int target) {
  5.         int targetLocation = -1;
  6.         boolean found = false; // include if we want to stop when we find the target
  7.         // without this variable, we will return the LAST location of the target
  8.  
  9.         int comparisonCount = 0;
  10.  
  11.         for (int i = 0; (i < numbers.length && !found); i++) {
  12.             comparisonCount++;
  13.             if (numbers[i] == target) {
  14.                 found = true;
  15.                 targetLocation = i;
  16.             }
  17.         }
  18.         System.out.println("In linear search iterative, comparison count is \t" + comparisonCount);
  19.         return targetLocation;
  20.  
  21.     }
  22.  
  23.     public static int linearSearchRecursive(int[] numbers, int target) {
  24.         comparisonCountLinearRecursive = 0;
  25.         int answer = linearSearchRecursiveHelper(numbers, target, 0, numbers.length - 1);
  26.         System.out.println("In linear search recursive, comparison count is \t" + comparisonCountLinearRecursive);
  27.         return answer;
  28.     }
  29.  
  30.     private static int comparisonCountLinearRecursive = 0;
  31.    
  32.     private static int linearSearchRecursiveHelper(int[] numbers, int target, int first, int last) {
  33.         comparisonCountLinearRecursive++;
  34.         if (first > last) { // indices cross over- it's not here
  35.             return -1;
  36.         } else if (target == numbers[first]) { // we found it!
  37.             return first;
  38.         } else { // keep looking
  39.             return linearSearchRecursiveHelper(numbers, target, first + 1, last);
  40.            
  41.         }
  42.     }
  43.  
  44.     public static int linearSearchIterativeImprovedForSorted(int[] numbers, int target) {
  45.         int targetLocation = -1;
  46.         boolean found = false;
  47.         boolean gonePastTarget = false;
  48.         int comparisonCount = 0;
  49.  
  50.         for (int i = 0; (i < numbers.length && !found && !gonePastTarget); i++) {
  51.             comparisonCount++;
  52.             if (numbers[i] == target) {
  53.                 found = true;
  54.                 targetLocation = i;
  55.             } else if (numbers[i] > target) {
  56.                 gonePastTarget = true;
  57.             }
  58.         }
  59.         System.out.println("In linear search optimized for sorting, count is \t" + comparisonCount);
  60.         return targetLocation;
  61.     }
  62.  
  63.     public static int binarySearchIterative(int[] numbers, int target) {
  64.         int targetLocation = -1;
  65.         boolean found = false;
  66.         int first = 0;
  67.         int last = numbers.length - 1;
  68.         int comparisonCount = 0;
  69.  
  70.         while (first <= last && !found) {
  71.             comparisonCount++;
  72.             int mid = (first + last) / 2;
  73.  
  74.             if (numbers[mid] == target) {
  75.                 targetLocation = mid;
  76.                 found = true;
  77.             } else if (numbers[mid] < target) {
  78.                 first = mid + 1;
  79.             } else { // numbers[mid] > target
  80.                 last = mid - 1;
  81.             }
  82.         }
  83.         System.out.println("In binary search iterative, comparison count is \t" + comparisonCount);
  84.         return targetLocation;
  85.     }
  86.  
  87.     public static int binarySearchRecursive(int[] numbers, int target) {
  88.         comparisonCountBinaryRecursive = 0;
  89.         int answer = binarySearchRecursiveHelper(numbers, target, 0, numbers.length - 1);
  90.         System.out.println("In binary search recursive, comparison count is \t" + comparisonCountBinaryRecursive);
  91.         return answer;
  92.     }
  93.  
  94.     private static int comparisonCountBinaryRecursive = 0;
  95.  
  96.     private static int binarySearchRecursiveHelper(int[] numbers, int target, int first, int last) {
  97.         int mid = ((last - first) / 2) + first;
  98.         comparisonCountBinaryRecursive++;
  99.         if (first > last) {
  100.             return -1; // indices cross over
  101.         } else if (target == numbers[mid]) {
  102.             return mid; // we found it!
  103.         } else if (target < numbers[mid]) {
  104.             return binarySearchRecursiveHelper(numbers, target, first, mid - 1);
  105.             // keep looking in left half
  106.         } else { // target > numbers[mid]
  107.             return binarySearchRecursiveHelper(numbers, target, mid + 1, last);
  108.             // keep looking in right half
  109.         }
  110.     }
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement