-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortTheOdd.java
39 lines (32 loc) · 868 Bytes
/
SortTheOdd.java
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
package packCodeWars;
import java.util.ArrayList;
import java.util.Collections;
public class SortTheOdd {
public static int[] sortArray(int[] array) {
ArrayList<Integer> ordenar = new ArrayList<Integer>();
//sacamos los valores a ordenar
for(int i = 0;i< array.length;i++){
if( (array[i] % 2)!= 0){
ordenar.add(array[i]);
array[i]=-1;
}
}
//ordenamos los impares
Collections.sort(ordenar);
//los colocamos una vez ordenados
for(int i = 0;i< array.length;i++){
if( array[i] == -1){
array[i] = ordenar.remove(0);
}
}
return array;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array = {5, 3, 2, 8, 1, 4};
array = SortTheOdd.sortArray(array);
for (int i = 0;i<array.length;i++){
System.out.println(array[i]);
}
}
}