Runtime recursive function

Asked

Viewed 52 times

-2

How I calculate the running time of a recursive function?

 public void ordenar(int vetor[],int i,int f){
    boolean aux2=true;
    int inicio=i,fim=f;
    while(i<f){
        if(vetor[i]>vetor[f]){
            int aux=vetor[i];
            vetor[i]=vetor[f];
            vetor[f]=aux;
            aux2=!aux2;
        }
        if(aux2)
            f--;
        else
            i++;
    }
     for(int x=0;x<vetor.length;x++){
        System.out.print(vetor[x]+" ");
    }
    if(i>inicio)
        ordenar(vetor,inicio,i-1);
    if(i<fim)
        ordenar(vetor,i+1,fim);
}
  • 1

    For the record, use System.currentTimeMillis is not the best way to measure the running time/performance of something (read here to better understand). Prefer to use specialized tools - I usually use the JMH, but the link already indicated has other options

1 answer

1


In this case it would be more interesting for you to make the start and end measurements before and after the recursive method call. Create a method to perform this measurement.

Ex:

public void Exemplo(){
   Exemplo();
}

public long calculaTempo(){
    long comeco=System.currentTimeMillis();
    Exemplo();
    long fim=System.currentTimeMillis();

    return fim - comeco;
}

OBS> Consider the method Example as a recursive method

Browser other questions tagged

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