From 1cb21915fe90231605713db706e5dc061f90f665 Mon Sep 17 00:00:00 2001 From: thegabriele97 Date: Fri, 6 Apr 2018 00:54:36 +0200 Subject: [PATCH 1/2] added iterative version of binary search algorithm --- Searches/IterativeBinarySearch.java | 59 +++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Searches/IterativeBinarySearch.java diff --git a/Searches/IterativeBinarySearch.java b/Searches/IterativeBinarySearch.java new file mode 100644 index 000000000..8e2c0304c --- /dev/null +++ b/Searches/IterativeBinarySearch.java @@ -0,0 +1,59 @@ + +import java.util.Arrays; +import java.util.Random; + +/** + * + * @author Gabriele La Greca : https://github.com/thegabriele97 + * + */ + +public final class IterativeBinarySearch { + + /** + * This method implements an iterative version of binary search algorithm + * + * @param a sorted array + * @param the key to search in array + * + * @return the index of key in the array or -1 if not found + */ + public static > int BS(T[] array, T key) { + int l, r, k, cmp; + + l = 0; + r = array.length - 1; + + while (l <= r) { + k = (l + r) / 2; + cmp = key.compareTo(array[k]); + + if (cmp == 0) { + return k; + } else if (cmp < 0) { + r = --k; + } else if (cmp > 0) { + l = ++k; + } + } + + return -1; + } + + //Only a main method for test purpose + public static void main(String[] args) { + Random rand = new Random(); + int base = rand.nextInt(1000); + + Integer[] array = new Integer[0xFFFF]; + for (int i = 0; i < array.length; i++) { + array[i] = base + (i + 1); + } + + Arrays.sort(array); //if needed + Integer key = base + rand.nextInt(array.length); + + System.out.println(BS(array, key)); + System.out.println(Arrays.binarySearch(array, key)); + } +} \ No newline at end of file From c7f4f53a4b53bbacc560e71af7fc61dfe4c3c2b0 Mon Sep 17 00:00:00 2001 From: Salvatore Gabriele La Greca <35406071+thegabriele97@users.noreply.github.com> Date: Sun, 8 Apr 2018 20:05:00 +0200 Subject: [PATCH 2/2] code clean --- Searches/IterativeBinarySearch.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Searches/IterativeBinarySearch.java b/Searches/IterativeBinarySearch.java index 8e2c0304c..60504ec0f 100644 --- a/Searches/IterativeBinarySearch.java +++ b/Searches/IterativeBinarySearch.java @@ -13,12 +13,12 @@ public final class IterativeBinarySearch { /** * This method implements an iterative version of binary search algorithm * - * @param a sorted array - * @param the key to search in array + * @param array a sorted array + * @param key the key to search in array * * @return the index of key in the array or -1 if not found */ - public static > int BS(T[] array, T key) { + public static > int binarySearch(T[] array, T key) { int l, r, k, cmp; l = 0; @@ -45,15 +45,15 @@ public final class IterativeBinarySearch { Random rand = new Random(); int base = rand.nextInt(1000); - Integer[] array = new Integer[0xFFFF]; + Integer[] array = new Integer[65535]; for (int i = 0; i < array.length; i++) { array[i] = base + (i + 1); } - Arrays.sort(array); //if needed - Integer key = base + rand.nextInt(array.length); + //Arrays.sort(array); //if needed + Integer key = base + rand.nextInt(array.length * 2); //can generate keys that aren't in array - System.out.println(BS(array, key)); + System.out.println(binarySearch(array, key)); System.out.println(Arrays.binarySearch(array, key)); } -} \ No newline at end of file +}