Skip to content

Commit 4e0649e

Browse files
Merge pull request wangzheng0822#70 from RichardWeiYang/master
11_sorts: more tight boundary for selectionSort() and remove minValue
2 parents 858adad + be96922 commit 4e0649e

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

java/11_sorts/Sorts.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,11 @@ public static void insertionSort(int[] a, int n) {
4949
// 选择排序,a表示数组,n表示数组大小
5050
public static void selectionSort(int[] a, int n) {
5151
if (n <= 1) return;
52-
for (int i = 0; i < n; ++i) {
52+
for (int i = 0; i < n - 1; ++i) {
5353
// 查找最小值
5454
int minIndex = i;
55-
int minValue = a[i];
56-
for (int j = i; j < n; ++j) {
57-
if (a[j] < minValue) {
58-
minValue = a[j];
55+
for (int j = i + 1; j < n; ++j) {
56+
if (a[j] < a[minIndex]) {
5957
minIndex = j;
6058
}
6159
}

0 commit comments

Comments
 (0)