Skip to content

Add tests, remove main in TernarySearch #5677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 10, 2024
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 @@ -1022,6 +1022,7 @@
* [SearchInARowAndColWiseSortedMatrixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrixTest.java)
* [SortOrderAgnosticBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SortOrderAgnosticBinarySearchTest.java)
* [SquareRootBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SquareRootBinarySearchTest.java)
* [TernarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/TernarySearchTest.java)
* [TestSearchInARowAndColWiseSortedMatrix](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java)
* [UnionFindTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/UnionFindTest.java)
* sorts
Expand Down
22 changes: 0 additions & 22 deletions src/main/java/com/thealgorithms/searches/TernarySearch.java
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;

/**
* A ternary search algorithm is a technique in computer science for finding the
Expand Down Expand Up @@ -60,23 +57,4 @@ private <T extends Comparable<T>> int ternarySearch(T[] arr, T key, int start, i
return ternarySearch(arr, key, mid1, mid2);
}
}

public static void main(String[] args) {
// just generate data
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)];

TernarySearch search = new TernarySearch();
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);
}
}
81 changes: 81 additions & 0 deletions src/test/java/com/thealgorithms/searches/TernarySearchTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.thealgorithms.searches;

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

import org.junit.jupiter.api.Test;

class TernarySearchTest {

@Test
void testFindElementInSortedArray() {
Integer[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
TernarySearch search = new TernarySearch();

int index = search.find(arr, 5);

assertEquals(4, index, "Should find the element 5 at index 4");
}

@Test
void testElementNotFound() {
Integer[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
TernarySearch search = new TernarySearch();

int index = search.find(arr, 11);

assertEquals(-1, index, "Should return -1 for element 11 which is not present");
}

@Test
void testFindFirstElement() {
Integer[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
TernarySearch search = new TernarySearch();

int index = search.find(arr, 1);

assertEquals(0, index, "Should find the first element 1 at index 0");
}

@Test
void testFindLastElement() {
Integer[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
TernarySearch search = new TernarySearch();

int index = search.find(arr, 10);

assertEquals(9, index, "Should find the last element 10 at index 9");
}

@Test
void testFindInLargeArray() {
Integer[] arr = new Integer[1000];
for (int i = 0; i < 1000; i++) {
arr[i] = i + 1; // Array from 1 to 1000
}
TernarySearch search = new TernarySearch();

int index = search.find(arr, 500);

assertEquals(499, index, "Should find element 500 at index 499");
}

@Test
void testNegativeNumbers() {
Integer[] arr = {-10, -5, -3, -1, 0, 1, 3, 5, 7, 10};
TernarySearch search = new TernarySearch();

int index = search.find(arr, -3);

assertEquals(2, index, "Should find the element -3 at index 2");
}

@Test
void testEdgeCaseEmptyArray() {
Integer[] arr = {};
TernarySearch search = new TernarySearch();

int index = search.find(arr, 5);

assertEquals(-1, index, "Should return -1 for an empty array");
}
}