Hola escribo para ver si alguien me puede ayudar quiero esceribir un programa en Java que reciba varios enteros desde la entrada estándar, los introduzca en un arreglo y se lo pase a otro método que los ordene. Debo mostrar el arreglo ordenado.
El algoritmo de ordenación será el siguiente:
Mientras el arreglo no esté ordenado completamente hacer:
Sea i una variable entera, sea n el tamaño del arreglo
Hacer desde i = 0 hasta i = n
Comparar ei > e i +1 entonces intercambiarlos.
Alguien me podría ayudar con el código.
Gracias!
Programa Java
Moderador: Moderadores
Re: Programa Java
Hola!!
Mira para poder ordenar tu vector puedes utilizar el Mètodo de la Burbuja, es sencillo y muy rapido, Utilizas dos Ciclos For..y dos variables de control (yo siempre utilizo i y j)
Si quieres puedes escribirme para que te mande el còdigo que tengo, ya que yo ya hice un programa parecido.
Mi mail es kendra_unveiled@hotmail.com
Mira para poder ordenar tu vector puedes utilizar el Mètodo de la Burbuja, es sencillo y muy rapido, Utilizas dos Ciclos For..y dos variables de control (yo siempre utilizo i y j)
Si quieres puedes escribirme para que te mande el còdigo que tengo, ya que yo ya hice un programa parecido.
Mi mail es kendra_unveiled@hotmail.com
Re: Programa Java
hace mucho no implemento esa aplicacion pero se que te funcionaria la funcion del COMPARE TO, es un ordenamineto de un arreglo y lo parte a la mitad es busqueda rapida. QUITE SORT cre oque se llamathon escribió:Hola escribo para ver si alguien me puede ayudar quiero esceribir un programa en Java que reciba varios enteros desde la entrada estándar, los introduzca en un arreglo y se lo pase a otro método que los ordene. Debo mostrar el arreglo ordenado.
El algoritmo de ordenación será el siguiente:
Mientras el arreglo no esté ordenado completamente hacer:
Sea i una variable entera, sea n el tamaño del arreglo
Hacer desde i = 0 hasta i = n
Comparar ei > e i +1 entonces intercambiarlos.
Alguien me podría ayudar con el código.
Gracias!
Re: Programa Java
mira..no se si este completamente bien, pero usando el metodo de la burbuja es bien facil....ordenas tus numeros y puedes poner el tamaño del vector aleatoriamente. Saludos Kendra!..enexo la clase Leer (tienes que instanciarla en el programa)
public class burbuja
{
public static void main (String[] args)
{
int hasta = 0 ;
int [] arreglo ;
int aux = 0;
System.out.print("Introduzca el numero de datos a cargar =>");
hasta = Leer.datoint();
arreglo = new int[hasta];
for (int i=0;i<=hasta-1;i++)
{
arreglo = Leer.datoint();
}
System.out.println("Los datos ordenados son:");
for (int i2=0;i2<=hasta-2;i2++)
{
for (int i=0 ;i<=hasta-2;i++)
{
if (arreglo>arreglo[i+1])
{
aux=arreglo[i+1];
arreglo[i+1]=arreglo;
arreglo=aux;
}
}
}
for (int i=0 ;i<=hasta-1;i++)
{
System.out.println(arreglo);
}
}
}
-************leer dato**********
import java.io.*;
public class Leer
{
public static String dato()
{
String sdato = " ";
try
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader FlujoE = new BufferedReader(isr);
sdato=FlujoE.readLine();
}
catch(IOException e)
{
System.err.println("Errors:" + e.getMessage());
}
return sdato;
}
public static short datoShort()
{
try
{
return Short.parseShort (dato());
}
catch (NumberFormatException e)
{
return Short. MIN_VALUE;
}
}
public static int datoint()
{
try
{
return Integer.parseInt (dato());
}
catch (NumberFormatException e)
{
return Integer. MIN_VALUE;
}
}
public static double datoDouble()
{
try
{
return Double.parseDouble (dato());
}
catch (NumberFormatException e)
{
return Double. NaN;
}
}
public static float datoFloat()
{
try
{
return Float.parseFloat (dato());
}
catch (NumberFormatException e)
{
return Float. NaN;
}
}
public static long datoLong()
{
try
{
return Long.parseLong (dato());
}
catch (NumberFormatException e)
{
return Long.MIN_VALUE;
}
}
}
public class burbuja
{
public static void main (String[] args)
{
int hasta = 0 ;
int [] arreglo ;
int aux = 0;
System.out.print("Introduzca el numero de datos a cargar =>");
hasta = Leer.datoint();
arreglo = new int[hasta];
for (int i=0;i<=hasta-1;i++)
{
arreglo = Leer.datoint();
}
System.out.println("Los datos ordenados son:");
for (int i2=0;i2<=hasta-2;i2++)
{
for (int i=0 ;i<=hasta-2;i++)
{
if (arreglo>arreglo[i+1])
{
aux=arreglo[i+1];
arreglo[i+1]=arreglo;
arreglo=aux;
}
}
}
for (int i=0 ;i<=hasta-1;i++)
{
System.out.println(arreglo);
}
}
}
-************leer dato**********
import java.io.*;
public class Leer
{
public static String dato()
{
String sdato = " ";
try
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader FlujoE = new BufferedReader(isr);
sdato=FlujoE.readLine();
}
catch(IOException e)
{
System.err.println("Errors:" + e.getMessage());
}
return sdato;
}
public static short datoShort()
{
try
{
return Short.parseShort (dato());
}
catch (NumberFormatException e)
{
return Short. MIN_VALUE;
}
}
public static int datoint()
{
try
{
return Integer.parseInt (dato());
}
catch (NumberFormatException e)
{
return Integer. MIN_VALUE;
}
}
public static double datoDouble()
{
try
{
return Double.parseDouble (dato());
}
catch (NumberFormatException e)
{
return Double. NaN;
}
}
public static float datoFloat()
{
try
{
return Float.parseFloat (dato());
}
catch (NumberFormatException e)
{
return Float. NaN;
}
}
public static long datoLong()
{
try
{
return Long.parseLong (dato());
}
catch (NumberFormatException e)
{
return Long.MIN_VALUE;
}
}
}
Re: Programa Java
mira..no se si este completamente bien, pero usando el metodo de la burbuja es bien facil....ordenas tus numeros y puedes poner el tamaño del vector aleatoriamente. Saludos Kendra!..enexo la clase Leer (tienes que instanciarla en el programa)
public class burbuja
{
public static void main (String[] args)
{
int hasta = 0 ;
int [] arreglo ;
int aux = 0;
System.out.print("Introduzca el numero de datos a cargar =>");
hasta = Leer.datoint();
arreglo = new int[hasta];
for (int i=0;i<=hasta-1;i++)
{
arreglo = Leer.datoint();
}
System.out.println("Los datos ordenados son:");
for (int i2=0;i2<=hasta-2;i2++)
{
for (int i=0 ;i<=hasta-2;i++)
{
if (arreglo>arreglo[i+1])
{
aux=arreglo[i+1];
arreglo[i+1]=arreglo;
arreglo=aux;
}
}
}
for (int i=0 ;i<=hasta-1;i++)
{
System.out.println(arreglo);
}
}
}
-************leer dato**********
import java.io.*;
public class Leer
{
public static String dato()
{
String sdato = " ";
try
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader FlujoE = new BufferedReader(isr);
sdato=FlujoE.readLine();
}
catch(IOException e)
{
System.err.println("Errors:" + e.getMessage());
}
return sdato;
}
public static short datoShort()
{
try
{
return Short.parseShort (dato());
}
catch (NumberFormatException e)
{
return Short. MIN_VALUE;
}
}
public static int datoint()
{
try
{
return Integer.parseInt (dato());
}
catch (NumberFormatException e)
{
return Integer. MIN_VALUE;
}
}
public static double datoDouble()
{
try
{
return Double.parseDouble (dato());
}
catch (NumberFormatException e)
{
return Double. NaN;
}
}
public static float datoFloat()
{
try
{
return Float.parseFloat (dato());
}
catch (NumberFormatException e)
{
return Float. NaN;
}
}
public static long datoLong()
{
try
{
return Long.parseLong (dato());
}
catch (NumberFormatException e)
{
return Long.MIN_VALUE;
}
}
}
public class burbuja
{
public static void main (String[] args)
{
int hasta = 0 ;
int [] arreglo ;
int aux = 0;
System.out.print("Introduzca el numero de datos a cargar =>");
hasta = Leer.datoint();
arreglo = new int[hasta];
for (int i=0;i<=hasta-1;i++)
{
arreglo = Leer.datoint();
}
System.out.println("Los datos ordenados son:");
for (int i2=0;i2<=hasta-2;i2++)
{
for (int i=0 ;i<=hasta-2;i++)
{
if (arreglo>arreglo[i+1])
{
aux=arreglo[i+1];
arreglo[i+1]=arreglo;
arreglo=aux;
}
}
}
for (int i=0 ;i<=hasta-1;i++)
{
System.out.println(arreglo);
}
}
}
-************leer dato**********
import java.io.*;
public class Leer
{
public static String dato()
{
String sdato = " ";
try
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader FlujoE = new BufferedReader(isr);
sdato=FlujoE.readLine();
}
catch(IOException e)
{
System.err.println("Errors:" + e.getMessage());
}
return sdato;
}
public static short datoShort()
{
try
{
return Short.parseShort (dato());
}
catch (NumberFormatException e)
{
return Short. MIN_VALUE;
}
}
public static int datoint()
{
try
{
return Integer.parseInt (dato());
}
catch (NumberFormatException e)
{
return Integer. MIN_VALUE;
}
}
public static double datoDouble()
{
try
{
return Double.parseDouble (dato());
}
catch (NumberFormatException e)
{
return Double. NaN;
}
}
public static float datoFloat()
{
try
{
return Float.parseFloat (dato());
}
catch (NumberFormatException e)
{
return Float. NaN;
}
}
public static long datoLong()
{
try
{
return Long.parseLong (dato());
}
catch (NumberFormatException e)
{
return Long.MIN_VALUE;
}
}
}