Eu estou tendo um problema. Quero fazer uma função que organiza os valores de uma memória array em ordem crescente. o programa funciona mas sempre tem um erro nas ultimas casas do array. Alguém ajuda?!
public static void ordemCrescente(){
long y[] = new long[x.length];
long menor;
int anulador;
System.arraycopy(x, 0, y, 0, x.length);
for (int i=0; i<x.length;i++){
menor = Practica.maior()+1;This text will be hidden
anulador=0;
for (int j=0;j<y.length;j++){
if (y[j]< menor && y[j]!=0){
anulador = j;
menor = y [j];
}
}
y[anulador] = 0;
if (i==(x.length)){
x[i]=Practica.maior();
}
else{
x[i]=menor;
}
}
}
long x [] = new long []{20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0};
long g;
int q=0;
for(int j=0;j<x.length/2;j++){
q++;
System.out.println("Quantidade: "+q);
for(int i=j;i<x.length-j;i++){
if (i!=x.length-1){
if (x[i] > x[i+1]){
g = x[i+1];
x[i+1] = x[i];
x[i] = g;
}
}
if (i!=0){
if (x[x.length-i] < x[x.length-i-1]){
g = x[x.length-i];
x[x.length-i] = x[x.length-i-1];
x[x.length-i-1] = g;
}
}
System.out.println(x[0]+" "+x[1]+" "+x[2]+" "+x[3]+" "+x[4]+" "+x[5]+" "+x[6]+" "+x[7]+" "+x[8]+" "+" "+x[9]+" "+
x[10]+" "+x[11]+" "+x[12]+" "+x[13]+" "+x[14]+" "+x[15]+" "+x[16]+" "+x[17]+" "+x[18]+" "+x[19]+" "+x[20]);
}
}
System.out.println("Quantidade: "+q);
for(int i=0;i<x.length;i++){
System.out.println(x[i]);
}
Podemos diminuir a busca fazendo duas verificações no mesmo loop, ou seja, olho as duas pontas do meu array e vou trazendo o maior e o menor número ao mesmo tempo.