Last active
July 12, 2017 03:42
-
-
Save RichardKnop/8e13e98fd8bbfddd36b2002e66de9492 to your computer and use it in GitHub Desktop.
Comb Sort
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" | |
) | |
func main() { | |
items := []int{4, 202, 3, 9, 6, 5, 1, 43, 506, 2, 0, 8, 7, 100, 25, 4, 5, 97, 1000, 27} | |
combsort(items) | |
fmt.Println(items) | |
} | |
func combsort(items []int) { | |
var ( | |
n = len(items) | |
gap = len(items) | |
shrink = 1.3 | |
swapped = true | |
) | |
for swapped { | |
swapped = false | |
gap = int(float64(gap) / shrink) | |
if gap < 1 { | |
gap = 1 | |
} | |
for i := 0; i+gap < n; i++ { | |
if items[i] > items[i+gap] { | |
items[i+gap], items[i] = items[i], items[i+gap] | |
swapped = true | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment