Skip to content

Commit 02e440d

Browse files
author
Eric Lee
committed
update selection sort
1 parent 8a63311 commit 02e440d

2 files changed

Lines changed: 7 additions & 5 deletions

File tree

‎algorithm/sort/selection.go‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package sort
22

3-
// 选择排序,a表示数组,n表示数组大小
4-
func SelectionSort(a []int, n int) {
5-
if n <= 1 {
3+
// 选择排序,a表示数组
4+
func SelectionSort(a []int) {
5+
length := len(a)
6+
if length <= 1 {
67
return
78
}
8-
for i := 0; i < n; i++ {
9+
for i := 0; i < length; i++ {
910
// 查找最小值
1011
minIndex := i
11-
for j := i + 1; j < n; j++ {
12+
for j := i + 1; j < length; j++ {
1213
if a[j] < a[minIndex] {
1314
minIndex = j
1415
}

‎algorithm/sort/sort_test.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package sort

0 commit comments

Comments
 (0)