Created
May 2, 2018 16:13
-
-
Save SharanSMenon/7c4f5f35cc9915ed12f3ee4d2c2476a1 to your computer and use it in GitHub Desktop.
Implements the binary search algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sharan; | |
import java.math.BigInteger; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
/** | |
* There is nothing for lists yet except for an empty | |
* constructor | |
*/ | |
public class BinarySearch { | |
private BinarySearch() { | |
} | |
public static void main(String[] args) { | |
ArrayList<BigInteger> list = new ArrayList<>(); | |
for (long i = 2; i < 53; i++) { | |
list.add(BigInteger.valueOf(i * 2)); | |
} | |
// Outputting the list | |
System.out.println("List: " + list); | |
// Using binary search implemented in this class. | |
System.out.println("Using the binary search method the index of 42 will be found in this list"); | |
double start = System.nanoTime(); | |
long bs1 = binarySearch(list, BigInteger.valueOf((long) 42)); | |
double end = System.nanoTime(); | |
double duration = end - start; | |
System.out.println("Index of 42: " + bs1); | |
System.out.println("Using the found index: " + list.get((int) bs1)); | |
System.out.println("Duration: " + duration + " nanoseconds."); | |
// Using binary search implemented in the Collections class | |
start = System.nanoTime(); | |
long bs2 = Collections.binarySearch(list, BigInteger.valueOf((long) 42)); | |
end = System.nanoTime(); | |
duration = end - start; | |
System.out.println("Collections.binarySearch for index of 42: " + bs2); | |
System.out.println("Using Collections.binarySearch index: " + list.get((int) bs2)); | |
System.out.println("Duration: " + duration + " nanoseconds."); | |
System.out.println("Both methods of binary search work fine"); | |
} | |
public static long binarySearch(List<BigInteger> list, BigInteger key) { | |
BigInteger high = BigInteger.valueOf((long) list.size()); | |
BigInteger low = BigInteger.ZERO; | |
if (high.compareTo(low) == -1) { | |
return -1; | |
} | |
BigInteger mid = low.add((high.subtract(low)).divide(BigInteger.TWO)); | |
try { | |
BigInteger ignore = list.get(mid.intValue()); | |
} catch (IndexOutOfBoundsException e) { | |
return -1; | |
} | |
if (key == list.get(mid.intValue())) { | |
return mid.longValue(); | |
} else if (key.compareTo(list.get(mid.intValue())) == -1) { | |
return binarySearch(list, key, low, mid.subtract(BigInteger.ONE)); | |
} else { | |
return binarySearch(list, key, mid.add(BigInteger.ONE), high); | |
} | |
} | |
public static long binarySearch(List<BigInteger> list, BigInteger key, BigInteger low, BigInteger high) { | |
if (high.compareTo(low) == -1) { | |
return low.subtract(BigInteger.ONE).longValue(); | |
} | |
BigInteger mid = low.add((high.subtract(low)).divide(BigInteger.TWO)); | |
try { | |
BigInteger ignore = list.get(mid.intValue()); | |
} catch (IndexOutOfBoundsException e) { | |
return -1; | |
} | |
if (key == list.get(mid.intValue())) { | |
return mid.longValue(); | |
} else if (key.compareTo(list.get(mid.intValue())) == -1) { | |
return binarySearch(list, key, low, mid.subtract(BigInteger.ONE)); | |
} else { | |
return binarySearch(list, key, mid.add(BigInteger.ONE), high); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment