Ordem crescente e decrescente
28/08/2012
0
package Lista_P2; import javax.swing.JOptionPane; public class exercicio11 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int numero1; int numero2; int numero3; int numero4; int auxiliar; numero1 = Integer.parseInt(JOptionPane.showInputDialog("Digite o numero 1")); numero2 = Integer.parseInt(JOptionPane.showInputDialog("Digite o numero 2")); numero3 = Integer.parseInt(JOptionPane.showInputDialog("Digite o numero 3")); numero4 = Integer.parseInt(JOptionPane.showInputDialog("Digite o numero 4")); if (numero1 > numero2) { auxiliar = numero1; numero1 = numero2; numero2 = auxiliar; } if (numero2 > numero3) { auxiliar = numero2; numero2 = numero3; numero3 = auxiliar; } if (numero1 > numero2) { auxiliar = numero1; numero1 = numero2; numero2 = auxiliar; } JOptionPane.showMessageDialog(null, "Ordem crescente: " + numero1 + " " + numero2 + " " + numero3); JOptionPane.showMessageDialog(null, "Ordem decescente: " +numero4 +" "+ numero3 + " " + numero2 + " " + numero1); } }
Ismael Silva
Post mais votado
08/07/2016
import javax.swing.JOptionPane ; public class Exercicio_3 { public static void main( String[ ] args ) { int[ ] numeros = new int[ ] { Integer.parseInt( JOptionPane.showInputDialog( "Digite o primeiro número" ) ), Integer.parseInt( JOptionPane.showInputDialog( "Digite o segundo número" ) ), Integer.parseInt( JOptionPane.showInputDialog( "Digite o terceiro número" ) ), } ; JOptionPane.showMessageDialog( null, "Digitados:\n" + numeros[ 0 ] + "\n" + numeros[ 1 ] + "\n" + numeros[ 2 ] ) ; ordenacaoCrescente( numeros ) ; JOptionPane.showMessageDialog( null, "Ordenação crescente:\n" + numeros[ 0 ] + "\n" + numeros[ 1 ] + "\n" + numeros[ 2 ] ) ; ordenacaoDecrescente( numeros ) ; JOptionPane.showMessageDialog( null, "Ordenação decrescente:\n" + numeros[ 0 ] + "\n" + numeros[ 1 ] + "\n" + numeros[ 2 ] ) ; } static void ordenacaoCrescente( int[ ] numeros ) { int quantidade = numeros.length ; int primeiro = 0 ; int segundo = 1 ; while ( segundo < quantidade ) { // na ordenação crescente troca os valores quando o primeiro é maior que o segundo if ( numeros[ primeiro ] > numeros[ segundo ] ) { int troca = numeros[ primeiro ] ; numeros[ primeiro ] = numeros[ segundo ] ; numeros[ segundo ] = troca ; primeiro = 0 ; segundo = 1 ; } else { primeiro++ ; segundo++ ; } } } static void ordenacaoDecrescente( int[ ] numeros ) { int quantidade = numeros.length ; int primeiro = 0 ; int segundo = 1 ; while ( segundo < quantidade ) { // na ordenação decrescente troca os valores quando o primeiro é menor que o segundo if ( numeros[ primeiro ] < numeros[ segundo ] ) { int troca = numeros[ primeiro ] ; numeros[ primeiro ] = numeros[ segundo ] ; numeros[ segundo ] = troca ; primeiro = 0 ; segundo = 1 ; } else { primeiro++ ; segundo++ ; } } } }
Merilyn Ribeiro
Mais Posts
28/08/2012
Davi Costa
public class bubbleSort { public static void main(String a[]) { int i; int array[] = { 12, 9, 4, 99, 120, 1, 3, 10 }; System.out.println("Values Before the sort:\n"); for (i = 0; i < array.length; i++) System.out.print(array[i] + " "); System.out.println(); bubble_srt(array, array.length); System.out.print("Values after the sort:\n"); for (i = 0; i < array.length; i++) System.out.print(array[i] + " "); System.out.println(); System.out.println("PAUSE"); } public static void bubble_srt(int a[], int n) { int i, j, t = 0; for (i = 0; i < n; i++) { for (j = 1; j < (n - i); j++) { if (a[j - 1] < a[j]) { t = a[j - 1]; a[j - 1] = a[j]; a[j] = t; } } } } }
28/08/2012
Ismael Silva
public class bubbleSort { public static void main(String a[]) { int i; int array[] = { 12, 9, 4, 99, 120, 1, 3, 10 }; System.out.println("Values Before the sort:\n"); for (i = 0; i < array.length; i++) System.out.print(array[i] + " "); System.out.println(); bubble_srt(array, array.length); System.out.print("Values after the sort:\n"); for (i = 0; i < array.length; i++) System.out.print(array[i] + " "); System.out.println(); System.out.println("PAUSE"); } public static void bubble_srt(int a[], int n) { int i, j, t = 0; for (i = 0; i < n; i++) { for (j = 1; j < (n - i); j++) { if (a[j - 1] < a[j]) { t = a[j - 1]; a[j - 1] = a[j]; a[j] = t; } } } } }
28/08/2012
Davi Costa
28/08/2012
Ismael Silva
19/03/2013
Davi Costa
19/03/2013
Marcelo Senaga