Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
65 lines (53 loc) · 1.26 KB

File metadata and controls

65 lines (53 loc) · 1.26 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package Algorithms.algorithm.sort;
import java.util.Arrays;
/*
* 给定一个未排序的数组,请给出波浪状排序:
* Example:
* input: 1 2 5 4 3 9
* output: 1 4 3 5 2 9
*
* input: 1 2 2 5 3 9
* output: 1 5 2 3 2 9
* */
public class WaveSort {
public static void main(String[] str) {
int[] in = {1,2,5,4,3,9,9,9, 12};
for (int i: in) {
System.out.print(i + " ");
}
System.out.println();
waveSort(in);
for (int i: in) {
System.out.print(i + " ");
}
}
public static void waveSort(int[] in) {
if (in == null) {
return;
}
// there should be at least 3 numbers.
if (in.length <= 2) {
return;
}
int len = in.length;
Arrays.sort(in);
for (int i: in) {
System.out.print(i + " ");
}
System.out.println();
for (int i = 0; i < len; i++) {
if (i % 2 == 0) {
for (int j = i + 1; j < len; j++) {
if (in[j] != in[i]) {
int tmp = in[i];
in[i] = in[j];
in[j] = tmp;
break;
}
}
}
}
}
public static void findKthNumber(int[] in, int k) {
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.