File tree 3 files changed +59
-17
lines changed
main/java/com/thealgorithms/searches
test/java/com/thealgorithms/searches
3 files changed +59
-17
lines changed Original file line number Diff line number Diff line change 1016
1016
* [ RecursiveBinarySearchTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java )
1017
1017
* [ RowColumnWiseSorted2dArrayBinarySearchTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java )
1018
1018
* [ SortOrderAgnosticBinarySearchTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SortOrderAgnosticBinarySearchTest.java )
1019
+ * [ SquareRootBinarySearchTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SquareRootBinarySearchTest.java )
1019
1020
* [ TestSearchInARowAndColWiseSortedMatrix] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java )
1020
1021
* sorts
1021
1022
* [ BeadSortTest] ( https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/BeadSortTest.java )
Original file line number Diff line number Diff line change 1
1
package com .thealgorithms .searches ;
2
2
3
- import java .util .Scanner ;
4
-
5
3
/**
6
4
* Given an integer x, find the square root of x. If x is not a perfect square,
7
5
* then return floor(√x).
@@ -18,20 +16,6 @@ public final class SquareRootBinarySearch {
18
16
private SquareRootBinarySearch () {
19
17
}
20
18
21
- /**
22
- * This is the driver method.
23
- *
24
- * @param args Command line arguments
25
- */
26
- public static void main (String [] args ) {
27
- Scanner sc = new Scanner (System .in );
28
- System .out .print ("Enter a number you want to calculate square root of : " );
29
- int num = sc .nextInt ();
30
- long ans = squareRoot (num );
31
- System .out .println ("The square root is : " + ans );
32
- sc .close ();
33
- }
34
-
35
19
/**
36
20
* This function calculates the floor of square root of a number. We use
37
21
* Binary Search algorithm to calculate the square root in a more optimised
@@ -40,7 +24,7 @@ public static void main(String[] args) {
40
24
* @param num Number
41
25
* @return answer
42
26
*/
43
- private static long squareRoot (long num ) {
27
+ static long squareRoot (long num ) {
44
28
if (num == 0 || num == 1 ) {
45
29
return num ;
46
30
}
Original file line number Diff line number Diff line change
1
+ package com .thealgorithms .searches ;
2
+
3
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
4
+
5
+ import org .junit .jupiter .api .Test ;
6
+
7
+ class SquareRootBinarySearchTest {
8
+
9
+ @ Test
10
+ void testPerfectSquare () {
11
+ long input = 16 ;
12
+ long expected = 4 ;
13
+ assertEquals (expected , SquareRootBinarySearch .squareRoot (input ), "Square root of 16 should be 4" );
14
+ }
15
+
16
+ @ Test
17
+ void testNonPerfectSquare () {
18
+ long input = 15 ;
19
+ long expected = 3 ;
20
+ assertEquals (expected , SquareRootBinarySearch .squareRoot (input ), "Square root of 15 should be 3" );
21
+ }
22
+
23
+ @ Test
24
+ void testZero () {
25
+ long input = 0 ;
26
+ long expected = 0 ;
27
+ assertEquals (expected , SquareRootBinarySearch .squareRoot (input ), "Square root of 0 should be 0" );
28
+ }
29
+
30
+ @ Test
31
+ void testOne () {
32
+ long input = 1 ;
33
+ long expected = 1 ;
34
+ assertEquals (expected , SquareRootBinarySearch .squareRoot (input ), "Square root of 1 should be 1" );
35
+ }
36
+
37
+ @ Test
38
+ void testLargeNumberPerfectSquare () {
39
+ long input = 1000000 ;
40
+ long expected = 1000 ;
41
+ assertEquals (expected , SquareRootBinarySearch .squareRoot (input ), "Square root of 1000000 should be 1000" );
42
+ }
43
+
44
+ @ Test
45
+ void testLargeNumberNonPerfectSquare () {
46
+ long input = 999999 ;
47
+ long expected = 999 ;
48
+ assertEquals (expected , SquareRootBinarySearch .squareRoot (input ), "Square root of 999999 should be 999" );
49
+ }
50
+
51
+ @ Test
52
+ void testNegativeInput () {
53
+ long input = -4 ;
54
+ long expected = 0 ; // Assuming the implementation should return 0 for negative input
55
+ assertEquals (expected , SquareRootBinarySearch .squareRoot (input ), "Square root of negative number should return 0" );
56
+ }
57
+ }
You can’t perform that action at this time.
0 commit comments