Pessoal eu to fazendo um tranpo que tem que pegar um vetor de qualquer tamanho achar o maior numero dele e colocar dentro do vetor e no meio. Ate tudo bem. Depois tem que ordenar crencente ate o maior e decrecente o resto. Quando faço o decrecente da um erro tosco. Ele imprime um “zero” inexistente dentro do vetor e no ultimo espaço.
O codigo da classe é essa:
public class DesafioxXx {
public static void main(String args[]){
int v[] = {12,5,100,8,462,198,25};
System.out.println("Exercicio corrigido do bubbleSort.\n");
for(int i=0; i<v.length; i++)
{System.out.print("-"+v[i]);}
System.out.println("O maior valor encontrado dentro do vetor e:");
M_V(v);
System.out.println();
System.exit(0);
}
public static void M_V(int a[]){
int maior = 0;
int pos = 0;
for(int j=0; j<a.length; j++){
if(a[j]>maior){
maior = a[j];
pos = j;
}
}
System.out.println(" ** "+maior+" ** "+"\n\n");
M_M_V(pos,a,maior);
}
public static void M_M_V(int p,int b[],int m){
int mm = m;
if(p!=3){
int tmp = b[3];
b[3] = b[p];
b[p]= tmp;
}
for(int x=0; x<b.length; x++)
{System.out.print("-"+b[x]);}
Div_Vetor(p,b,mm);
}
public static void Div_Vetor(int p, int c[], int m){
int d[] = new int[3];
int e[] = new int[3];
for(int u=0; u<d.length; u++)
{ e[u]=c[u];}
for(int y=0; y<2; y++)
{ d[y]=c[y+5];}
bubblesort(e);
bubblesort1(d);
System.out.println("\nOs novos numeros arrumados");
for(int h=0; h<e.length;h++)
{System.out.print(e[h]+"-");}
System.out.print(m+"-");
for(int g=0; g<d.length; g++)
{System.out.print(d[g]+"-");}
}
public static void bubblesort(int re[]){
for(int pass=1; pass<re.length; pass++)
for(int ps=0; ps<re.length-1;ps++)
if(re[ps]>re[ps+1])
troca(re,ps,ps+1);
}
public static void bubblesort1(int re[]){
for(int pass=1; pass<re.length; pass++)
for(int ps=0; ps<re.length-1;ps++)
if(re[ps]<re[ps+1])
troca(re,ps,ps+1);
}
public static void troca(int d[], int p, int s){
int aux = d[p];
d[p]=d[s];
d[s]=aux;
}
}
As partes importantes são:
public static void Div_Vetor(int p, int c[], int m){
int d[] = new int[3];
int e[] = new int[3];
for(int u=0; u<d.length; u++)
{ e[u]=c[u];}
for(int y=0; y<2; y++)
{ d[y]=c[y+5];}
bubblesort(e);
bubblesort1(d);
System.out.println("\nOs novos numeros arrumados");
for(int h=0; h<e.length;h++)
{System.out.print(e[h]+"-");}
System.out.print(m+"-");
for(int g=0; g<d.length; g++)
{System.out.print(d[g]+"-");}
}
public static void bubblesort1(int re[]){
for(int pass=1; pass<re.length; pass++)
for(int ps=0; ps<re.length-1;ps++)
if(re[ps]<re[ps+1])
troca(re,ps,ps+1);
}
public static void troca(int d[], int p, int s){
int aux = d[p];
d[p]=d[s];
d[s]=aux;
}
Não consegui achar o erro lógico disso… Alguem poderia como contar isso… Ja tentei de algumas formas… Grato.