Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f1c0edf

Browse files
committed
add fmt for understand insertion sort
1 parent 8fa35c3 commit f1c0edf

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

‎insertion_sort.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
package algorithm
22

3+
import "fmt"
4+
35
// InsertionSort ..
46
// 算法步骤:
57
// 将第一待排序序列第一个元素看做一个有序序列,把第二个元素到最后一个元素当成是未排序序列。
68
// 从头到尾依次扫描未排序序列,将扫描到的每个元素插入有序序列的适当位置。
79
//(如果待插入的元素与有序序列中的某个元素相等,则将待插入元素插入到相等元素的后面。)
10+
// arr := []int{3, 2, 0, 5, 10, 1}
811
func InsertionSort(arr []int) []int {
912
for i := range arr {
1013
preIndex := i - 1
1114
current := arr[i]
15+
fmt.Printf("current: %v\n", current)
1216
for preIndex >= 0 && arr[preIndex] > current {
1317
arr[preIndex+1] = arr[preIndex]
1418
preIndex--
1519
}
20+
fmt.Printf("finally i: %v, current: %v, preIndex: %v\n", i, current, preIndex)
1621
arr[preIndex+1] = current
1722
}
1823
return arr

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /