Repository navigation
Expand file tree
/
Copy pathSortingNumbers.java
More file actions
74 lines (62 loc) · 2.17 KB
/
Copy pathSortingNumbers.java
File metadata and controls
74 lines (62 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class SortingNumbers {
//ordena los numeros de menor a mayor
private static void sort(int a[], int c){
for (int i = 1; i < c; i++){
int count = i;
int newArray = a[i];
while ((count > 0)&&(a[count - 1]> newArray)){
a[count] = a[count - 1];
count--;
}
a[count] = newArray;
}
}
//redimensiona el array para no usar una dimension harcode.
private static Object resizeArray (Object oldArray, int newSize) {
int oldSize = java.lang.reflect.Array.getLength(oldArray);
Class elementType = oldArray.getClass().getComponentType();
Object newArray = java.lang.reflect.Array.newInstance(elementType, newSize);
int preserveLength = Math.min(oldSize, newSize);
if (preserveLength > 0){
System.arraycopy(oldArray, 0, newArray, 0, preserveLength);
}
return newArray;
}
//Main: ejecuta los metodos de ordenar y redimensionar para pedir por pantalla y dar salida al nuevo array.
public static void main (String[] args){
//redimensionarArray red = new redimensionarArray();
Console con = System.console();
String seguir = "S";
int pos =0;
int[] in = new int[pos+1];
int i;
System.out.println("ORGANIZADOR DE NUMEROS INGRESADOS POR TECLADO DE MENOR A MAYOR");
while (seguir.equals("S")){
System.out.println("Favor ingrese un numero: ");
in[pos] = Integer.parseInt(con.readLine());
System.out.println("Desea ingresar otro numero S, N para parar ");
seguir = con.readLine();
seguir = seguir.toUpperCase();
seguir.trim();
pos++;
in = (int[])resizeArray(in, (pos+1));
//System.out.println("Dimension: "+in.length);
}
System.out.println();
System.out.println("El orden en que se ingresaron");
for (i = 0; i < in.length; i++){
System.out.print(in[i]+" ");
}
System.out.println();
System.out.println("De la manera que se ordenaron");
sort(in, in.length);
for (i = 0; i < in.length; i++){
System.out.print(in[i]+" ");
}
System.out.println();
}
}