-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwap0Sort.java
More file actions
39 lines (35 loc) · 906 Bytes
/
Swap0Sort.java
File metadata and controls
39 lines (35 loc) · 906 Bytes
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 huawei;
/**
* Created by LXF on 2017/8/15.
*/
public class Swap0Sort {
public static void main(String[] args) {
int[] arr = new int[]{1, 6, 5, 4, 0, 3, 2};
int n = arr.length;
if (arr[0] != 0) {
for (int j = 1; j < n; j++) {
if (arr[0] > arr[j] && arr[j] == 0) {
swap(arr, 0, j);
}
}
}
for (int i = 1; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
swap(arr, 0, i);
swap(arr, i, j);
swap(arr, 0, j);
}
}
}
for (int i : arr) {
System.out.print(i);
}
}
public static void swap (int[] a,int i, int j){
int temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}