mirror of
https://github.com/TheAlgorithms/Java.git
synced 2026-03-13 08:40:43 +08:00
@@ -4,20 +4,19 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class BinarySearch2dArrayTest {
|
||||
|
||||
@Test
|
||||
// valid test case
|
||||
public void BinarySearch2dArrayTestMiddle() {
|
||||
int[][] arr = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
|
||||
int[][] arr = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
|
||||
int target = 6;
|
||||
|
||||
int[] ans = BinarySearch2dArray.BinarySearch(arr, target);
|
||||
int[] expected = { 1, 1 };
|
||||
int[] expected = {1, 1};
|
||||
System.out.println(Arrays.toString(ans));
|
||||
assertEquals(1, ans[0]);
|
||||
assertEquals(1, ans[1]);
|
||||
@@ -26,11 +25,11 @@ public class BinarySearch2dArrayTest {
|
||||
@Test
|
||||
// valid test case
|
||||
public void BinarySearch2dArrayTestMiddleSide() {
|
||||
int[][] arr = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
|
||||
int[][] arr = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
|
||||
int target = 8;
|
||||
|
||||
int[] ans = BinarySearch2dArray.BinarySearch(arr, target);
|
||||
int[] expected = { 1, 3 };
|
||||
int[] expected = {1, 3};
|
||||
System.out.println(Arrays.toString(ans));
|
||||
assertEquals(1, ans[0]);
|
||||
assertEquals(3, ans[1]);
|
||||
@@ -39,11 +38,11 @@ public class BinarySearch2dArrayTest {
|
||||
@Test
|
||||
// valid test case
|
||||
public void BinarySearch2dArrayTestUpper() {
|
||||
int[][] arr = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
|
||||
int[][] arr = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
|
||||
int target = 2;
|
||||
|
||||
int[] ans = BinarySearch2dArray.BinarySearch(arr, target);
|
||||
int[] expected = { 0, 1 };
|
||||
int[] expected = {0, 1};
|
||||
System.out.println(Arrays.toString(ans));
|
||||
assertEquals(0, ans[0]);
|
||||
assertEquals(1, ans[1]);
|
||||
@@ -52,11 +51,11 @@ public class BinarySearch2dArrayTest {
|
||||
@Test
|
||||
// valid test case
|
||||
public void BinarySearch2dArrayTestUpperSide() {
|
||||
int[][] arr = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
|
||||
int[][] arr = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
|
||||
int target = 1;
|
||||
|
||||
int[] ans = BinarySearch2dArray.BinarySearch(arr, target);
|
||||
int[] expected = { 0, 0 };
|
||||
int[] expected = {0, 0};
|
||||
System.out.println(Arrays.toString(ans));
|
||||
assertEquals(0, ans[0]);
|
||||
assertEquals(0, ans[1]);
|
||||
@@ -65,11 +64,11 @@ public class BinarySearch2dArrayTest {
|
||||
@Test
|
||||
// valid test case
|
||||
public void BinarySearch2dArrayTestLower() {
|
||||
int[][] arr = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
|
||||
int[][] arr = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
|
||||
int target = 10;
|
||||
|
||||
int[] ans = BinarySearch2dArray.BinarySearch(arr, target);
|
||||
int[] expected = { 2, 1 };
|
||||
int[] expected = {2, 1};
|
||||
System.out.println(Arrays.toString(ans));
|
||||
assertEquals(2, ans[0]);
|
||||
assertEquals(1, ans[1]);
|
||||
@@ -78,11 +77,11 @@ public class BinarySearch2dArrayTest {
|
||||
@Test
|
||||
// valid test case
|
||||
public void BinarySearch2dArrayTestLowerSide() {
|
||||
int[][] arr = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
|
||||
int[][] arr = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
|
||||
int target = 11;
|
||||
|
||||
int[] ans = BinarySearch2dArray.BinarySearch(arr, target);
|
||||
int[] expected = { 2, 2 };
|
||||
int[] expected = {2, 2};
|
||||
System.out.println(Arrays.toString(ans));
|
||||
assertEquals(2, ans[0]);
|
||||
assertEquals(2, ans[1]);
|
||||
@@ -91,11 +90,11 @@ public class BinarySearch2dArrayTest {
|
||||
@Test
|
||||
// valid test case
|
||||
public void BinarySearch2dArrayTestNotFound() {
|
||||
int[][] arr = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
|
||||
int[][] arr = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
|
||||
int target = 101;
|
||||
|
||||
int[] ans = BinarySearch2dArray.BinarySearch(arr, target);
|
||||
int[] expected = { -1, -1 };
|
||||
int[] expected = {-1, -1};
|
||||
System.out.println(Arrays.toString(ans));
|
||||
assertEquals(-1, ans[0]);
|
||||
assertEquals(-1, ans[1]);
|
||||
@@ -106,7 +105,7 @@ public class BinarySearch2dArrayTest {
|
||||
*/
|
||||
@Test
|
||||
public void BinarySearch2dArrayTestOneRow() {
|
||||
int[][] arr = { { 1, 2, 3, 4 }};
|
||||
int[][] arr = {{1, 2, 3, 4}};
|
||||
int target = 2;
|
||||
|
||||
// Assert that the requirement, that the array only has one row, is fulfilled.
|
||||
@@ -122,10 +121,11 @@ public class BinarySearch2dArrayTest {
|
||||
*/
|
||||
@Test
|
||||
public void BinarySearch2dArrayTestTargetInMiddle() {
|
||||
int[][] arr = { { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 }, { 11, 12, 13, 14, 15} };
|
||||
int[][] arr = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}};
|
||||
int target = 8;
|
||||
// Assert that the requirement, that the target is in the middle row and middle column, is fulfilled.
|
||||
assertEquals(arr[arr.length/2][arr[0].length/2], target);
|
||||
// Assert that the requirement, that the target is in the middle row and middle column, is
|
||||
// fulfilled.
|
||||
assertEquals(arr[arr.length / 2][arr[0].length / 2], target);
|
||||
int[] ans = BinarySearch2dArray.BinarySearch(arr, target);
|
||||
System.out.println(Arrays.toString(ans));
|
||||
assertEquals(1, ans[0]);
|
||||
@@ -138,13 +138,13 @@ public class BinarySearch2dArrayTest {
|
||||
*/
|
||||
@Test
|
||||
public void BinarySearch2dArrayTestTargetAboveMiddleRowInMiddleColumn() {
|
||||
int[][] arr = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
|
||||
int[][] arr = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
|
||||
int target = 3;
|
||||
|
||||
// Assert that the requirement, that he target is in the middle column,
|
||||
// in an array with an even number of columns, and on the row "above" the middle row.
|
||||
assertEquals(arr[0].length % 2, 0);
|
||||
assertEquals(arr[arr.length/2-1][arr[0].length/2], target);
|
||||
assertEquals(arr[arr.length / 2 - 1][arr[0].length / 2], target);
|
||||
int[] ans = BinarySearch2dArray.BinarySearch(arr, target);
|
||||
System.out.println(Arrays.toString(ans));
|
||||
assertEquals(0, ans[0]);
|
||||
@@ -160,7 +160,7 @@ public class BinarySearch2dArrayTest {
|
||||
int target = 5;
|
||||
|
||||
// Assert that an empty array is not valid input for the method.
|
||||
assertThrows(ArrayIndexOutOfBoundsException.class, () -> BinarySearch2dArray.BinarySearch(arr, target));
|
||||
assertThrows(ArrayIndexOutOfBoundsException.class,
|
||||
() -> BinarySearch2dArray.BinarySearch(arr, target));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,28 +1,21 @@
|
||||
package com.thealgorithms.searches;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class BreadthFirstSearchTest {
|
||||
|
||||
private static final DepthFirstSearch.Node rootNode = new DepthFirstSearch.Node(
|
||||
"A",
|
||||
List.of(
|
||||
new DepthFirstSearch.Node(
|
||||
"B",
|
||||
List.of(
|
||||
new DepthFirstSearch.Node("D"),
|
||||
new DepthFirstSearch.Node("F", List.of(new DepthFirstSearch.Node("H"), new DepthFirstSearch.Node("I")))
|
||||
)
|
||||
),
|
||||
new DepthFirstSearch.Node("C", List.of(new DepthFirstSearch.Node("G"))),
|
||||
new DepthFirstSearch.Node("E")
|
||||
)
|
||||
);
|
||||
private static final DepthFirstSearch.Node rootNode = new DepthFirstSearch.Node("A",
|
||||
List.of(
|
||||
new DepthFirstSearch.Node("B",
|
||||
List.of(new DepthFirstSearch.Node("D"),
|
||||
new DepthFirstSearch.Node("F",
|
||||
List.of(new DepthFirstSearch.Node("H"), new DepthFirstSearch.Node("I"))))),
|
||||
new DepthFirstSearch.Node("C", List.of(new DepthFirstSearch.Node("G"))),
|
||||
new DepthFirstSearch.Node("E")));
|
||||
|
||||
@Test
|
||||
void searchI() {
|
||||
|
||||
@@ -8,10 +8,9 @@ public class HowManyTimesRotatedTest {
|
||||
|
||||
@Test
|
||||
public void testHowManyTimesRotated() {
|
||||
int[] arr1 = {5, 1,2,3,4};
|
||||
int[] arr1 = {5, 1, 2, 3, 4};
|
||||
assertEquals(1, HowManyTimesRotated.rotated(arr1));
|
||||
int[] arr2 = {15,17,2,3,5};
|
||||
int[] arr2 = {15, 17, 2, 3, 5};
|
||||
assertEquals(2, HowManyTimesRotated.rotated(arr2));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,72 +1,69 @@
|
||||
package com.thealgorithms.searches;
|
||||
|
||||
import com.thealgorithms.searches.OrderAgnosticBinarySearch;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import com.thealgorithms.searches.OrderAgnosticBinarySearch;
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class OrderAgnosticBinarySearchTest {
|
||||
@Test
|
||||
//valid Test Case
|
||||
public void ElementInMiddle() {
|
||||
int[] arr = {10, 20, 30, 40, 50};
|
||||
int answer = OrderAgnosticBinarySearch.BinSearchAlgo(arr, 0, arr.length - 1, 30);
|
||||
System.out.println(answer);
|
||||
int expected = 2;
|
||||
assertEquals(expected, answer);
|
||||
}
|
||||
@Test
|
||||
// valid Test Case
|
||||
public void ElementInMiddle() {
|
||||
int[] arr = {10, 20, 30, 40, 50};
|
||||
int answer = OrderAgnosticBinarySearch.BinSearchAlgo(arr, 0, arr.length - 1, 30);
|
||||
System.out.println(answer);
|
||||
int expected = 2;
|
||||
assertEquals(expected, answer);
|
||||
}
|
||||
|
||||
@Test
|
||||
//valid Test Case
|
||||
public void RightHalfDescOrder() {
|
||||
int[] arr = {50, 40, 30, 20, 10};
|
||||
int answer = OrderAgnosticBinarySearch.BinSearchAlgo(arr, 0, arr.length - 1, 10);
|
||||
System.out.println(answer);
|
||||
int expected = 4;
|
||||
assertEquals(expected, answer);
|
||||
}
|
||||
@Test
|
||||
// valid Test Case
|
||||
public void RightHalfDescOrder() {
|
||||
int[] arr = {50, 40, 30, 20, 10};
|
||||
int answer = OrderAgnosticBinarySearch.BinSearchAlgo(arr, 0, arr.length - 1, 10);
|
||||
System.out.println(answer);
|
||||
int expected = 4;
|
||||
assertEquals(expected, answer);
|
||||
}
|
||||
|
||||
@Test
|
||||
//valid test case
|
||||
public void LeftHalfDescOrder() {
|
||||
int[] arr = {50, 40, 30, 20, 10};
|
||||
int answer = OrderAgnosticBinarySearch.BinSearchAlgo(arr, 0, arr.length - 1, 50);
|
||||
System.out.println(answer);
|
||||
int expected = 0;
|
||||
assertEquals(expected, answer);
|
||||
}
|
||||
@Test
|
||||
// valid test case
|
||||
public void LeftHalfDescOrder() {
|
||||
int[] arr = {50, 40, 30, 20, 10};
|
||||
int answer = OrderAgnosticBinarySearch.BinSearchAlgo(arr, 0, arr.length - 1, 50);
|
||||
System.out.println(answer);
|
||||
int expected = 0;
|
||||
assertEquals(expected, answer);
|
||||
}
|
||||
|
||||
@Test
|
||||
//valid test case
|
||||
public void RightHalfAscOrder() {
|
||||
int[] arr = {10, 20, 30, 40, 50};
|
||||
int answer = OrderAgnosticBinarySearch.BinSearchAlgo(arr, 0, arr.length - 1, 50);
|
||||
System.out.println(answer);
|
||||
int expected = 4;
|
||||
assertEquals(expected, answer);
|
||||
}
|
||||
@Test
|
||||
// valid test case
|
||||
public void RightHalfAscOrder() {
|
||||
int[] arr = {10, 20, 30, 40, 50};
|
||||
int answer = OrderAgnosticBinarySearch.BinSearchAlgo(arr, 0, arr.length - 1, 50);
|
||||
System.out.println(answer);
|
||||
int expected = 4;
|
||||
assertEquals(expected, answer);
|
||||
}
|
||||
|
||||
@Test
|
||||
//valid test case
|
||||
public void LeftHalfAscOrder() {
|
||||
int[] arr = {10, 20, 30, 40, 50};
|
||||
int answer = OrderAgnosticBinarySearch.BinSearchAlgo(arr, 0, arr.length - 1, 10);
|
||||
System.out.println(answer);
|
||||
int expected = 0;
|
||||
assertEquals(expected, answer);
|
||||
}
|
||||
@Test
|
||||
// valid test case
|
||||
public void LeftHalfAscOrder() {
|
||||
int[] arr = {10, 20, 30, 40, 50};
|
||||
int answer = OrderAgnosticBinarySearch.BinSearchAlgo(arr, 0, arr.length - 1, 10);
|
||||
System.out.println(answer);
|
||||
int expected = 0;
|
||||
assertEquals(expected, answer);
|
||||
}
|
||||
|
||||
@Test
|
||||
//valid test case
|
||||
public void ElementNotFound() {
|
||||
int[] arr = {10, 20, 30, 40, 50};
|
||||
int answer = OrderAgnosticBinarySearch.BinSearchAlgo(arr, 0, arr.length - 1, 100);
|
||||
System.out.println(answer);
|
||||
int expected = -1;
|
||||
assertEquals(expected, answer);
|
||||
}
|
||||
}
|
||||
@Test
|
||||
// valid test case
|
||||
public void ElementNotFound() {
|
||||
int[] arr = {10, 20, 30, 40, 50};
|
||||
int answer = OrderAgnosticBinarySearch.BinSearchAlgo(arr, 0, arr.length - 1, 100);
|
||||
System.out.println(answer);
|
||||
int expected = -1;
|
||||
assertEquals(expected, answer);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,10 +180,8 @@ class QuickSelectTest {
|
||||
|
||||
@Test
|
||||
void quickSelectNullList() {
|
||||
NullPointerException exception = assertThrows(
|
||||
NullPointerException.class,
|
||||
() -> QuickSelect.select(null, 0)
|
||||
);
|
||||
NullPointerException exception
|
||||
= assertThrows(NullPointerException.class, () -> QuickSelect.select(null, 0));
|
||||
String expectedMsg = "The list of elements must not be null.";
|
||||
assertEquals(expectedMsg, exception.getMessage());
|
||||
}
|
||||
@@ -191,32 +189,25 @@ class QuickSelectTest {
|
||||
@Test
|
||||
void quickSelectEmptyList() {
|
||||
List<String> objects = Collections.emptyList();
|
||||
IllegalArgumentException exception = assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> QuickSelect.select(objects, 0)
|
||||
);
|
||||
IllegalArgumentException exception
|
||||
= assertThrows(IllegalArgumentException.class, () -> QuickSelect.select(objects, 0));
|
||||
String expectedMsg = "The list of elements must not be empty.";
|
||||
assertEquals(expectedMsg, exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void quickSelectIndexOutOfLeftBound() {
|
||||
IndexOutOfBoundsException exception = assertThrows(
|
||||
IndexOutOfBoundsException.class,
|
||||
() -> QuickSelect.select(Collections.singletonList(1), -1)
|
||||
);
|
||||
IndexOutOfBoundsException exception = assertThrows(IndexOutOfBoundsException.class,
|
||||
() -> QuickSelect.select(Collections.singletonList(1), -1));
|
||||
String expectedMsg = "The index must not be negative.";
|
||||
assertEquals(expectedMsg, exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void quickSelectIndexOutOfRightBound() {
|
||||
IndexOutOfBoundsException exception = assertThrows(
|
||||
IndexOutOfBoundsException.class,
|
||||
() -> QuickSelect.select(Collections.singletonList(1), 1)
|
||||
);
|
||||
String expectedMsg =
|
||||
"The index must be less than the number of elements.";
|
||||
IndexOutOfBoundsException exception = assertThrows(IndexOutOfBoundsException.class,
|
||||
() -> QuickSelect.select(Collections.singletonList(1), 1));
|
||||
String expectedMsg = "The index must be less than the number of elements.";
|
||||
assertEquals(expectedMsg, exception.getMessage());
|
||||
}
|
||||
|
||||
@@ -230,15 +221,12 @@ class QuickSelectTest {
|
||||
}
|
||||
|
||||
private static List<Character> generateRandomCharacters(int n) {
|
||||
return RANDOM
|
||||
.ints(n, ASCII_A, ASCII_Z)
|
||||
return RANDOM.ints(n, ASCII_A, ASCII_Z)
|
||||
.mapToObj(i -> (char) i)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private static <T extends Comparable<T>> List<T> getSortedCopyOfList(
|
||||
List<T> list
|
||||
) {
|
||||
private static <T extends Comparable<T>> List<T> getSortedCopyOfList(List<T> list) {
|
||||
return list.stream().sorted().collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,108 +6,108 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
public class RowColumnWiseSorted2dArrayBinarySearchTest {
|
||||
|
||||
@Test
|
||||
public void rowColumnSorted2dArrayBinarySearchTestMiddle() {
|
||||
Integer[][] arr = {
|
||||
{ 10, 20, 30, 40 },
|
||||
{ 15, 25, 35, 45 },
|
||||
{ 18, 28, 38, 48 },
|
||||
{ 21, 31, 41, 51 },
|
||||
};
|
||||
Integer target = 35;
|
||||
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(arr, target);
|
||||
int[] expected = { 1, 2 };
|
||||
assertEquals(expected[0], ans[0]);
|
||||
assertEquals(expected[1], ans[1]);
|
||||
}
|
||||
@Test
|
||||
public void rowColumnSorted2dArrayBinarySearchTestMiddle() {
|
||||
Integer[][] arr = {
|
||||
{10, 20, 30, 40},
|
||||
{15, 25, 35, 45},
|
||||
{18, 28, 38, 48},
|
||||
{21, 31, 41, 51},
|
||||
};
|
||||
Integer target = 35;
|
||||
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(arr, target);
|
||||
int[] expected = {1, 2};
|
||||
assertEquals(expected[0], ans[0]);
|
||||
assertEquals(expected[1], ans[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rowColumnSorted2dArrayBinarySearchTestSide() {
|
||||
Integer[][] arr = {
|
||||
{ 10, 20, 30, 40 },
|
||||
{ 15, 25, 35, 45 },
|
||||
{ 18, 28, 38, 48 },
|
||||
{ 21, 31, 41, 51 },
|
||||
};
|
||||
Integer target = 48;
|
||||
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(arr, target);
|
||||
int[] expected = { 2, 3 };
|
||||
assertEquals(expected[0], ans[0]);
|
||||
assertEquals(expected[1], ans[1]);
|
||||
}
|
||||
@Test
|
||||
public void rowColumnSorted2dArrayBinarySearchTestSide() {
|
||||
Integer[][] arr = {
|
||||
{10, 20, 30, 40},
|
||||
{15, 25, 35, 45},
|
||||
{18, 28, 38, 48},
|
||||
{21, 31, 41, 51},
|
||||
};
|
||||
Integer target = 48;
|
||||
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(arr, target);
|
||||
int[] expected = {2, 3};
|
||||
assertEquals(expected[0], ans[0]);
|
||||
assertEquals(expected[1], ans[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rowColumnSorted2dArray_BinarySearchTestUpper() {
|
||||
Integer[][] arr = {
|
||||
{ 10, 20, 30, 40 },
|
||||
{ 15, 25, 35, 45 },
|
||||
{ 18, 28, 38, 48 },
|
||||
{ 21, 31, 41, 51 },
|
||||
};
|
||||
Integer target = 20;
|
||||
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(arr, target);
|
||||
int[] expected = { 0, 1 };
|
||||
assertEquals(expected[0], ans[0]);
|
||||
assertEquals(expected[1], ans[1]);
|
||||
}
|
||||
@Test
|
||||
public void rowColumnSorted2dArray_BinarySearchTestUpper() {
|
||||
Integer[][] arr = {
|
||||
{10, 20, 30, 40},
|
||||
{15, 25, 35, 45},
|
||||
{18, 28, 38, 48},
|
||||
{21, 31, 41, 51},
|
||||
};
|
||||
Integer target = 20;
|
||||
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(arr, target);
|
||||
int[] expected = {0, 1};
|
||||
assertEquals(expected[0], ans[0]);
|
||||
assertEquals(expected[1], ans[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rowColumnSorted2dArray_BinarySearchTestUpperSide() {
|
||||
Integer[][] arr = {
|
||||
{ 10, 20, 30, 40 },
|
||||
{ 15, 25, 35, 45 },
|
||||
{ 18, 28, 38, 48 },
|
||||
{ 21, 31, 41, 51 },
|
||||
};
|
||||
Integer target = 40;
|
||||
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(arr, target);
|
||||
int[] expected = { 0, 3 };
|
||||
assertEquals(expected[0], ans[0]);
|
||||
assertEquals(expected[1], ans[1]);
|
||||
}
|
||||
@Test
|
||||
public void rowColumnSorted2dArray_BinarySearchTestUpperSide() {
|
||||
Integer[][] arr = {
|
||||
{10, 20, 30, 40},
|
||||
{15, 25, 35, 45},
|
||||
{18, 28, 38, 48},
|
||||
{21, 31, 41, 51},
|
||||
};
|
||||
Integer target = 40;
|
||||
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(arr, target);
|
||||
int[] expected = {0, 3};
|
||||
assertEquals(expected[0], ans[0]);
|
||||
assertEquals(expected[1], ans[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rowColumnSorted2dArray_BinarySearchTestLower() {
|
||||
Integer[][] arr = {
|
||||
{ 10, 20, 30, 40 },
|
||||
{ 15, 25, 35, 45 },
|
||||
{ 18, 28, 38, 48 },
|
||||
{ 21, 31, 41, 51 },
|
||||
};
|
||||
Integer target = 31;
|
||||
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(arr, target);
|
||||
int[] expected = { 3, 1 };
|
||||
assertEquals(expected[0], ans[0]);
|
||||
assertEquals(expected[1], ans[1]);
|
||||
}
|
||||
@Test
|
||||
public void rowColumnSorted2dArray_BinarySearchTestLower() {
|
||||
Integer[][] arr = {
|
||||
{10, 20, 30, 40},
|
||||
{15, 25, 35, 45},
|
||||
{18, 28, 38, 48},
|
||||
{21, 31, 41, 51},
|
||||
};
|
||||
Integer target = 31;
|
||||
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(arr, target);
|
||||
int[] expected = {3, 1};
|
||||
assertEquals(expected[0], ans[0]);
|
||||
assertEquals(expected[1], ans[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rowColumnSorted2dArray_BinarySearchTestLowerSide() {
|
||||
Integer[][] arr = {
|
||||
{ 10, 20, 30, 40 },
|
||||
{ 15, 25, 35, 45 },
|
||||
{ 18, 28, 38, 48 },
|
||||
{ 21, 31, 41, 51 },
|
||||
};
|
||||
Integer target = 51;
|
||||
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(arr, target);
|
||||
int[] expected = { 3, 3 };
|
||||
assertEquals(expected[0], ans[0]);
|
||||
assertEquals(expected[1], ans[1]);
|
||||
}
|
||||
@Test
|
||||
public void rowColumnSorted2dArray_BinarySearchTestLowerSide() {
|
||||
Integer[][] arr = {
|
||||
{10, 20, 30, 40},
|
||||
{15, 25, 35, 45},
|
||||
{18, 28, 38, 48},
|
||||
{21, 31, 41, 51},
|
||||
};
|
||||
Integer target = 51;
|
||||
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(arr, target);
|
||||
int[] expected = {3, 3};
|
||||
assertEquals(expected[0], ans[0]);
|
||||
assertEquals(expected[1], ans[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rowColumnSorted2dArray_BinarySearchTestNotFound() {
|
||||
Integer[][] arr = {
|
||||
{ 10, 20, 30, 40 },
|
||||
{ 15, 25, 35, 45 },
|
||||
{ 18, 28, 38, 48 },
|
||||
{ 21, 31, 41, 51 },
|
||||
};
|
||||
Integer target = 101;
|
||||
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(arr, target);
|
||||
int[] expected = { -1, -1 };
|
||||
assertEquals(expected[0], ans[0]);
|
||||
assertEquals(expected[1], ans[1]);
|
||||
}
|
||||
@Test
|
||||
public void rowColumnSorted2dArray_BinarySearchTestNotFound() {
|
||||
Integer[][] arr = {
|
||||
{10, 20, 30, 40},
|
||||
{15, 25, 35, 45},
|
||||
{18, 28, 38, 48},
|
||||
{21, 31, 41, 51},
|
||||
};
|
||||
Integer target = 101;
|
||||
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(arr, target);
|
||||
int[] expected = {-1, -1};
|
||||
assertEquals(expected[0], ans[0]);
|
||||
assertEquals(expected[1], ans[1]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,38 +1,29 @@
|
||||
package com.thealgorithms.searches;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TestSearchInARowAndColWiseSortedMatrix {
|
||||
@Test
|
||||
public void searchItem() {
|
||||
int[][] matrix = {
|
||||
{ 3, 4, 5, 6, 7 },
|
||||
{ 8, 9, 10, 11, 12 },
|
||||
{ 14, 15, 16, 17, 18 },
|
||||
{ 23, 24, 25, 26, 27 },
|
||||
{ 30, 31, 32, 33, 34 }
|
||||
};
|
||||
int[][] matrix = {{3, 4, 5, 6, 7}, {8, 9, 10, 11, 12}, {14, 15, 16, 17, 18},
|
||||
{23, 24, 25, 26, 27}, {30, 31, 32, 33, 34}};
|
||||
|
||||
var test = new SearchInARowAndColWiseSortedMatrix();
|
||||
int[] res = test.search(matrix, 16);
|
||||
int[] expectedResult = { 2, 2 };
|
||||
int[] expectedResult = {2, 2};
|
||||
assertArrayEquals(expectedResult, res);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void notFound() {
|
||||
int[][] matrix = {
|
||||
{ 3, 4, 5, 6, 7 },
|
||||
{ 8, 9, 10, 11, 12 },
|
||||
{ 14, 15, 16, 17, 18 },
|
||||
{ 23, 24, 25, 26, 27 },
|
||||
{ 30, 31, 32, 33, 34 }
|
||||
};
|
||||
int[][] matrix = {{3, 4, 5, 6, 7}, {8, 9, 10, 11, 12}, {14, 15, 16, 17, 18},
|
||||
{23, 24, 25, 26, 27}, {30, 31, 32, 33, 34}};
|
||||
|
||||
var test = new SearchInARowAndColWiseSortedMatrix();
|
||||
int[] res = test.search(matrix, 96);
|
||||
int[] expectedResult = { -1, -1 };
|
||||
int[] expectedResult = {-1, -1};
|
||||
assertArrayEquals(expectedResult, res);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,24 +4,23 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class sortOrderAgnosticBinarySearchTest{
|
||||
public class sortOrderAgnosticBinarySearchTest {
|
||||
|
||||
@Test
|
||||
public void testAscending(){
|
||||
int[] arr = {1,2,3,4,5};// for ascending order.
|
||||
public void testAscending() {
|
||||
int[] arr = {1, 2, 3, 4, 5}; // for ascending order.
|
||||
int target = 2;
|
||||
int ans=sortOrderAgnosticBinarySearch.find(arr, target);
|
||||
int ans = sortOrderAgnosticBinarySearch.find(arr, target);
|
||||
int excepted = 1;
|
||||
assertEquals(excepted,ans);
|
||||
assertEquals(excepted, ans);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDescending(){
|
||||
int[] arr = {5,4,3,2,1};// for descending order.
|
||||
public void testDescending() {
|
||||
int[] arr = {5, 4, 3, 2, 1}; // for descending order.
|
||||
int target = 2;
|
||||
int ans=sortOrderAgnosticBinarySearch.find(arr, target);
|
||||
int ans = sortOrderAgnosticBinarySearch.find(arr, target);
|
||||
int excepted = 3;
|
||||
assertEquals(excepted,ans );
|
||||
assertEquals(excepted, ans);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user