# How to organize an array?

6

How do I get this program to organize the data in the same `Array` so that first the numbers are greater than 0 and then the zeros in the `Ordenar()` method? For example:

Entry

(2,3,0,1,0)

Exit

(2,3,1,0,0)

Here I post the code that I have

``````Scanner leer=new Scanner(System.in);

for(i=0;i<a.length;i++){
System.out.print("ingrese posicion["+i+"]=");
a[i]=leer.nextInt();
}
Ordenar();
}//main

static public void Ordenar(){
//Código que necesito
}
``````

asked by Camilo Diaz 02.11.2016 в 18:47
source

3

Good day Camilo, to organize an Array there are different sorting methods already defined, here I leave you an article about that and its implementation in Java. Then, to what I see in your example, you only need to change the position zeros. so I would recommend a modification of the bubble method, which would be like this

``````public static void burbuja(int[]matrix){
int temp;
for(int i=1;i < matrix.length;i++){
for (int j=0 ; j < matrix.length- 1; j++){
if (matrix[j] == 0){
temp = matrix[j];
matrix[j] = matrix[j+1];
matrix[j+1] = temp;
}
}
}
}
``````

Entry

(2,3,0,1,0)

Exit

(2,3,1,0,0)

source
4

What I understand is that you do not want a traditional "sort", but that you simply want the `0` to be at the end, while the other numbers preserve its order in the array.

One method of doing this is in 2 stages:

• Run all numbers that are not zero to the left.
• Fill the rest of the array with zeros.
• This method only requires a maximum of `array.length x 2` iterations in the worst case, so it is not very expensive.

Code:

``````public static void main (String [] args) {
int[] a = new int[] {0,0,2,3,0,0,0,1,0,4};

// Primer pase, correr los numeros que no son cero a la izquierda.
int lastNonZeroIndex = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] != 0) {
if (i != lastNonZeroIndex) {
a[lastNonZeroIndex] = a[i];
}
lastNonZeroIndex++;
}
}

// Segundo pase, llenar el resto del array con ceros.
for (int i = lastNonZeroIndex; i < a.length; i++) {
a[i] = 0;
}

System.out.println(Arrays.toString(a));
}
``````

Result:

[2, 3, 1, 4, 0, 0, 0, 0, 0, 0]

2

You can initialize the array in which you are going to save the data since in this way you ensure that all the values are going to be 0 by default.

``````int[] a = new int[5];
``````

Then you only have to save the last position in which a number greater than zero has been left and save in that position the new value greater than zero that has entered. Once this is done, add one so that the next value greater than zero is inserted in the new `ultimaPosicion` .

The complete program would be:

``````public static void main(String[] args) {
Scanner leer=new Scanner(System.in);
int[] a = new int[5];
int ultimaPosicion = 0;
int numero;

/* ORDENACIÓN */
for(int i=0; i<a.length; i++){
System.out.println("Introduce un número");
numero = leer.nextInt();
if(numero != 0){
a[ultimaPosicion] = numero;
ultimaPosicion++;
}
}

/* COMPROBAR QUE EL ARRAY ESTÁ ORDENADO */
for(int j = 0; j < a.length; j++){
System.out.print(a[j] + " ");
}
}
``````

In this way, you can save the Sort method and do the order directly in the data entry.

For the entry:

0 2 0 5 4

You would have the exit:

2 5 4 0 0

1

You must use `public static <T> void sort(T[] a, Comparator<? super T> c)` and create a comparator that does what you want. An implementation would be this:

``````int[] array = new int[]{2,3,0,1,0};

Array array2 = new Array<Integer>();
for(int i : array){
}

array2.sort(new Comparator(){

@Override
public int compare(Object o1, Object o2) {
return ((Integer)o1 == 0)?1:-1;
}

});

System.out.println(array2);
``````

The output is:

[1, 3, 2, 0, 0]

0

Depending on the situation you are raising, the fix needs to be sorted after reading it. In other words, you did not have it previously saved, you only know its length. In this case you can read it conveniently and maintain a linear complexity O (n) to your function.

``````  Scanner leer=new Scanner(System.in);

int a_len = 10, j = 0;
int[] a = new int[a_len]; // Inicialmente todos los valores son 0

for(i=0;i<a.length;i++){
System.out.print("ingrese posicion["+i+"]=");
a[j]=leer.nextInt();  // Lees en la última posición donde había un 0

// Ordenar

if(a[j] != 0)
++j;

// Ordenar
}
}//main
``````