Difficulties with binarySearch Class Arrays method

Asked

Viewed 915 times

5

I created two arrays one of integers and the other of Strings(objects), but when I used the binarySearch method to know the position of the elements, the return of the positions of the Strings array came out different, because it happened?

showing code and return.

import java.util.Arrays;
public class ArraySimples {
    public static void main(String[] args) {
        String [] paises ={"Brasil", "Russia", "India", "China", "Argentina","Paraguai"};
        int [] numeros = {5,7,9,11,13};
        int posicao0 =Arrays.binarySearch(paises, "Brasil");
        int posicao1 =Arrays.binarySearch(paises, "Russia");
        int posicao2 =Arrays.binarySearch(paises, "India");
        int posicao3 =Arrays.binarySearch(paises, " China");
        int posicao4 =Arrays.binarySearch(paises, "Argentina");
        int posicao5 =Arrays.binarySearch(paises, "Paraguai");

        System.out.println("Brasil: " + posicao0);
        System.out.println("Russia: " + posicao1);
        System.out.println("India: " + posicao2);
        System.out.println("China: " + posicao3);
        System.out.println("Argentina:" + posicao4);
        System.out.println("Paraguai" + posicao5);

        int posicao00 =Arrays.binarySearch(numeros, 5);
        int posicao11 =Arrays.binarySearch(numeros, 7);
        int posicao22 =Arrays.binarySearch(numeros, 9);
        int posicao33 =Arrays.binarySearch(numeros, 11);
        int posicao44 =Arrays.binarySearch(numeros, 13);
        System.out.println("5: " + posicao00);
        System.out.println("7: " + posicao11);
        System.out.println("9: " + posicao22);
        System.out.println("11: " + posicao33);
        System.out.println("13: " +posicao44);
    }
}

Esse foi o resulta, porque apareceram posições negativas?

1 answer

7


To use binarySearch, before you need to have an ordered Array.

If you do this, it already changes the result (but read until the end):

java.util.Arrays.sort();

See this excerpt from java documentation:

The array must be Sorted into ascending order According to the natural Ordering of its Elements (as by the Sort(Object[]) method) prior to making this call. If it is not Sorted, the Results are Undefined.

Translating:

The array needs to be arranged in ascending order according to the natural order of its elements (as with the Sort(Object[]) method before making this call. If not organized, the results are undefined.

Realize that as the numbers of your test were already ordered at source, the result was expected. Countries, in turn, are not in natural order. If they were in order too, we probably wouldn’t notice a potential problem.

The solution, applied to your code, is simple... but read carefully what comes next.

import java.util.Arrays;

public class ArraySimples {
   public static void main(String[] args) {

   String [] paises ={"Brasil", "Russia", "India", "China", "Argentina", "Paraguai"};

      Arrays.sort(paises);

      int posicao0 = Arrays.binarySearch(paises, "Brasil");
      int posicao1 = Arrays.binarySearch(paises, "Russia");
      int posicao2 = Arrays.binarySearch(paises, "India");
      int posicao3 = Arrays.binarySearch(paises, "China");
      int posicao4 = Arrays.binarySearch(paises, "Argentina");
      int posicao5 = Arrays.binarySearch(paises, "Paraguai");

      System.out.println("Brasil: " + posicao0);
      System.out.println("Russia: " + posicao1);
      System.out.println("India: " + posicao2);
      System.out.println("China: " + posicao3);
      System.out.println("Argentina: " + posicao4);
      System.out.println("Paraguai: " + posicao5);
   }
}


Working is different from being right :D

Binary search only matters when there are several. Much more searches and at different times than what is in the question, as a whole list of countries that will be consulted numerous times in the code.

Otherwise, it’s much better to loop for.

To better understand, let’s use a more practical example:

You’ve got 600 books, mixed up, and you want just one, "The Process" by Kafka.

The sort + binarySearch is you first organize ALL 600 in alphabetical order... and only after you finish ordering, start searching. Take the middle one, see if it’s what you want, if it’s not split the pile in two, look for one side, and so on, until you get to the desired one (and by ordering the book already passed several times in your hand and you did not take). You realize this alternative is not very smart?

Much better to just look one by one in the sequence, and when you find what you want, you take it and leave. End of story. That is, in the case of only one or two searches, the only way out is to forget this binary search business and use a simple loop.

  • Thank you Bacco, you helped a lot :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.