Created
July 13, 2015 14:32
-
-
Save ckeyer/cbdbd7425aece664c9e2 to your computer and use it in GitHub Desktop.
希尔排序 是插入排序的一种。也称缩小增量排序,是直接插入排序算法的一种更高效的改进版本。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sort" | |
) | |
func ShellSort(data interface{}, args ...int) { | |
if v, ok := data.(sort.Interface); ok { | |
for gap := v.Len() / 2; gap > 0; gap /= 2 { | |
for i := gap; i < v.Len(); i++ { | |
for j := i - gap; j >= 0 && v.Less(j+gap, j); j -= gap { | |
v.Swap(j, j+gap) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment