forked from xingzhougmu/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort
81 lines (70 loc) · 1.37 KB
/
sort
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
75
76
77
78
79
80
81
/*
sort an array to make it all odd numbers are in front of even numbers, and keep the original relative order.
Space complexity: in-place; time complexity: O(N)
Input:
A[]=[5,6,8,2,3,9,4]; output:
[5,3,9,6,8,2,4]
*/
public class Sort{
public static void main(String[] args){
sort1(array);
for (int i=0; i<array.length; i++)
System.out.print(array[i] + " ");
}
private static void sort(int[] array){
int odd = 0;
int even = 0;
for (int i=0; i<array.length; i++){
if (array[i]%2 != 0)
even++;
else
break;
}
for (int i=0; i<array.length; i++){
if (array[i]%2 == 0)
odd++;
else
break;
}
if (odd > even){
int tmp = array[odd];
for (int j=odd; j>even; j--)
array[j] = array[j-1];
array[even] = tmp;
odd = even;
even = odd + 1;
}
for (int i=odd+1;i<array.length; i++){
if (array[i]%2 != 0){
int tmp = array[i];
for (int j=i; j>even; j--)
array[j] = array[j-1];
array[even] = tmp;
even++;
}
}
}
private static void sort1(int[] array){
int odd = 0;
int even = 0;
for (int i=0; i<array.length; i++){
if (array[i]%2 != 0)
even++;
else
break;
}
for (int i=0;i<array.length; i++){
if (array[i]%2 != 0){
odd = i;
if (odd > even){
int tmp = array[i];
for (int j=i; j>even; j--)
array[j] = array[j-1];
array[even] = tmp;
even++;
}
}
}
}
private static int[] array = {5, 6, 8, 2, 3, 9, 4};
}