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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
| /**
* 插入排序算法
*/
class InsertSort implements Sort {
public void sort(int[] data) {
for (int i = 1; i < data.length; i++) {
for (int j = i; j > 0 && data[j] < data[j - 1]; j--) {
SortUtil.swap(data, j, j - 1);
}
}
}
}
/**
* 冒泡排序算法
*/
class BubbleSort implements Sort {
public void sort(int[] data) {
for (int i = 0; i < data.length; i++) {
for (int j = data.length - 1; j > i; j--) {
if (data[j] < data[j - 1]) {
SortUtil.swap(data, j, j - 1);
}
}
}
}
}
/**
* 选择排序算法
*/
class SelectionSort implements Sort {
public void sort(int[] data) {
for (int i = 0; i < data.length; i++) {
int lowIndex = i;
for (int j = data.length - 1; j > i; j--) {
if (data[j] < data[lowIndex]) {
lowIndex = j;
}
}
SortUtil.swap(data, i, lowIndex);
}
}
}
/**
* 希尔排序算法
*/
class ShellSort implements Sort {
public void sort(int[] data) {
for (int i = data.length / 2; i > 2; i /= 2) {
for (int j = 0; j < i; j++) {
insertSort(data, j, i);
}
}
insertSort(data, 0, 1);
}
private void insertSort(int[] data, int start, int inc) {
for (int i = start + inc; i < data.length; i += inc) {
for (int j = i; j >= inc && data[j] < data[j - inc]; j -= inc) {
SortUtil.swap(data, j, j - inc);
}
}
}
}
/**
* 快速排序算法
*/
class QuickSort implements Sort {
public void sort(int[] data) {
quickSort(data, 0, data.length - 1);
}
private void quickSort(int[] data, int left, int right) {
if (left >= right) { return; }
int pivot = partition(data, left, right);
quickSort(data, left, pivot);// 对左半段排序
quickSort(data, pivot + 1, right);// 对右半段排序
}
private int partition(int[] data, int left, int right) {
int pivot = data[left];
while (left < right) {
while (left < right && data[right] > pivot) {
right--;
}
if (left < right) {
SortUtil.swap(data, left, right);
}
while (left < right && data[left] < pivot) {
left++;
}
if (left < right) {
SortUtil.swap(data, left, right);
}
}
return left;
}
}
/**
* 归并排序算法
*/
class MergeSort implements Sort {
public void sort(int[] data) {
int[] temp = new int[data.length];
mergeSort(data, temp, 0, data.length - 1);
}
private void mergeSort(int[] data, int[] temp, int left, int right) {
if (left == right) { return; }
int mid = (left + right) / 2;
mergeSort(data, temp, left, mid);
mergeSort(data, temp, mid + 1, right);
for (int i = left; i <= right; i++) {
temp = Arrays.copyOf(data, data.length);
}
int i1 = left;
int i2 = mid + 1;
for (int cur = left; cur <= right; cur++) {
if (i1 == mid + 1) {
data[cur] = temp[i2++];
} else if (i2 > right) {
data[cur] = temp[i1++];
} else if (temp[i1] < temp[i2]) {
data[cur] = temp[i1++];
} else {
data[cur] = temp[i2++];
}
}
}
}
/**
* 堆排序算法
*/
class HeapSort implements Sort {
public void sort(int[] data) {
MaxHeap h = new MaxHeap();
h.init(data);
for (int i : data) {
h.remove();
}
System.arraycopy(h.queue, 1, data, 0, data.length);
}
private static class MaxHeap {
void init(int[] data) {
this.queue = new int[data.length + 1];
for (int element : data) {
queue[++size] = element;
fixUp(size);
}
}
private int size = 0;
private int[] queue;
public void remove() {
SortUtil.swap(queue, 1, size--);
fixDown(1);
}
// fixdown
private void fixDown(int k) {
int j;
while ((j = k << 1) <= size) {
if (j < size && queue[j] < queue[j + 1]) {
j++;
}
if (queue[k] > queue[j]) { // 不用交换
break;
}
SortUtil.swap(queue, j, k);
k = j;
}
}
private void fixUp(int k) {
while (k > 1) {
int j = k >> 1;
if (queue[j] > queue[k]) {
break;
}
SortUtil.swap(queue, j, k);
k = j;
}
}
}
}
|