parent
cce1dbd124
commit
03a4832a7d
@ -0,0 +1,16 @@
|
|||||||
|
package com.thealgorithms.devutils.searches;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The common interface of most searching algorithms that search in matrixes.
|
||||||
|
*
|
||||||
|
* @author Aitor Fidalgo (https://github.com/aitorfi)
|
||||||
|
*/
|
||||||
|
public interface MatrixSearchAlgorithm {
|
||||||
|
/**
|
||||||
|
* @param key is an element which should be found
|
||||||
|
* @param matrix is a matrix where the element should be found
|
||||||
|
* @param <T> Comparable type
|
||||||
|
* @return array containing the first found coordinates of the element
|
||||||
|
*/
|
||||||
|
<T extends Comparable<T>> int[] find(T matrix[][], T key);
|
||||||
|
}
|
@ -1,37 +1,46 @@
|
|||||||
package com.thealgorithms.searches;
|
package com.thealgorithms.searches;
|
||||||
|
|
||||||
// The search is for any array which is sorted row and column-wise too. For ex :
|
import com.thealgorithms.devutils.searches.MatrixSearchAlgorithm;
|
||||||
// int[][] arr = { {10, 20, 30, 40},
|
|
||||||
// {15, 25, 35, 45},
|
|
||||||
// {18, 28, 38, 48},
|
|
||||||
// {21, 31, 41, 51}
|
|
||||||
// };
|
|
||||||
// This array is sorted in both row and column manner.
|
|
||||||
// In this two pointers are taken, the first points to the 0th row and the second one points to end column, and then the
|
|
||||||
// element corresponding to the pointers placed in the array is compared with the target that either its equal, greater or
|
|
||||||
// smaller than the target. If the element is equal to the target, the co-ordinates of that element is returned i.e. an
|
|
||||||
// array of the two pointers will be returned, else if the target is greater than corresponding element then the pointer
|
|
||||||
// pointing to the 0th row will be incremented by 1, else if the target is lesser than the corresponding element then the
|
|
||||||
// pointer pointing to the end column will be decremented by 1. And if the element doesn't exist in the array, an array
|
|
||||||
// {-1, -1} will be returned.
|
|
||||||
|
|
||||||
public class RowColumnWiseSorted2dArrayBinarySearch {
|
/**
|
||||||
|
* The search is for any array which is sorted row and column-wise too. For ex :
|
||||||
|
* {{10, 20, 30, 40},
|
||||||
|
* {15, 25, 35, 45},
|
||||||
|
* {18, 28, 38, 48},
|
||||||
|
* {21, 31, 41, 51}}
|
||||||
|
*
|
||||||
|
* This array is sorted in both row and column manner.
|
||||||
|
* In this two pointers are taken, the first points to the 0th row and the second one points to end column, and then the
|
||||||
|
* element corresponding to the pointers placed in the array is compared with the target that either its equal, greater or
|
||||||
|
* smaller than the target. If the element is equal to the target, the co-ordinates of that element is returned i.e. an
|
||||||
|
* array of the two pointers will be returned, else if the target is greater than corresponding element then the pointer
|
||||||
|
* pointing to the 0th row will be incremented by 1, else if the target is lesser than the corresponding element then the
|
||||||
|
* pointer pointing to the end column will be decremented by 1. And if the element doesn't exist in the array, an array
|
||||||
|
* {-1, -1} will be returned.
|
||||||
|
*/
|
||||||
|
public class RowColumnWiseSorted2dArrayBinarySearch
|
||||||
|
implements MatrixSearchAlgorithm {
|
||||||
|
|
||||||
public static int[] search(int[][] matrix, int target) {
|
@Override
|
||||||
|
public <T extends Comparable<T>> int[] find(T[][] matrix, T key) {
|
||||||
|
return search(matrix, key);
|
||||||
|
}
|
||||||
|
|
||||||
int rowPointer = 0; //The pointer at 0th row
|
public static <T extends Comparable<T>> int[] search(T[][] matrix, T target) {
|
||||||
int colPointer = matrix.length-1; //The pointer at end column
|
int rowPointer = 0; //The pointer at 0th row
|
||||||
|
int colPointer = matrix.length - 1; //The pointer at end column
|
||||||
|
|
||||||
while (rowPointer < matrix.length && colPointer >= 0){
|
while (rowPointer < matrix.length && colPointer >= 0) {
|
||||||
|
int comp = target.compareTo(matrix[rowPointer][colPointer]);
|
||||||
|
|
||||||
if (matrix[rowPointer][colPointer] == target)
|
if (comp == 0) {
|
||||||
return new int[] {rowPointer, colPointer};
|
return new int[] { rowPointer, colPointer };
|
||||||
|
} else if (comp > 0) {
|
||||||
else if (matrix[rowPointer][colPointer] < target)
|
rowPointer++; //Incrementing the row pointer if the target is greater
|
||||||
rowPointer++; //Incrementing the row pointer if the target is greater
|
} else {
|
||||||
else
|
colPointer--; //Decrementing the column pointer if the target is lesser
|
||||||
colPointer--; //Decrementing the column pointer if the target is lesser
|
}
|
||||||
}
|
|
||||||
return new int[] {-1, -1}; //The not found condition
|
|
||||||
}
|
}
|
||||||
|
return new int[] { -1, -1 }; //The not found condition
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,124 +1,113 @@
|
|||||||
package com.thealgorithms.searches;
|
package com.thealgorithms.searches;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
public class RowColumnWiseSorted2dArrayBinarySearchTest {
|
public class RowColumnWiseSorted2dArrayBinarySearchTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
// valid test case
|
public void rowColumnSorted2dArrayBinarySearchTestMiddle() {
|
||||||
public void rowColumnSorted2dArrayBinarySearchTestMiddle() {
|
Integer[][] arr = {
|
||||||
int[][] arr = { {10, 20, 30, 40},
|
{ 10, 20, 30, 40 },
|
||||||
{15, 25, 35, 45},
|
{ 15, 25, 35, 45 },
|
||||||
{18, 28, 38, 48},
|
{ 18, 28, 38, 48 },
|
||||||
{21, 31, 41, 51}
|
{ 21, 31, 41, 51 },
|
||||||
};
|
};
|
||||||
int target = 35;
|
Integer target = 35;
|
||||||
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(arr, target);
|
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(arr, target);
|
||||||
int[] expected = {1,2};
|
int[] expected = { 1, 2 };
|
||||||
System.out.println(Arrays.toString(ans));
|
assertEquals(expected[0], ans[0]);
|
||||||
assertEquals(1, ans[0]);
|
assertEquals(expected[1], ans[1]);
|
||||||
assertEquals(2, ans[1]);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
// valid test case
|
public void rowColumnSorted2dArrayBinarySearchTestSide() {
|
||||||
public void rowColumnSorted2dArrayBinarySearchTestSide() {
|
Integer[][] arr = {
|
||||||
int[][] arr = { {10, 20, 30, 40},
|
{ 10, 20, 30, 40 },
|
||||||
{15, 25, 35, 45},
|
{ 15, 25, 35, 45 },
|
||||||
{18, 28, 38, 48},
|
{ 18, 28, 38, 48 },
|
||||||
{21, 31, 41, 51}
|
{ 21, 31, 41, 51 },
|
||||||
};
|
};
|
||||||
int target = 48;
|
Integer target = 48;
|
||||||
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(arr, target);
|
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(arr, target);
|
||||||
int[] expected = {2,3};
|
int[] expected = { 2, 3 };
|
||||||
System.out.println(Arrays.toString(ans));
|
assertEquals(expected[0], ans[0]);
|
||||||
assertEquals(2, ans[0]);
|
assertEquals(expected[1], ans[1]);
|
||||||
assertEquals(3, ans[1]);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
// valid test case
|
public void rowColumnSorted2dArray_BinarySearchTestUpper() {
|
||||||
public void rowColumnSorted2dArray_BinarySearchTestUpper() {
|
Integer[][] arr = {
|
||||||
int[][] arr = { {10, 20, 30, 40},
|
{ 10, 20, 30, 40 },
|
||||||
{15, 25, 35, 45},
|
{ 15, 25, 35, 45 },
|
||||||
{18, 28, 38, 48},
|
{ 18, 28, 38, 48 },
|
||||||
{21, 31, 41, 51}
|
{ 21, 31, 41, 51 },
|
||||||
};
|
};
|
||||||
int target = 20;
|
Integer target = 20;
|
||||||
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(arr, target);
|
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(arr, target);
|
||||||
int[] expected = {0,1};
|
int[] expected = { 0, 1 };
|
||||||
System.out.println(Arrays.toString(ans));
|
assertEquals(expected[0], ans[0]);
|
||||||
assertEquals(0, ans[0]);
|
assertEquals(expected[1], ans[1]);
|
||||||
assertEquals(1, ans[1]);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
// valid test case
|
public void rowColumnSorted2dArray_BinarySearchTestUpperSide() {
|
||||||
public void rowColumnSorted2dArray_BinarySearchTestUpperSide() {
|
Integer[][] arr = {
|
||||||
int[][] arr = { {10, 20, 30, 40},
|
{ 10, 20, 30, 40 },
|
||||||
{15, 25, 35, 45},
|
{ 15, 25, 35, 45 },
|
||||||
{18, 28, 38, 48},
|
{ 18, 28, 38, 48 },
|
||||||
{21, 31, 41, 51}
|
{ 21, 31, 41, 51 },
|
||||||
};
|
};
|
||||||
int target = 40;
|
Integer target = 40;
|
||||||
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(arr, target);
|
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(arr, target);
|
||||||
int[] expected = {0,3};
|
int[] expected = { 0, 3 };
|
||||||
System.out.println(Arrays.toString(ans));
|
assertEquals(expected[0], ans[0]);
|
||||||
assertEquals(0, ans[0]);
|
assertEquals(expected[1], ans[1]);
|
||||||
assertEquals(3, ans[1]);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
// valid test case
|
public void rowColumnSorted2dArray_BinarySearchTestLower() {
|
||||||
public void rowColumnSorted2dArray_BinarySearchTestLower() {
|
Integer[][] arr = {
|
||||||
int[][] arr = { {10, 20, 30, 40},
|
{ 10, 20, 30, 40 },
|
||||||
{15, 25, 35, 45},
|
{ 15, 25, 35, 45 },
|
||||||
{18, 28, 38, 48},
|
{ 18, 28, 38, 48 },
|
||||||
{21, 31, 41, 51}
|
{ 21, 31, 41, 51 },
|
||||||
};
|
};
|
||||||
int target = 31;
|
Integer target = 31;
|
||||||
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(arr, target);
|
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(arr, target);
|
||||||
int[] expected = {3,1};
|
int[] expected = { 3, 1 };
|
||||||
System.out.println(Arrays.toString(ans));
|
assertEquals(expected[0], ans[0]);
|
||||||
assertEquals(3, ans[0]);
|
assertEquals(expected[1], ans[1]);
|
||||||
assertEquals(1, ans[1]);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
// valid test case
|
public void rowColumnSorted2dArray_BinarySearchTestLowerSide() {
|
||||||
public void rowColumnSorted2dArray_BinarySearchTestLowerSide() {
|
Integer[][] arr = {
|
||||||
int[][] arr = { {10, 20, 30, 40},
|
{ 10, 20, 30, 40 },
|
||||||
{15, 25, 35, 45},
|
{ 15, 25, 35, 45 },
|
||||||
{18, 28, 38, 48},
|
{ 18, 28, 38, 48 },
|
||||||
{21, 31, 41, 51}
|
{ 21, 31, 41, 51 },
|
||||||
};
|
};
|
||||||
int target = 51;
|
Integer target = 51;
|
||||||
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(arr, target);
|
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(arr, target);
|
||||||
int[] expected = {3,3};
|
int[] expected = { 3, 3 };
|
||||||
System.out.println(Arrays.toString(ans));
|
assertEquals(expected[0], ans[0]);
|
||||||
assertEquals(3, ans[0]);
|
assertEquals(expected[1], ans[1]);
|
||||||
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_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]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user