Add tests, remove main in TernarySearch (#5677)

This commit is contained in:
Hardik Pawar
2024-10-11 02:02:32 +05:30
committed by GitHub
parent 6802029251
commit 4ec2701302
3 changed files with 82 additions and 22 deletions

View File

@ -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
@ -60,23 +57,4 @@ public class TernarySearch implements SearchAlgorithm {
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);
}
}