问题
Im looking for a way to implement a code in java that works the same way as a binary search in an ordered ArrayList but for an ordered List Thanks
回答1:
The algorithm should be the same for both an ArrayList
and a List
, given they are both ordered.
回答2:
You can use
Collections.<T>binarySearch(List<T> list, T key)
for binary search on any List
. It works on ArrayList
and on LinkedList
and on any other List
.
However:
binary search is only fast if you have direct access to each element:
This method runs in log(n) time for a "random access" list (which provides near-constant-time positional access). If the specified list does not implement the RandomAccess interface and is large, this method will do an iterator-based binary search that performs O(n) link traversals and O(log n) element comparisons.
If your List
does not provide "random access" you might have better luck by creating a copy of that List
that does provide this.
LinkedList<String> list = new LinkedList<String>();
// fill
Either like so
ArrayList<String> fastList = new ArrayList<String>(list);
Collections.binarySearch(fastList, "Hello World");
or maybe like so
String[] array = list.toArray(new String[list.size()]);
Arrays.binarySearch(array, "Hello World");
If your List
is not ordered by default and you have to sort it prior to searching you might get the best result by doing it with arrays since
Collections.sort(list);
creates a temporary array that is sorted and used to re-create the list which you should be able to prevent if you do it with arrays directly.
String[] array = list.toArray(new String[list.size()]);
Arrays.sort(array);
Arrays.binarySearch(array, "Hello World");
回答3:
"Binary search" only makes sense if the elements of the list (or some kind of pointers to the elements) are organized sequentially in memory, so that if know that your search has been narrowed down to indexes Low and High, you can jump right to the element at (Low + High) / 2 without having to slog through all the other elements. This isn't going to work for a general List, which could be a LinkedList. For something like that, you can't really do better than starting at the front of the list and going through all the elements in order.
回答4:
You should also remember that you cannot do binary search if the list is not ordered. It doesn't make sense. Binary search is O(log n)
because you can at every point disregard half of the list since you have knowledge that it is ordered. If the list is not ordered, then your search is O(n) and you cannot use binary.
your own implementation for binary search should look like this:
int binary_search(int A[], int key, int imin, int imax)
{
// test if array is empty
if (imax < imin)
// set is empty, so return value showing not found
return KEY_NOT_FOUND;
else
{
// calculate midpoint to cut set in half
int imid = midpoint(imin, imax);
// three-way comparison
if (A[imid] > key)
// key is in lower subset
return binary_search(A, key, imin, imid-1);
else if (A[imid] < key)
// key is in upper subset
return binary_search(A, key, imid+1, imax);
else
// key has been found
return imid;
}
}
source: Wikipedia
If you want to use Java.util implementation then use:
java.util.Arrays.binarySearch(int[] a, int key)
Array a
should be sorted of course.
来源:https://stackoverflow.com/questions/18110619/binary-search-in-an-ordered-list-in-java