Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,7 @@
* [SRTFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SRTFSchedulingTest.java)
* searches
* [BinarySearch2dArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/BinarySearch2dArrayTest.java)
* [BinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/BinarySearchTest.java)
* [BM25InvertedIndexTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/BM25InvertedIndexTest.java)
* [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)
Expand Down
26 changes: 0 additions & 26 deletions src/main/java/com/thealgorithms/searches/BinarySearch.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package com.thealgorithms.searches;

import com.thealgorithms.devutils.searches.SearchAlgorithm;
import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.IntStream;

/**
* Binary search is one of the most popular algorithms The algorithm finds the
Expand Down Expand Up @@ -57,26 +53,4 @@ private <T extends Comparable<T>> int search(T[] array, T key, int left, int rig
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.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);
}
}
108 changes: 108 additions & 0 deletions src/test/java/com/thealgorithms/searches/BinarySearchTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.thealgorithms.searches;

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

import java.util.stream.IntStream;
import org.junit.jupiter.api.Test;

/**
* Unit tests for the BinarySearch class.
*/
class BinarySearchTest {

/**
* Test for basic binary search functionality.
*/
@Test
void testBinarySearchFound() {
BinarySearch binarySearch = new BinarySearch();
Integer[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int key = 7;
int expectedIndex = 6; // Index of the key in the array
assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the found element should be 6.");
}

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

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

/**
* Test for binary search with last element as the key.
*/
@Test
void testBinarySearchLastElement() {
BinarySearch binarySearch = new BinarySearch();
Integer[] array = {1, 2, 3, 4, 5};
int key = 5; // Last element
int expectedIndex = 4; // Index of the key in the array
assertEquals(expectedIndex, 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() {
BinarySearch binarySearch = new BinarySearch();
Integer[] array = {1};
int key = 1; // Only element present
int expectedIndex = 0; // Index of the key in the array
assertEquals(expectedIndex, 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() {
BinarySearch binarySearch = new BinarySearch();
Integer[] array = {1};
int key = 2; // Key not present
int expectedIndex = -1; // Key not found
assertEquals(expectedIndex, binarySearch.find(array, key), "The element should not be found in the array.");
}

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

/**
* Test for binary search on large array.
*/
@Test
void testBinarySearchLargeArray() {
BinarySearch binarySearch = new BinarySearch();
Integer[] array = IntStream.range(0, 10000).boxed().toArray(Integer[] ::new); // Array from 0 to 9999
int key = 9999; // Last element
int expectedIndex = 9999; // Index of the last element
assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the last element should be 9999.");
}
}