GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Java/Searches/BinarySearch.java at master · feiteng/Java Expand file tree/ Copy pathBinarySearch.javaMore file actions 95 lines (79 loc) · 2.75 KB / Copy pathBinarySearch.javaFile metadata and controls95 lines (79 loc) · 2.75 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 package Searches; import java.util.Arrays; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; import java.util.stream.IntStream; import static java.lang.String.format; /** * * * * Binary search is one of the most popular algorithms * The algorithm finds the position of a target value within a sorted array * * Worst-case performance O(log n) * Best-case performance O(1) * Average performance O(log n) * Worst-case space complexity O(1) * * * @author Varun Upadhyay (https://github.com/varunu28) * @author Podshivalov Nikita (https://github.com/nikitap492) * * @see SearchAlgorithm * @see IterativeBinarySearch * */ class BinarySearch implements SearchAlgorithm { /** * * @param array is an array where the element should be found * @param key is an element which should be found * @param <T> is any comparable type * @return index of the element */ @Override public <T extends Comparable<T>> int find(T[] array, T key) { return search(array, key, 0, array.length); } /** * This method implements the Generic Binary Search * * @param array The array to make the binary search * @param key The number you are looking for * @param left The lower bound * @param right The upper bound * @return the location of the key **/ private <T extends Comparable<T>> int search(T array[], T key, int left, int right){ if (right < left) return -1; // this means that the key not found // find median int median = (left + right) >>> 1; int comp = key.compareTo(array[median]); if (comp == 0) { return median; } else if (comp < 0) { return search(array, key, left, median - 1); } else { return search(array, key, median + 1, right); } } // Driver Program public static void main(String[] args) { // Just generate data Random r = ThreadLocalRandom.current(); int size = 100; int maxElement = 100000; Integer[] integers = IntStream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().boxed().toArray(Integer[]::new); // The element that should be found int shouldBeFound = integers[r.nextInt(size - 1)]; BinarySearch search = new BinarySearch(); int atIndex = search.find(integers, shouldBeFound); System.out.println(format( "Should be found: %d. Found %d at index %d. An array length %d", shouldBeFound, integers[atIndex], atIndex, size )); int toCheck = Arrays.binarySearch(integers, shouldBeFound); System.out.println(format("Found by system method at an index: %d. Is equal: %b", toCheck, toCheck == atIndex)); } } |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |