public class MySearch {
public static int search(MyArray array, int value) {
int index = -1;
int start = 0, end = array.length - 1;
while(start <= end) {
int mid = (start + end) / 2;
if(array.compToValue(mid, value) == 1) end = mid - 1;
else if(array.compToValue(mid, value) == -1) start = mid + 1;
else return mid;
}
return index;
}
}
In some cases the number of comparisons is exceeded as you can see in the screenshot. I'm not allowed to use read operations (get). The number of comparisons I'm allowed to make is O(logn).
Your code says nothing about whether the array is ordered. If the array is not ordered, you can do only linear search, and a search operation in O(log n) is impossible.
If the array is ordered, as Turing85's comment says:
extract array.compToValue(mid, value) into a variable, use this variable in the if-clauses instead of calculating the value twice.
Also useful advice is to always use curly brackets. Java allows to drop it if the code block is only a single line, but doing that is a bad practice.
Related
I am trying to develop a faster way than what I currently have to add an element to a sorted array list. Currently this is my strategy
public void insertSorted(E value) {
add(value);
for (int i = size() - 1; i > 0 && value.compareTo(get(i - 1)) < 0; i--) {
this.swap(i);
}
}
and my add method...
public void add(E element) {
ensureCapacity();
array[size++] = element;
}
So I read that using a binary search algorithm I could more efficiently find the best way to put an element even faster.
I tried developing that, but somehow it always outputs me 0.
private int binarySearch(E value) {
int low = 0;
int high = this.size()-1;
while (low <= high) {
int mid = (low + high) / 2;
E midVal = this.get(mid);
int cmp = midVal.compareTo(value);
if (cmp < 0)
low = mid + 1;
else if (cmp > 0)
high = mid - 1;
else
return mid;
}
return low;
}
public void insertSorted(E value) {
int searchResult = binarySearch(value);
add(value, searchResult);
System.out.println("Value: " + value + ". Position = " + searchResult);
}
Could someone help me out? If necessary I will show full code
Rather than developing your own binary search, use built-in Arrays.binarySearch implementation. However, this wouldn't give you much improvement over your original version in terms of time.
To see why, consider the steps that you take to place the value in the sorted sequence:
Find the insertion position
Move items to the right of insertion position by one
Place the element into insertion position
The first step can be done in O(log2N). The second step takes O(N). The last step takes O(1). Overall, insertion's time complexity is O(log2N + N + 1), which is the same as O(N). The algorithm is dominated by the second step, so you might as well use linear search as you move items to the right by one.
I'm writing a program that determines how many comparisons it takes to run a binary search algorithm for a given number and sorted array. What I don't understand is what counts as a comparison.
// returns the number of comparisons it takes to find key in sorted list, array
public static int binarySearch(int key, int[] array) {
int left = 0;
int mid;
int right = array.length - 1;
int i = 0;
while (true) {
if (left > right) {
mid = -1;
break;
}
else {
mid = (left + right)/2;
if (key < array[mid]) {
i++;
right = mid - 1;
}
else if (key > array[mid]) {
i++;
left = mid + 1;
}
else {
break; // success
}
}
}
return i;
}
The function returns i, which is supposed to be the total number of comparisons made in finding the key in array. But what defines a comparison? Is it any time there is a conditional?
Thanks for any help, just trying to understand this concept.
Usually, a comparison occurs each time the key is compared to an array element. The code seems to not be counting that, though. It is counting how many times one of the search boundaries (left or right) is changed. It's not exactly the same thing being counted, but it's pretty close to the same thing, since the number of times a boundary is shifted is directly related to the number of times through the loop and hence to the number of times a comparison is made. At most, the two ways of counting will be off by 1 or 2 (I didn't bother to figure that out exactly).
Note also that if one were to use the usual definition, the code could be rewritten to use Integer.compare(int,int) do a single comparison of key with array[mid] to determine whether key was less than, equal to, or greater than array[mid].
public static int binarySearch(int key, int[] array) {
int left = 0;
int mid;
int right = array.length - 1;
int i = 0;
while (left <= right) {
mid = (left + right)/2;
int comp = Integer.compare(key, array[mid]);
i++;
if (comp < 0) {
right = mid - 1;
}
else if (comp > 0) {
left = mid + 1;
}
else {
break; // success
}
}
return i;
}
This is my current search method:
public static int search(int[] array, int numero) {
int start = 0;
int end = array.length - 1;
int center;
while (start <= end) {
center = (start + end) / 2;
if (array[center] == numero) {
return center;
} else if (array[center] < numero) {
start = center + 1;
} else {
end = center - 1;
}
}
return -1;
}
It searches from user input numero into a previously bubble sorted Array that's found in the Main method.
What I'm trying to figure out is how to print ALL of the coincidences found in the array, and not just the first one found.
I was thinking about adding results to a List and then returning that to Main, but as I tried that an endless loop happened at the first result found, causing it to add itself to the List repeatedly until the program crashes.
Assuming that you know the basic theory behind binary searches, separate it into 3 steps.
Search using binary search methods.
once a match is found, scan up from that point, until you find a non matching element.
Scan down, adding to a result list, until you find a non
matching element.
If you don't need to care about occurrence order, you could combine steps 2 and 3 and just scan up adding to the list, and scan down adding to the list, since due to the sorting, everything you hit is guaranteed to match until it doesn't.
If you do care about occurrence order, step 2 could be optimised by jumping ahead and checking, and writing a modified binary search that searches for a transition of matching/notmatching instead of a match.
This could be further optimised by keeping statistics or profiling, to find the perfect jump distance, or basing it off of the last up-most check.
actually it's easy because the list is already sorted, the numbers you expect to find are adjacent.
just like Ryan's answer, I'll put some code
public static List<Integer> searchAll (int[] array, int numero){
int firstMatchIndex = search( array, numero);
List<Integer> results = new ArrayList<Integer>():
results.add(firstMatchIndex);
boolean left = true;
while( left){
int i = firstMatchIndex - 1;
if(i<0 || array[i] != numero){
left = false;
}else{
results.add(i);
}
}
boolean right = true;
while( right){
int i = firstMatchIndex + 1;
if(i>array.length || array[i] != numero){
right = false;
}else{
results.add(i);
}
}
}
I have a program that creates a class dictionary, in which it populates and arrayList of strings with words given from a command line argument(in alphabetical order, all different lengths). Anyway, I need to implement binary search to look for a prefix in the dictionary as part of a backtracking method. I run into problems when the prefix is longer than the word in the dictionary---I tried to adjust binary search for this situation but it is producing incorrect results. I really don't understand binary search enough to fix this issue. If I don't account for the issue of a prefix being longer than a word, it .subString produces string indexoutofbounds. Any help would be greatly appreciated.
public int searchPrefix(String prefixKey){
int minIndex=0;
int maxIndex= newDictionary.size()-1;
return searchPrefix( prefixKey, minIndex,maxIndex);
}
public int searchPrefix(String prefixKey, int minIndex, int maxIndex){
if(minIndex>maxIndex){
return-1;
}
int midIndex=(maxIndex-minIndex)/2+minIndex;
if (prefixKey.length()>newDictionary.get(midIndex).length()){
return searchPrefix( prefixKey, midIndex+1,maxIndex);
}
else if(newDictionary.get(midIndex).length(<prefixKey.length()&&newDictionary.get(midIndex).compareTo(prefixKey.substring(0,newDictionary.get(midIndex).length()))>0){
return searchPrefix(prefixKey,minIndex,maxIndex);
}
else if(newDictionary.get(midIndex).substring(0,prefixKey.length()).compareTo(prefixKey)>0){
return searchPrefix(prefixKey,minIndex,maxIndex-1);
}
else if(newDictionary.get(midIndex).length()<prefixKey.length()&&newDictionary.get(midIndex).compareTo(prefixKey.substring(0,newDictionary.get(midIndex).length()))<0){
return searchPrefix(prefixKey,minIndex,maxIndex);
}
else if(newDictionary.get(midIndex).substring(0,prefixKey.length()).compareTo(prefixKey)<0){
return searchPrefix( prefixKey, midIndex+1,maxIndex);
}
else
return midIndex;
}
Have taken the binarySearch method from Collections class and modified as per your need.
private static int binarySearch(List<String> list, String key) {
int low = 0;
int high = list.size() - 1;
while (low <= high) {
int mid = (low + high) >>> 1;
String midVal = list.get(mid);
int cmp = -1;
if (midVal.length() > key.length())
cmp = midVal.substring(0, key.length()).compareTo(key);
else
cmp = key.substring(0, midVal.length()).compareTo(midVal) * -1;
if (cmp < 0)
low = mid + 1;
else if (cmp > 0)
high = mid - 1;
else
return mid; // key found
}
return -1; // key not found
}
Hope this will help you.
The String method compareTo takes care of String values of different length, so that (e.g.) "ABC" precedes "ABCD" so all of these case distinctions in your method aren't really necessary.
The cascaded of statement begins:
if (prefixKey.length() > newDictionary.get(midIndex).length()){
return searchPrefix( prefixKey, midIndex+1,maxIndex);
}
else if(newDictionary.get(midIndex).length() < prefixKey.length() && ...
But theses conditions are identical, which means that the second branch is never reached.
You have two statements:
return searchPrefix(prefixKey,minIndex,maxIndex);
Under no circumstances should a recursive call be made with exactly the same parameters as were passed to the current call: infinite recursion results.
Why can't you use Arrays.binarySearch?
I can't really suggest an improvement because you haven't described the problem. Please provide an example, giving a small dictionary and a set of keys with expected results.
My algorithm is suppose to tell me if 'x'(which has the value 5) is in the sorted array. However, I keep getting a 0. Well since my condition states that if 'x' is not in the array show 0. Where am I going wrong?
import java.util.Arrays;
public class binarySeacg {
public static void main (String[]args)
{
int[] array = {10,7,11,5,13,8};
exchangesort(array);
binsearch(array,5);
System.out.println(Arrays.toString(array));
}
public static void exchangesort(int[] S)
{
int i,j,temp;
for(i=0;i<S.length;i++)
for(j=i+1;j<S.length;j++)
if(S[i]>S[j])
{
temp = S[i];
S[i] = S[j];
S[j] = temp;
}
}
public static int binsearch(int[] S, int x)
{
int location, low, high, mid;
low = 1; high = S.length;
location = 0;
while(low<=high && location==0)
{
mid =(low + high)/2;
if(x== S[mid])
location = mid;
else if(x < S[mid])
high = mid -1;
else
low = mid + 1;
}
System.out.println(location);
return location;
}
}
You set low = 1;, and 5 is the minimal element - so it is in index 0 - so in the sublist of [1,S.length] - it is indeed not there.
You should set low = 0;, and start from the first element - not the second. (Remember that index in java starts from 0, not 1).
(PS, note that in this specific case - the algorithm is correct, since in the sorted list - 5 is in the index 0).
Here you are sorting an array and then the sorted array is used for searching the element.
And if the search is successful, then you do the below assignment
location = mid; which means you are assigning the matching element's index to the location variable.
In this case, element 5 is in 0th index.
Hence you are always getting 0 on your STDOUT
Because, you are trying to find x value, which you are passing 3 and in your list. It is not present. So, change it to other value like 5 and then try.
Also, you should start low=0 instead of low=1. Because, it will miss the first element all the time.
public static int binsearch(int[] S, int x)
{
int location, low, high, mid;
low = 0; high = S.length;
location = 0;
while(low<=high && location==0)
{
mid =(low + high)/2;
if(x == S[mid])
{
location = mid;break;
}
else if (x < S[mid])
{
high = mid - 1;
} else
{
low = mid + 1;
}
}
System.out.println(location);
return location;
}
Note : For the different output, change the value binsearch(array,5); here, which is called from main() method. Remember, change the value, which are present in your list.
in java and most languages, the index starts from 0, not 1, and ends at n-1, not n
for binary search, check carefully about when exiting the while loop, and always remember the meaning of your low and high variables, whether it is [low, high], or [low, high)
for this specific problem, u should also consider what if there r duplicates in the array. whether to return the first element or anyone in the array