Skip to content

Commit becee6a

Browse files
committed
modify
1 parent 035b826 commit becee6a

File tree

4 files changed

+42
-47
lines changed

4 files changed

+42
-47
lines changed
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
package org.algorithm.array.sort.client;
22

3-
import org.algorithm.array.sort.impl.BubbleBothwaySort;
4-
import org.algorithm.array.sort.impl.BubbleOptimiSort;
5-
import org.algorithm.array.sort.impl.BubbleSort;
6-
import org.algorithm.array.sort.impl.BubbleStandardSort;
3+
import org.algorithm.array.sort.impl.OddEvenSort;
74
import org.algorithm.array.sort.interf.Sortable;
85
import org.utils.naga.containers.ArrayUtils;
96

107
public class SortClient {
118

129
public static void main(String[] args) {
13-
// int[] array = {34, 23, 76, 56, 54, 12, 34, 65, 45, 9, 8, 7, 6, 5};
10+
int[] array = {34, 23, 76, 56, 54, 12, 34, 65, 45, 9, 8, 7, 6, 5};
1411
// int[] array = {34, 23, 98, 76, 56, 45};
1512
// int[] array = {23, 34, 54, 56, 76, 98};
16-
int[] array = {6, 5, 4, 3, 2, 1};
17-
Sortable sortable = new BubbleBothwaySort();
13+
// int[] array = {6, 5, 4, 3, 2, 1};
14+
ArrayUtils.show(array);
15+
Sortable sortable = new OddEvenSort();
1816
array = sortable.sort(array);
19-
20-
// ArrayUtils.show(array);
17+
ArrayUtils.show(array);
2118
}
2219
}

src/org/algorithm/array/sort/impl/BubbleBothwaySort.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package org.algorithm.array.sort.impl;
22

3-
import java.util.Arrays;
4-
53
import org.algorithm.array.sort.interf.Sortable;
64
import org.utils.naga.containers.ArrayUtils;
7-
import org.utils.naga.threads.ThreadUtils;
85

96
/**
107
* <p>
@@ -24,6 +21,12 @@ public int[] sort(int[] array) {
2421
return null;
2522
}
2623

24+
core(array);
25+
26+
return array;
27+
}
28+
29+
private void core(int[] array) {
2730
int arrayLength = array.length;
2831

2932
int preIndex = 0;
@@ -32,38 +35,32 @@ public int[] sort(int[] array) {
3235
preSort(array, arrayLength, preIndex);
3336
preIndex++;
3437

38+
ArrayUtils.show(array);
3539
if (preIndex >= backIndex) {
3640
break;
3741
}
3842

39-
ThreadUtils.sleep(10);
40-
41-
backSort(array, arrayLength, backIndex);
43+
backSort(array, backIndex);
4244
backIndex--;
4345

44-
ThreadUtils.sleep(10);
46+
ArrayUtils.show(array);
4547
}
46-
47-
return array;
4848
}
49-
49+
5050
// 从前向后排序
5151
private void preSort(int[] array, int length, int preIndex) {
5252
for (int i = preIndex + 1; i < length; i++) {
5353
if (array[preIndex] > array[i]) {
5454
ArrayUtils.swap(array, preIndex, i);
55-
ArrayUtils.show(array);
5655
}
5756
}
5857
}
5958

6059
// 从后向前排序
61-
private void backSort(int[] array, int length, int backIndex) {
60+
private void backSort(int[] array, int backIndex) {
6261
for (int i = backIndex - 1; i >= 0; i--) {
6362
if (array[i] > array[backIndex]) {
6463
ArrayUtils.swap(array, i, backIndex);
65-
// ArrayUtils.show(array);
66-
System.err.println(Arrays.toString(array));
6764
}
6865
}
6966
}

src/org/algorithm/array/sort/impl/BubbleSort.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
/**
77
* <p>
8-
* 初级版冒泡排序(单向冒泡)
8+
* 标准的冒泡排序算法
99
* </p>
10-
* 2016年1月19日
10+
* 2016年1月27日
1111
*
1212
* @author <a href="http://weibo.com/u/5131020927">Q-WHai</a>
1313
* @see <a href="http://blog.csdn.net/lemon_tree12138">http://blog.csdn.net/lemon_tree12138</a>
@@ -21,17 +21,22 @@ public int[] sort(int[] array) {
2121
return null;
2222
}
2323

24+
core(array);
25+
26+
return array;
27+
}
28+
29+
private void core(int[] array) {
30+
2431
int arrayLength = array.length;
32+
2533
for (int i = 0; i < arrayLength; i++) {
26-
for (int j = i + 1; j < arrayLength; j++) {
27-
if (array[i] > array[j]) {
28-
ArrayUtils.swap(array, i, j);
29-
ArrayUtils.show(array);
34+
for (int j = arrayLength - 2; j >= i; j--) {
35+
if (array[j] > array[j + 1]) {
36+
ArrayUtils.swap(array, j, j + 1);
3037
}
3138
}
39+
ArrayUtils.show(array);
3240
}
33-
34-
return array;
3541
}
36-
3742
}

src/org/algorithm/array/sort/impl/BubbleStandardSort.java renamed to src/org/algorithm/array/sort/impl/SampleSwapSort.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,34 @@
55

66
/**
77
* <p>
8-
* 标准的冒泡排序算法
8+
* 初级版冒泡排序(单向冒泡)
9+
* [简单的交换排序]
910
* </p>
10-
* 2016年1月27日
11+
* 2016年1月19日
1112
*
1213
* @author <a href="http://weibo.com/u/5131020927">Q-WHai</a>
1314
* @see <a href="http://blog.csdn.net/lemon_tree12138">http://blog.csdn.net/lemon_tree12138</a>
1415
* @version 0.1.1
1516
*/
16-
public class BubbleStandardSort implements Sortable {
17+
public class SampleSwapSort implements Sortable {
1718

1819
@Override
1920
public int[] sort(int[] array) {
2021
if (array == null) {
2122
return null;
2223
}
2324

24-
core(array);
25-
26-
return array;
27-
}
28-
29-
private void core(int[] array) {
30-
3125
int arrayLength = array.length;
32-
3326
for (int i = 0; i < arrayLength; i++) {
34-
for (int j = arrayLength - 2; j >= i; j--) {
35-
if (array[j] > array[j + 1]) {
36-
ArrayUtils.swap(array, j, j + 1);
37-
ArrayUtils.show(array);
27+
for (int j = i + 1; j < arrayLength; j++) {
28+
if (array[i] > array[j]) {
29+
ArrayUtils.swap(array, i, j);
3830
}
3931
}
32+
ArrayUtils.show(array);
4033
}
34+
35+
return array;
4136
}
37+
4238
}

0 commit comments

Comments
 (0)