Java
In java, Collections/Arrays class contains binary search.
- Collections.binarySearch(list, key);
- Arrays.binarySearch(array, key);
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Function returns location of unique item in a sorted list. | |
* | |
* @param <T> Class, that implements comparable interface. | |
* @param data list of elements | |
* @param searchValue element to search for | |
* @return Location of the the element. If item is not found, -1 is returned. | |
* @throws java.lang.NullPointerException | |
* If any checked element in the list is 'null'. | |
*/ | |
public static <T extends Comparable<? super T>> // | |
int binarySearch(final List<T> data, T searchValue) | |
throws java.lang.NullPointerException { | |
return binarySearch(data, searchValue, 0, data.size() - 1); | |
} | |
/** | |
* Function returns location of unique item in a sorted list. | |
* | |
* @param <T> Class, that implements comparable interface. | |
* @param data list of elements | |
* @param searchValue element to search for | |
* @param left lower bound | |
* @param right upper bound | |
* @return Location of the the element. If item is not found, -1 is returned. | |
* @throws java.lang.NullPointerException | |
* If any checked element in the list is 'null'. | |
*/ | |
public static <T extends Comparable<? super T>> // | |
int binarySearch(final List<T> data, T searchValue, int left, int right) | |
throws java.lang.NullPointerException { | |
final int NOT_FOUND = -1; | |
if (right < left) | |
return NOT_FOUND; | |
int mid = (left + right) >>> 1; | |
int cmp = searchValue.compareTo(data.get(mid)); | |
if (cmp > 0) | |
return binarySearch(data, searchValue, mid + 1, right); | |
if (cmp < 0) | |
return binarySearch(data, searchValue, left, mid - 1); | |
return mid; | |
} |
C#
In C#, list itself and Arrays class contains binary search.
list.BinarySearch(key); Array.BinarySearch(array, key);My naive Java implementation translated to C#.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Function returns location of unique item in a sorted list. | |
/// </summary> | |
/// <typeparam name="T">Class, that implements comparable interface.</typeparam> | |
/// <param name="data">list of elements</param> | |
/// <param name="searchValue">element to search for</param> | |
/// <returns>Location of the the element. If item is not found, -1 is returned.</returns> | |
/// <exception cref="System.NullReferenceException"> | |
/// If any checked element in the list is 'null'. (Other then first one) | |
/// </exception> | |
public static int binarySearch<T>(IList<T> data, T searchValue) | |
where T : IComparable<T> | |
{ | |
return binarySearch(data, searchValue, 0, data.Count()); | |
} | |
/// <summary> | |
/// Function returns location of unique item in a sorted list. | |
/// </summary> | |
/// <typeparam name="T">Class, that implements comparable interface.</typeparam> | |
/// <param name="data">list of elements</param> | |
/// <param name="searchValue">element to search for</param> | |
/// <param name="left">lower bound</param> | |
/// <param name="right">upper bound</param> | |
/// <returns>Location of the the element. If item is not found, -1 is returned.</returns> | |
/// <exception cref="System.NullReferenceException"> | |
/// If any checked element in the list is 'null'. (Other then first one) | |
/// </exception> | |
public static int binarySearch<T>(IList<T> data, T searchValue, int left, int right) | |
where T : IComparable<T> | |
{ | |
const int NOT_FOUND = -1; | |
if (right < left) | |
return NOT_FOUND; | |
int mid = (int)((uint)(left + right) >> 1); | |
int cmp = searchValue.CompareTo(data[mid]); | |
if (cmp > 0) | |
return binarySearch(data, searchValue, mid + 1, right); | |
if (cmp < 0) | |
return binarySearch(data, searchValue, left, mid - 1); | |
return mid; | |
} |
Mathematica
In mathematica, Combinatorica library contains binary search.
BinarySearch[list, key]
No comments:
Post a Comment