From 318cced06fe29db1dd23eee5564459dd6fee80a6 Mon Sep 17 00:00:00 2001 From: homnay1234 <99612060+homnay1234@users.noreply.github.com> Date: Sun, 20 Feb 2022 22:26:05 +0700 Subject: [PATCH 1/4] jUnit Test on Selection Sort I hope i can add a jUnit Test on Selection Sort for your Project --- TestSelectionSort | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 TestSelectionSort diff --git a/TestSelectionSort b/TestSelectionSort new file mode 100644 index 00000000..b98e7b86 --- /dev/null +++ b/TestSelectionSort @@ -0,0 +1,36 @@ +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class TestSort { + + SelectionSort selectionSort = new SelectionSort(); + @Test + public void tesInsertIntoSorted() { + int[] inputArr = new int[] {1,3,5}; + int[] inputResult = new int[] {1,3,5}; + selectionSort.selectionSort(inputArr); + assertArrayEquals(inputResult, inputArr); + } + + SelectionSort selectionSort2 = new SelectionSort(); + @Test + public void tesInsertIntoSorted2() { + int[] inputArr = new int[] {5,3,1}; + int[] inputResult = new int[] {1,3,5}; + selectionSort.selectionSort(inputArr); + assertArrayEquals(inputResult, inputArr); + } + + + SelectionSort selectionSort3 = new SelectionSort(); + @Test + public void tesInsertIntoSorted3() { + int[] inputArr = new int[] {4,5,3,2}; + int[] inputResult = new int[] {2,3,4,5}; + selectionSort.selectionSort(inputArr); + assertArrayEquals(inputResult, inputArr); + } + + +} From 4b1b50aba41b6ce480f6b636f2d52976a47d5b05 Mon Sep 17 00:00:00 2001 From: homnay1234 <99612060+homnay1234@users.noreply.github.com> Date: Sun, 20 Feb 2022 22:33:22 +0700 Subject: [PATCH 2/4] jUnit Test on BinarySearch Algorithm I hope i can add a jUnit Test on BinarySearch Algorithm for you project --- TestBinarySearch | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 TestBinarySearch diff --git a/TestBinarySearch b/TestBinarySearch new file mode 100644 index 00000000..82136b60 --- /dev/null +++ b/TestBinarySearch @@ -0,0 +1,43 @@ +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class TestSearch { + + BinarySearch binarySearch = new BinarySearch() ; + @Test + public void tesInsertIntoSorted() { + int[] inputArr = new int[] {0,1,2,3,4}; + assertTrue( binarySearch.binarySearch(inputArr, 2) ==2 ); + } + + BinarySearch binarySearch1 = new BinarySearch() ; + @Test + public void tesInsertIntoSorted1() { + int[] inputArr = new int[] {0,1,2,3,4}; + assertTrue( binarySearch1.binarySearch(inputArr, 1)==1 ); + } + + BinarySearch binarySearch2 = new BinarySearch() ; + @Test + public void tesInsertIntoSorted2() { + int[] inputArr = new int[] {0,1,2,3,4}; + assertTrue( binarySearch2.binarySearch(inputArr, 0)==0 ); + } + + BinarySearch binarySearch3 = new BinarySearch() ; + @Test + public void tesInsertIntoSorted3() { + int[] inputArr = new int[] {0,1,2,3,4}; + assertTrue( binarySearch3.binarySearch(inputArr, 1)==1 ); + } + + BinarySearch binarySearch4 = new BinarySearch() ; + @Test + public void tesInsertIntoSorted4() { + int[] inputArr = new int[] {0,1,2,3,4}; + assertTrue( binarySearch4.binarySearch(inputArr, 1)==1 ); + } + + +} From 21ed2887b7143fea93464c4ac58c2bf218d37aca Mon Sep 17 00:00:00 2001 From: homnay1234 <99612060+homnay1234@users.noreply.github.com> Date: Sun, 20 Feb 2022 22:40:40 +0700 Subject: [PATCH 3/4] Make a small Refactor for BinarySearch Algorithm I you should change two methods from private to public for it's easier to use in another file, for example jUnit Test in Package private static int binarySearch(int[] a, int n) { return binarySearch(a, n, 0, a.length - 1); } private static int binarySearchNonRecursive(int[] a, int n) { ...... } --- Refactor BinarySearch Algorithm | 73 +++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 Refactor BinarySearch Algorithm diff --git a/Refactor BinarySearch Algorithm b/Refactor BinarySearch Algorithm new file mode 100644 index 00000000..2ec3ded3 --- /dev/null +++ b/Refactor BinarySearch Algorithm @@ -0,0 +1,73 @@ +public class BinarySearch { + + /** + * Searches an element {@param n} in a sorted array {@param a} + * and returns its index in O(log n) time. The Index may not + * correspond to the first occurrence of the element. + * + * @param a sorted array to be searched + * @param n number to be searched in the array + * @return index of {@param n} or {@code -1} if not present + */ + public static int binarySearch(int[] a, int n) { + return binarySearch(a, n, 0, a.length - 1); + } + + public static int binarySearch(int[] a, int n, int low, int high) { + + if (low <= high) { + int mid = (low + high) / 2; // to prevent overflow you can instead do: mid = low + (high - low) / 2 + + if (n == a[mid]) { + return mid; + } else if (n < a[mid]) { + return binarySearch(a, n, 0, mid - 1); + } else { + return binarySearch(a, n, mid + 1, high); + } + } else { + return -1; + } + } + + /** + * Non-recursive version of binary search. + * + * @param a sorted array to be searched + * @param n number to be searched in the array + * @return index of {@param n} or {@code -1} if not present + */ + public static int binarySearchNonRecursive(int[] a, int n) { + int low = 0, high = a.length, mid; + while (low <= high) { + mid = (low + high) / 2; // to prevent overflow you can instead do: mid = low + (high - low) / 2 + if (n == a[mid]) { + return mid; + } else if (n < a[mid]) { + high = mid - 1; + } else { + low = mid + 1; + } + } + return -1; + } + + /** + * Driver for testing. + * + * @param a + */ + public static void main(String[] args) { + System.out.println(binarySearch(new int[]{0, 2}, 2)); + System.out.println(binarySearch(new int[]{0, 1, 2, 3}, 2)); + System.out.println(binarySearch(new int[]{0, 1, 2, 3}, 3)); + System.out.println(binarySearch(new int[]{0, 2}, 0)); + System.out.println(binarySearch(new int[]{0, 1, 2, 2, 2, 3, 3}, 2)); // doesn't return index of first occurrence + System.out.println("---------"); + System.out.println(binarySearchNonRecursive(new int[]{0, 2}, 2)); + System.out.println(binarySearchNonRecursive(new int[]{0, 1, 2, 3}, 2)); + System.out.println(binarySearchNonRecursive(new int[]{0, 1, 2, 3}, 3)); + System.out.println(binarySearchNonRecursive(new int[]{0, 2}, 0)); + System.out.println(binarySearchNonRecursive(new int[]{0, 1, 2, 2, 2, 3, 3}, 2)); + } +} From 0eba744683851db88ae2b4b1a3005fc761f0284b Mon Sep 17 00:00:00 2001 From: homnay1234 <99612060+homnay1234@users.noreply.github.com> Date: Sun, 20 Feb 2022 22:51:29 +0700 Subject: [PATCH 4/4] jUnit Test on Binary Search Algorithm I hope i can add a jUnit Test on Binary Search Algorithm on your Project --- TestBinarySearch2 | 73 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 TestBinarySearch2 diff --git a/TestBinarySearch2 b/TestBinarySearch2 new file mode 100644 index 00000000..2ec3ded3 --- /dev/null +++ b/TestBinarySearch2 @@ -0,0 +1,73 @@ +public class BinarySearch { + + /** + * Searches an element {@param n} in a sorted array {@param a} + * and returns its index in O(log n) time. The Index may not + * correspond to the first occurrence of the element. + * + * @param a sorted array to be searched + * @param n number to be searched in the array + * @return index of {@param n} or {@code -1} if not present + */ + public static int binarySearch(int[] a, int n) { + return binarySearch(a, n, 0, a.length - 1); + } + + public static int binarySearch(int[] a, int n, int low, int high) { + + if (low <= high) { + int mid = (low + high) / 2; // to prevent overflow you can instead do: mid = low + (high - low) / 2 + + if (n == a[mid]) { + return mid; + } else if (n < a[mid]) { + return binarySearch(a, n, 0, mid - 1); + } else { + return binarySearch(a, n, mid + 1, high); + } + } else { + return -1; + } + } + + /** + * Non-recursive version of binary search. + * + * @param a sorted array to be searched + * @param n number to be searched in the array + * @return index of {@param n} or {@code -1} if not present + */ + public static int binarySearchNonRecursive(int[] a, int n) { + int low = 0, high = a.length, mid; + while (low <= high) { + mid = (low + high) / 2; // to prevent overflow you can instead do: mid = low + (high - low) / 2 + if (n == a[mid]) { + return mid; + } else if (n < a[mid]) { + high = mid - 1; + } else { + low = mid + 1; + } + } + return -1; + } + + /** + * Driver for testing. + * + * @param a + */ + public static void main(String[] args) { + System.out.println(binarySearch(new int[]{0, 2}, 2)); + System.out.println(binarySearch(new int[]{0, 1, 2, 3}, 2)); + System.out.println(binarySearch(new int[]{0, 1, 2, 3}, 3)); + System.out.println(binarySearch(new int[]{0, 2}, 0)); + System.out.println(binarySearch(new int[]{0, 1, 2, 2, 2, 3, 3}, 2)); // doesn't return index of first occurrence + System.out.println("---------"); + System.out.println(binarySearchNonRecursive(new int[]{0, 2}, 2)); + System.out.println(binarySearchNonRecursive(new int[]{0, 1, 2, 3}, 2)); + System.out.println(binarySearchNonRecursive(new int[]{0, 1, 2, 3}, 3)); + System.out.println(binarySearchNonRecursive(new int[]{0, 2}, 0)); + System.out.println(binarySearchNonRecursive(new int[]{0, 1, 2, 2, 2, 3, 3}, 2)); + } +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy