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
-
4there are nice utility classes with promising names like `Collections.binarySearch()` or `Arrays.binarySearch()` that come with every Java. – zapl Aug 07 '13 at 18:11
-
2Hi, if you get downvotes it will be because you are showing no effort, you should try to tackle the problem before posting a question. – jsedano Aug 07 '13 at 18:11
-
2That doesn't really make much sense. A List is not a data structure allowing for a random access, you can't really do a binary search without that. – piokuc Aug 07 '13 at 18:12
-
2A list is not a data structure allowing for random access? Yes it is. – Stefan Reich Nov 10 '16 at 16:54
4 Answers
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");

- 63,179
- 10
- 123
- 154
-
The binary search returns the index of the key element. If the key element is not found the index returned is (- (insertion point) – 1). For example, In the list [2, 4, 5], if we search for 3, then the search will return -2. And if we search for 4, the search will return 1. – Jayram Kumar Apr 04 '23 at 03:55
The algorithm should be the same for both an ArrayList
and a List
, given they are both ordered.

- 893
- 1
- 7
- 15
"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.

- 31,309
- 3
- 58
- 84
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.

- 9,015
- 32
- 84
- 152
-
I think he did remember that, that's why he used the word "ordered" in his question... – ajb Aug 07 '13 at 18:18
-
then what is the difference between List and ArrayList? does he mean ordered array vs. arraylist? – Saher Ahwal Aug 07 '13 at 18:21
-
1An ArrayList is one implementation of a List. A List is a sequence of elements. An ArrayList is one way to represent those elements. LinkedList is another. I think ArrayList keeps the elements in an array-like structure, so that accessing an element at any particular index is quick but some operations like inserting in the middle of the list are slower. LinkedList makes it quicker to insert in the middle but slower to get the Nth element of the list. None of those say anything about whether the elements are in sorted order. – ajb Aug 07 '13 at 18:33
-
yeah I know. List is an interface. So maybe he meant array vs arrayList – Saher Ahwal Aug 07 '13 at 18:36