-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrategyTest.java
More file actions
301 lines (260 loc) · 6.67 KB
/
StrategyTest.java
File metadata and controls
301 lines (260 loc) · 6.67 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package Strategy;
import java.lang.reflect.Array;
import java.util.Arrays;
//一个排序算法族
public class StrategyTest {
public static void main(String[] args) {
//客户端
//使用冒泡排序
SortUse<Integer> user = new SortUse<Integer>(new Integer[]{99,2,100,33,2,2,3,555,6,7,88},new BubbleSort<Integer>());
System.out.println("冒泡排序前:"+user);
user.sort();
System.out.println("冒泡排序后:"+user);
//使用插入排序
user.setT(new Integer[]{45,2,4,111,98,54,0,1,2});
user.setSort(new InsertSort<Integer>());
System.out.println("插入排序前:"+user);
user.sort();
System.out.println("插入排序后:"+user);
//使用直接选择排序
user.setT(new Integer[]{45,2,4,111,98,54,0,1,2});
user.setSort(new SelectionSort<Integer>());
System.out.println("直接选择排序前:"+user);
user.sort();
System.out.println("直接选择排序后:"+user);
//使用归并排序
user.setT(new Integer[]{45,2,4,111,98,54,0,1,2});
user.setSort(new MergeSort<Integer>());
System.out.println("归并排序前:"+user);
user.sort();
System.out.println("归并排序后:"+user);
//使用归并排序
user.setT(new Integer[]{111,98,54,45,4,2,2,1,0});
user.setSort(new QuickSort<Integer>());
System.out.println("快速排序前:"+user);
user.sort();
System.out.println("快速排序后:"+user);
}
}
/*
* @Component:排序算法的上下文:持有一个排序算法的引用。和算法交互。调用排序算法。
*/
class SortUse<T extends Comparable<? super T>>{
private Sort<T> sort;
private T[] t;
//传入要排序的对象数组、比较器以及要使用的排序算法
public SortUse(T[] t,Sort<T> sort){
this.sort = sort;
this.t = t;
}
public void sort(){
this.sort.sort(t);
}
public Sort<T> getSort() {
return sort;
}
public void setSort(Sort<T> sort) {
this.sort = sort;
}
public T[] getT() {
return t;
}
public void setT(T[] t) {
this.t = t;
}
public String toString(){
return Arrays.toString(t);
}
}
/*
*@Component:排序的虚拟类:代表一个排序算法对象。 为上下文对象定义了公共接口。
*/
abstract class Sort<T extends Comparable<? super T>>{
public abstract void sort(T[] t);
}
/*
* 具体的算法实现类:实现了虚拟类的接口
* 具体的算法实现类:冒泡排序
*/
class BubbleSort<T extends Comparable<? super T>> extends Sort<T>{
@Override
public void sort(T[] t) {
T temp;
for(int i = t.length-1;i>=0;i--){
for(int j = i-1;j>=0;j--){
if(t[i].compareTo(t[j]) < 0){
temp = t[i];
t[i] = t[j];
t[j] = temp;
}
}
}
}
}
/*
* @Component:具体的算法实现类:直接插入排序
* 将值插入一个已经排好序的序列
*/
class InsertSort<T extends Comparable<? super T>> extends Sort<T>{
@Override
public void sort(T[] t) {
T temp;
//从第一个开始排
for(int i = 1;i < t.length;i++){
for(int j = 0;j < i;j++){
if(t[i].compareTo(t[j]) < 0){
temp = t[i];
//要插入的位置之后的元素全部向后移
for(int k = i;k > j;k--){
t[k] = t[k-1];
}
t[j] = temp;
break;
}
}
}
}
}
/*
* @Component:具体的算法实现类:希尔排序
*
*/
class ShellSort<T extends Comparable<? super T>> extends Sort<T>{
@Override
public void sort(T[] t) {
}
}
/*
* @Component:具体的算法实现类:直接选择排序
* 从后面的序列中选取最大或最小的值,将其和已经排好序的序列的后一位交换值
*/
class SelectionSort<T extends Comparable<? super T>> extends Sort<T>{
@Override
public void sort(T[] t) {
//记录要交换元素的坐标
int index;
//记录最小值
T min;
//临时值,交换两个坐标的值使用
T temp;
for(int i = 0;i < t.length-1;i++){
min = t[i];
index = i;
for(int j = i+1;j < t.length;j++){
if(min.compareTo(t[j]) > 0){
min = t[j];
index = j;
}
}
temp = t[i];
t[i] = t[index];
t[index] = temp;
}
}
}
/*
* @Component:具体的算法实现类:堆排序
*
*/
class HeapSort<T extends Comparable<? super T>> extends Sort<T>{
@Override
public void sort(T[] t) {
// TODO Auto-generated method stub
}
}
/*
* @Component:具体的算法实现类:归并排序
* (1)两个序列各自排好序
* (2)合并两个排好序的序列,使之成为一个有序的序列
*/
class MergeSort<T extends Comparable<? super T>> extends Sort<T>{
@Override
public void sort(T[] t) {
sort(t,0,t.length-1);
}
private void sort(T[] t,int low,int high){
int middle = (low + high) / 2;
if(low < high){
//middle左边排序
sort(t,low,middle);
//middle右边排序
sort(t,middle+1,high);
//排好序的两个序列合并
mergeTwoList(t,low,middle,high);
}
}
//两个排好序的数组进行合并
@SuppressWarnings("unchecked")
private void mergeTwoList(T[] t,int low,int middle,int high){
if(t.length > 0 && null != t[0]){
//构建一个临时数组,存放排序后的值
T[] temp = (T[]) Array.newInstance(t[0].getClass(), high - low +1);
int headOfList1 = low;
int headOfList2 = middle + 1;
int indexOfTemp = 0;
while(headOfList1 <= middle && headOfList2 <= high){
if(t[headOfList1].compareTo(t[headOfList2]) < 0){
temp[indexOfTemp++] = t[headOfList1++];
}else{
temp[indexOfTemp++] = t[headOfList2++];
}
}
//此时一定有一个数组走空或两个数组走空,把另一个数组的残余数据移到临时数据中
while(headOfList1 <= middle){
temp[indexOfTemp++] = t[headOfList1++];
}
while(headOfList2 <= high){
temp[indexOfTemp++] = t[headOfList2++];
}
indexOfTemp = 0;
//把排好序的临时数组的数据移到原先的数组中
for(int i = low;i <= high;i++){
t[i] = temp[indexOfTemp++];
}
}else{
throw new RuntimeException();
}
}
}
/*
* @Component:具体的算法实现类:快速排序
*(1)在数据集之中,选择一个元素作为"基准"(pivot)。
*(2)所有小于"基准"的元素,都移到"基准"的左边;所有大于"基准"的元素,都移到"基准"的右边。
*(3)对"基准"左边和右边的两个子集,不断重复第一步和第二步,直到所有子集只剩下一个元素为止。
*/
class QuickSort<T extends Comparable<? super T>> extends Sort<T>{
@Override
public void sort(T[] t) {
sort(t,0,t.length-1);
}
private void sort(T[] t,int low,int high){
//low < high的情况下才排序
if(low < high){
T baseObject = t[low];
int left = low;
int right = high;
//记录下一个要填的坐标值
int index = low;
while(left != right){
//防止空指针把left < right放到前面来
while(left < right && t[right].compareTo(baseObject) >= 0){
right--;
}
t[index] = t[right];
t[right] = null;
index = right;
while(left < right && t[left].compareTo(baseObject) <= 0){
left++;
}
t[index] = t[left];
t[left] = null;
index = left;
}
t[index] = baseObject;
//左半区排序
sort(t,low,index-1);
//右半区排序
sort(t,index+1,high);
}
}
}