Skip to content

Implement saddleback search #1258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/main/java/com/search/SaddlebackSearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.search;

/**
* Saddleback search is an algorithm which finds the position of a target value within a 2D sorted array
*/
public class SaddlebackSearch {

/**
* This method performs Saddleback Search
*
* @param array The **Sorted** array in which we will search the element.
* @param value the element that we want to search for.
* @return The index (row and column) of the element if found.
* Else returns -1 -1.
*/
public static <T extends Comparable<T>> int[] find(T[][] array, T value) {
return search(array, value, array.length - 1, 0);
}

/**
* This method performs Saddleback Search
*
* @param array The **Sorted** array in which we will search the element.
* @param value the element that we want to search for.
* @param row the current row.
* @param col the current column.
* @return The index (row and column) of the element if found.
* Else returns -1 -1.
*/
private static <T extends Comparable<T>> int[] search(T[][] array, T value, int row, int col) {
if (row < 0 || col >= array[row].length) {
return new int[] {-1, -1};
}

int compareTo = array[row][col].compareTo(value);

if (compareTo == 0) {
return new int[] {row, col};
} else if (compareTo > 0) {
return search(array, value, row - 1, col);
}

return search(array, value, row, col + 1);
}

}
36 changes: 36 additions & 0 deletions src/test/java/com/search/SaddlebackSearchTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.search;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class SaddlebackSearchTest {

@Test
void testBinarySearch() {
Integer[][] arr1 = {
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20},
};

Assertions.assertArrayEquals(new int[] {1, 0}, SaddlebackSearch.find(arr1, 6), "Incorrect index");
Assertions.assertArrayEquals(new int[] {1, 4}, SaddlebackSearch.find(arr1, 10), "Incorrect index");
Assertions.assertArrayEquals(new int[] {3, 4}, SaddlebackSearch.find(arr1, 20), "Incorrect index");
Assertions.assertArrayEquals(new int[] {0, 0}, SaddlebackSearch.find(arr1, 1), "Incorrect index");
Assertions.assertArrayEquals(new int[] {-1, -1}, SaddlebackSearch.find(arr1, -10), "Incorrect index");

String[][] arr2 = {
{"A", "B", "C", "D", "E"},
{"F", "G", "H", "I", "J"},
{"K", "L", "M", "N", "O"},
};

Assertions.assertArrayEquals(new int[] {1, 0}, SaddlebackSearch.find(arr2, "F"), "Incorrect index");
Assertions.assertArrayEquals(new int[] {1, 4}, SaddlebackSearch.find(arr2, "J"), "Incorrect index");
Assertions.assertArrayEquals(new int[] {2, 4}, SaddlebackSearch.find(arr2, "O"), "Incorrect index");
Assertions.assertArrayEquals(new int[] {0, 0}, SaddlebackSearch.find(arr2, "A"), "Incorrect index");
Assertions.assertArrayEquals(new int[] {-1, -1}, SaddlebackSearch.find(arr2, "X"), "Incorrect index");
}

}