Add an interface for matrix search algorithms (closes #3461) (#3464)

This commit is contained in:
Aitor Fidalgo Sánchez
2022-10-09 13:45:36 +02:00
committed by GitHub
parent cce1dbd124
commit 03a4832a7d
3 changed files with 152 additions and 138 deletions

View File

@ -1,124 +1,113 @@
package com.thealgorithms.searches;
import org.junit.jupiter.api.Test;
import java.util.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class RowColumnWiseSorted2dArrayBinarySearchTest {
@Test
// valid test case
public void rowColumnSorted2dArrayBinarySearchTestMiddle() {
int[][] arr = { {10, 20, 30, 40},
{15, 25, 35, 45},
{18, 28, 38, 48},
{21, 31, 41, 51}
};
int target = 35;
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(arr, target);
int[] expected = {1,2};
System.out.println(Arrays.toString(ans));
assertEquals(1, ans[0]);
assertEquals(2, 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
// valid test case
public void rowColumnSorted2dArrayBinarySearchTestSide() {
int[][] arr = { {10, 20, 30, 40},
{15, 25, 35, 45},
{18, 28, 38, 48},
{21, 31, 41, 51}
};
int target = 48;
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(arr, target);
int[] expected = {2,3};
System.out.println(Arrays.toString(ans));
assertEquals(2, ans[0]);
assertEquals(3, 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
// valid test case
public void rowColumnSorted2dArray_BinarySearchTestUpper() {
int[][] arr = { {10, 20, 30, 40},
{15, 25, 35, 45},
{18, 28, 38, 48},
{21, 31, 41, 51}
};
int target = 20;
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(arr, target);
int[] expected = {0,1};
System.out.println(Arrays.toString(ans));
assertEquals(0, ans[0]);
assertEquals(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
// valid test case
public void rowColumnSorted2dArray_BinarySearchTestUpperSide() {
int[][] arr = { {10, 20, 30, 40},
{15, 25, 35, 45},
{18, 28, 38, 48},
{21, 31, 41, 51}
};
int target = 40;
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(arr, target);
int[] expected = {0,3};
System.out.println(Arrays.toString(ans));
assertEquals(0, ans[0]);
assertEquals(3, 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
// valid test case
public void rowColumnSorted2dArray_BinarySearchTestLower() {
int[][] arr = { {10, 20, 30, 40},
{15, 25, 35, 45},
{18, 28, 38, 48},
{21, 31, 41, 51}
};
int target = 31;
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(arr, target);
int[] expected = {3,1};
System.out.println(Arrays.toString(ans));
assertEquals(3, ans[0]);
assertEquals(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
// valid test case
public void rowColumnSorted2dArray_BinarySearchTestLowerSide() {
int[][] arr = { {10, 20, 30, 40},
{15, 25, 35, 45},
{18, 28, 38, 48},
{21, 31, 41, 51}
};
int target = 51;
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(arr, target);
int[] expected = {3,3};
System.out.println(Arrays.toString(ans));
assertEquals(3, ans[0]);
assertEquals(3, ans[1]);
}
@Test
// valid test case
public void rowColumnSorted2dArray_BinarySearchTestNotFound() {
int[][] arr = { {10, 20, 30, 40},
{15, 25, 35, 45},
{18, 28, 38, 48},
{21, 31, 41, 51}
};
int target = 101;
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(arr, target);
int[] expected = {-1,-1};
System.out.println(Arrays.toString(ans));
assertEquals(-1, ans[0]);
assertEquals(-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]);
}
}