Olá,
Eu gostaria que alguém me ajudasse a analisar um algoritmo em Java. Vejamos:
import java.util.Arrays;
public class Sort {
public static void main(String args[])
{
int[] a = {10,12,1,5,30};
int i, j = 0;
for (j = 2; j < a.length; j++)
{
int key = a[j];
System.out.println("key: " + key);
System.out.println("j: " + j);
i = j - 1;
System.out.println("i = j - 1: " + i);
System.out.println(i);
while(i >= 0 && a[i] > key) {
a[i + 1] = a[i];
i = i - 1;
}
a[i + 1] = key;
}
for (j = 0; j < a.length; j++) {
System.out.println(Arrays.toString(a));
}
}
}
Porque entra no loop (while), sendo que a[i] não é maior que key ?
Agradeço!