1
good night.
I’m implementing a code that stores the amount of comparisons and the sorting time of binary search. So, I’m storing the data in a text file for later analysis.
It’s just that I have a problem storing the amount of times you make the comparison. The logic for saving the text file is inside Main.. And I have a static method pesquisa_bin to account for variavel que contabiliza -> vtc, I need to put this variable inside my text file, but I’m not sure how to return this method data.
Follows the code...
    package br.pesquisaBinaria;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Random;
import java.util.Scanner;
public class PesquisaBinaria {
    public static void main(String[] args) {
        int n;
        for (int cnt_tamanho = 1; cnt_tamanho <= 2; cnt_tamanho++) {
            if (cnt_tamanho == 1)
                n = 100;
            else
                n = 1000;
            int vpesq, vret, k;
            int item[] = new int[n];
        Random gerador = new Random();
        Scanner ler = new Scanner(System.in);
        System.out.println(" ");
        System.out.print("Aguarde... Gerando vetor");
        for(k=0;k<n;k++){
            item[k]=gerador.nextInt(100*2);
        }
        imprime(item);
        double tempoi = System.nanoTime();
        System.out.println(" ");
        System.out.println("\n*********ORDENADO....");
        int i, menor, j, aux;
        for(i=0;i<n;i++){
            menor = i;
            for(j=menor+1;j<n;j++){
                if(item[j] < item[menor]){
                    menor = j;
                }
            }
            if(menor != i){
                aux = item[i];
                item[i] = item[menor];
                item[menor] = aux;
            }
        }
        imprime(item);
        System.out.println("*********PESQUISA BINÁRIA....");
        System.out.println("*********Entre com o número para ser pesquisado");
        vpesq=ler.nextInt();
        vret = pesquisa_bin(item, vpesq);
        if(vret == -1){
            System.out.println("Não encontrado.");
        } else {
            System.out.println("Encontrado na posição: " + vret);
        }
        double tempof = System.nanoTime();
        double tempom = tempof - tempoi;
        tempom = tempom/1000000000.0;
        System.out.printf("Tempo gasto: %.10f \n", tempom);
        try {
            // aberto para operações de saída através do objeto arq instanciado e criado a
            // partir da classe FileWrite
            FileWriter arq = new FileWriter("PesquisaBinaria.txt", true);
            // o objeto de gravação gravaArq é associado a um fluxo de saída de dados
            // baseado
            // em caracteres através da classe PrinterWriter.
            PrintWriter gravaArq = new PrintWriter(arq, true);
            // gravaArq.write(qtvezes, ctcomp, cttroca, tempom);
            gravaArq.printf("%2d ; %2d ; %.10f ; %n", vtc, tempom, chave);
            gravaArq.close();
            arq.close();
        } catch (Exception e) {
            System.out.println("ERRO - NÃO DEU CERTO");
        }
    }
   }
    public static int pesquisa_bin(int array[], int chave){
        int esq = 0;
        int dir = array.length-1;
        int vtc = 0;
        int meio;
        System.out.println(" ");
        System.out.println("Procurando o número: " + chave);
        do{
            meio =esq + (dir - esq)/2;
            vtc++;
            if(chave < array[meio]) {
                dir=meio-1;
            }
            vtc++;    
           if(chave > array[meio])
               esq=meio+1;
            else 
                return meio;
         }while(esq<=dir);
        System.out.println("Não encontramos este número e executamos" + vtc + " interações para isso");
        // PRECISO RETORNAR ESSE VTC E COLOCAR DENTRO DO gravaArq.write(..)
         return -1;
    }
    public static void imprime(int array[]){
        System.out.println(" ");
        for(int i=0; i<array.length;i++){
            System.out.print(array[i] + " ");
        }
        System.out.print(" ");
    }
}
In fact, this value of 49 is wrong, it could not return 49, do not know where he is taking this number!. I debugged by decreasing the vector, and I would have to have a return of 3 and is returning this 49.
– Luiz Gustavo
I found, @rnd_rss, helped me a lot!! Inside the
pesquisa_binhad a different return of vtc.... Thank you!– Luiz Gustavo