Skip to content

Instantly share code, notes, and snippets.

@oldthreefeng
Last active July 19, 2019 02:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oldthreefeng/ff62aa7fada1d7b4c02d20296a9c4970 to your computer and use it in GitHub Desktop.
Save oldthreefeng/ff62aa7fada1d7b4c02d20296a9c4970 to your computer and use it in GitHub Desktop.
package sort
type Sorter interface{
Len() int
Less(i, j int) bool
Swap(i, j int)
}
func Sort(data Sorter) {
for i:=1; i<=data.Len(); i++{
for j:=0; j<=data.len()-i; j++{
if data.Less(i+1, i) {
data.Swap(i, i+1)
}
}
}
}
type IntArray []int
func (P *IntArray) Len() int{ return len(p) }
func (P *IntArray) Less(i, j int) bool { return p[i]<P[j] }
func (P *IntArray) Swap(i, j int) { return p[i], p[j] = p[j], p[i] }
func SortInt(a []int) { Sort(IntArray(a)) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment