Contador

Antes de tudo, sou iniciante em Java. :slight_smile:
Implementei um contador comp, neste método de ordenação, que informava quantas comparações ocorriam ao decorrer do método.

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

  int i, j, k;
  int comp = 0;
  int vet_size = 100;
  int aux;
  int[]numbers = new int[vet_size];
  
  for(i=0; i<vet_size; i++) {
   numbers[i] = (int)(1+Math.random()*vet_size);
  }
  for(j=0; j<vet_size; j++) {
    for(k=j; k<vet_size; k++) {
      if(numbers[j] > numbers[k]) {
        aux = numbers[j];
        numbers[j] = numbers[k];
        numbers[k] = aux;
      }
      comp++;
    }
  }

  System.out.println("COMPARE: "+comp);
  
  }
}

O código acima é funcional e apresenta a quantidade correta de comparações (5050) para um vetor de 100 posições.
Porém, ao criar um método exclusivo para a ordenação é que o problema surge. A ordenação em si funciona corretamente, mas não sei bem como acessar o parâmetro count do método bublle() para acessar a quantidade de comparações. E se eu puder acessar, não sei se estará correto.

public class BublleTestMethod {

  public static void main(String[] args) {
  
    int size = 100;
    int count = 0;
    int[] array = new int[size];
  
      for(int i=0; i<array.length; i++) {   
         array[i] = (int)(1+Math.random()*size);
      }
      long start = System.currentTimeMillis();
      bublle(array, count);
      long ended = System.currentTimeMillis();
      long runtime = ended - start;
      System.out.println("RUNTIME: "+runtime+" ms\n");
      System.out.println("COMPARE: "+count);
  
  }
  
  public static void bublle(int[]numbers, int comp) {
  int aux;
    for(int j=0; j<numbers.length; j++) {
       for(int k=j; k<numbers.length; k++) {
          if(numbers[j] > numbers[k]) {
            aux = numbers[j];
            numbers[j] = numbers[k];
            numbers[k] = aux;
          }
          comp++;
       }
    }
  }
}

Desde já, agradeço a ajuda.

Pq vc não retorna o comp no método bublle? Assim:

public static int bublle(int[] numbers) {
	int comp = 0;
	int aux;
	
	for(int j = 0; j < numbers.length; j++) {
		for(int k = j; k < numbers.length; k++) {
			if(numbers[j] > numbers[k]) {
				aux = numbers[j];
				numbers[j] = numbers[k];
				numbers[k] = aux;
			}
			
			comp++;
		}
	}
	
	return comp;
}

No caso, querendo-se imprimir a array ordenada, bastaria criar um laço após o método e fazê-lo percorrer o vetor já ordenado e, por fim, imprimí-lo. Mas no caso do contador comp, como expôr-lo na impressão?

public static void main(String[] args) {
	int size = 100;
	int[] array = new int[size];

	for (int i = 0; i < array.length; i++) {
		array[i] = (int) (1 + Math.random() * size);
	}
	
	long start = System.currentTimeMillis();
	int count = bublle(array);
	long ended = System.currentTimeMillis();
	long runtime = ended - start;
	
	System.out.println("RUNTIME: " + runtime + " ms\n");
	System.out.println("COMPARE: " + count);

}

public static int bublle(int[] numbers) {
	int comp = 0;
	int aux;
	
	for (int j = 0; j < numbers.length; j++) {
		for (int k = j; k < numbers.length; k++) {
			if (numbers[j] > numbers[k]) {
				aux = numbers[j];
				numbers[j] = numbers[k];
				numbers[k] = aux;
			}
			comp++;
		}
	}
	
	return comp;
}
1 curtida