Note that Java SE API provides a simple way of searching.
Arrays.binarySearch( ... )
Collections.binarySearch( ... )
check: Java SE API - Oracle Java Platform SE 6.0
- java.util.Collections
- java.util.Arrays
But thinking deeper of what is inside the method helps us to understand more about searching and to implement with other programming languages.
There are two common approaches:
1 Linear Search - go and compare with each item in a linear way
2 Binary Search - go and compare with the middle item of each iteration
Example:
searching through a given group of integers with 10 elements:
int a[] = { 7, 5, 8, 2, 9, 0, 3, 6, 1, 4 };
we need to find out if this group has 20. if yes, show the index of 20, otherwise the index of 20 set to -1.
(0 is the beginning index of an array, so -1 means it is not in the array)
So how do we perform the search?
We aim to minimize the comparisons to find the target data fast.
Let's compare 2 basic search algorithms.
1 Linear Search
The linear search needs to go through each item, unless we are lucky that the first item is our target.
Here, it is obvious that there is no 20 in the integer array.
So, we need to perform 10 comparisons.
Code:
- int a[] = { 7, 5, 8, 2, 9, 0, 3, 6, 1, 4 };
- int target = 20;
- //try to change target to any value in the array
- int found=-1;
- for(int i=0;i<a.length;i++){
- if(a[i]==target){
- found=i;
- }
- }
- System.out.println(found);
2 The Binary Search
The binary search needs roughly 3-4 comparisons to find 20
Well, first we need to sort the given array, whether in ascending or descending order, it is up to you.
int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Since we know the order is ascending, let us begin the begin the Binary Search.
Steps:
- find the element in the middle which is 4, 4 is less than 20, now we know anything on the left side of 4 does not have 20, so we move on.
- find the element in middle of the right hand side of 4 which is 7, 7 is less than 20, we move on.
- comparing 20 with 8 ...
- comparing 20 with 9 ... End of story!!!
- int a[] = { 0, 1, 2, 3, 4, 5, 6,7,8,9 };
- int target = 20;
- //try to change target to any value in the array
- boolean isFound = false;
- int len = a.length;
- int begin = 0;
- int end = len - 1;
- int mid = (end - begin) / 2;
- while (!isFound&&begin<=end) {
- if (a[mid] == target) {
- isFound = true;
- break;
- } else if (a[mid] < target) {
- begin = mid+1;
- } else {
- end = mid-1;
- }
- mid = (end + begin) / 2;
- }
- System.out.println(isFound+" - "+mid);
Linear Search or Binary Search?
Linear Search does not require pre-sort the elements, and it is easy to code, maintain and debug. The time complexity of Linear Search is O(n).
Binary Search requires pre-sort the elements, but it saves time in finding the target. The line of code are longer, so it takes time to code, maintain and debug. The time complexity of Binary Search is O(log2n)
Some people may think it doesn't matter which algorithm we perform here. Yes.
But if we are searching an element within a group with 1,000 or even 1,000,000 elements.
Binary Search helps to save heaps of time in such a case.
To Search Other Data Types:
For String, the String class implements Comparable interface, when we compare 2 Strings we use compareTo method.
For Object, the class must implement the Comparable interface, and we need to override the compareTo method.
Other references:
Algorithm - Wikipedia
Analysis of algorithms - Wikipedia
Time complexity - Wikipedia
Binary search algorithm - Wikipedia
No comments:
Post a Comment