Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* [IsPowerTwo](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/IsPowerTwo.java)
* [LowestSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java)
* [NonRepeatingNumberFinder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinder.java)
* [NumberAppearingOddTimes](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimes.java)
* [NumbersDifferentSigns](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumbersDifferentSigns.java)
* [ReverseBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java)
* [SingleBitOperations](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SingleBitOperations.java)
Expand Down Expand Up @@ -638,6 +639,7 @@
* [IsPowerTwoTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/IsPowerTwoTest.java)
* [LowestSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java)
* [NonRepeatingNumberFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinderTest.java)
* [NumberAppearingOddTimesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimesTest.java)
* [NumbersDifferentSignsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumbersDifferentSignsTest.java)
* [ReverseBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ReverseBitsTest.java)
* [SingleBitOperationsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SingleBitOperationsTest.java)
Expand Down Expand Up @@ -969,6 +971,7 @@
* [BreadthFirstSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/BreadthFirstSearchTest.java)
* [DepthFirstSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/DepthFirstSearchTest.java)
* [HowManyTimesRotatedTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/HowManyTimesRotatedTest.java)
* [IterativeBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/IterativeBinarySearchTest.java)
* [KMPSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/KMPSearchTest.java)
* [OrderAgnosticBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/OrderAgnosticBinarySearchTest.java)
* [PerfectBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/PerfectBinarySearchTest.java)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.thealgorithms.searches;

import com.thealgorithms.devutils.searches.SearchAlgorithm;
import java.util.Arrays;
import java.util.Random;
import java.util.stream.Stream;

/**
* Binary search is one of the most popular algorithms This class represents
Expand Down Expand Up @@ -55,23 +52,4 @@ public <T extends Comparable<T>> int find(T[] array, T key) {

return -1;
}

// Only a main method for test purpose
public static void main(String[] args) {
Random r = new Random();
int size = 100;
int maxElement = 100000;
Integer[] integers = Stream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().toArray(Integer[] ::new);

// the element that should be found
Integer shouldBeFound = integers[r.nextInt(size - 1)];

IterativeBinarySearch search = new IterativeBinarySearch();
int atIndex = search.find(integers, shouldBeFound);

System.out.printf("Should be found: %d. Found %d at index %d. An array length %d%n", shouldBeFound, integers[atIndex], atIndex, size);

int toCheck = Arrays.binarySearch(integers, shouldBeFound);
System.out.printf("Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package com.thealgorithms.searches;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

/**
* Unit tests for the IterativeBinarySearch class.
*/
class IterativeBinarySearchTest {

/**
* Test for basic binary search functionality when the element is found.
*/
@Test
void testBinarySearchFound() {
IterativeBinarySearch binarySearch = new IterativeBinarySearch();
Integer[] array = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512};
Integer key = 128;
int expectedIndex = 7; // Index of the key in the array
assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the found element should be 7.");
}

/**
* Test for binary search when the element is not present in the array.
*/
@Test
void testBinarySearchNotFound() {
IterativeBinarySearch binarySearch = new IterativeBinarySearch();
Integer[] array = {1, 2, 4, 8, 16};
Integer key = 6; // Element not present in the array
assertEquals(-1, binarySearch.find(array, key), "The element should not be found in the array.");
}

/**
* Test for binary search with the first element as the key.
*/
@Test
void testBinarySearchFirstElement() {
IterativeBinarySearch binarySearch = new IterativeBinarySearch();
Integer[] array = {1, 2, 4, 8, 16};
Integer key = 1; // First element
assertEquals(0, binarySearch.find(array, key), "The index of the first element should be 0.");
}

/**
* Test for binary search with the last element as the key.
*/
@Test
void testBinarySearchLastElement() {
IterativeBinarySearch binarySearch = new IterativeBinarySearch();
Integer[] array = {1, 2, 4, 8, 16};
Integer key = 16; // Last element
assertEquals(4, binarySearch.find(array, key), "The index of the last element should be 4.");
}

/**
* Test for binary search with a single element present.
*/
@Test
void testBinarySearchSingleElementFound() {
IterativeBinarySearch binarySearch = new IterativeBinarySearch();
Integer[] array = {1};
Integer key = 1; // Only element present
assertEquals(0, binarySearch.find(array, key), "The index of the single element should be 0.");
}

/**
* Test for binary search with a single element not present.
*/
@Test
void testBinarySearchSingleElementNotFound() {
IterativeBinarySearch binarySearch = new IterativeBinarySearch();
Integer[] array = {1};
Integer key = 2; // Key not present
assertEquals(-1, binarySearch.find(array, key), "The element should not be found in the array.");
}

/**
* Test for binary search with an empty array.
*/
@Test
void testBinarySearchEmptyArray() {
IterativeBinarySearch binarySearch = new IterativeBinarySearch();
Integer[] array = {}; // Empty array
Integer key = 1; // Key not present
assertEquals(-1, binarySearch.find(array, key), "The element should not be found in an empty array.");
}

/**
* Test for binary search on a large array.
*/
@Test
void testBinarySearchLargeArray() {
IterativeBinarySearch binarySearch = new IterativeBinarySearch();
Integer[] array = new Integer[10000];
for (int i = 0; i < array.length; i++) {
array[i] = i * 2;
} // Array from 0 to 19998, step 2
Integer key = 9998; // Present in the array
assertEquals(4999, binarySearch.find(array, key), "The index of the found element should be 4999.");
}

/**
* Test for binary search on large array with a non-existent key.
*/
@Test
void testBinarySearchLargeArrayNotFound() {
IterativeBinarySearch binarySearch = new IterativeBinarySearch();
Integer[] array = new Integer[10000];
for (int i = 0; i < array.length; i++) {
array[i] = i * 2;
} // Array from 0 to 19998, step 2
Integer key = 9999; // Key not present
assertEquals(-1, binarySearch.find(array, key), "The element should not be found in the array.");
}
}