Golang program for implementation of Comb Sort
The Comb Sort is a variant of the Bubble Sort. Bubble sort always compares adjacent values. So all inversions are removed one by one. Comb Sort improves on Bubble Sort by using gap of size more than 1. The gap starts with a large value and shrinks by a factor of 1.3 in every iteration until it reaches the value 1. Thus Comb Sort removes more than one inversion counts with one swap and performs better than Bubble Sort.
Example
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
slice := generateSlice(20)
fmt.Println("\n--- Unsorted --- \n\n", slice)
combsort(slice)
fmt.Println("\n--- Sorted ---\n\n", slice, "\n")
}
// Generates a slice of size, size filled with random numbers
func generateSlice(size int) []int {
slice := make([]int, size, size)
rand.Seed(time.Now().UnixNano())
for i := 0; i < size; i++ {
slice[i] = rand.Intn(999) - rand.Intn(999)
}
return slice
}
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
}
}
}
}
Output
--- Unsorted ---
[-317 -381 -14 -215 -590 -243 -412 380 -312 925 158 -46 177 22 -482 273 217 514 -392 424]
--- Sorted ---
[-590 -482 -412 -392 -381 -317 -312 -243 -215 -46 -14 22 158 177 217 273 380 424 514 925]
Most Helpful This Week
Golang program for implementation of Floyd–Warshall Algorithm
Golang program for implementation of Interpolation Search
Golang program for implementation of AVL Trees
Golang program to print a matrix in Spiral Format
Golang program for implementation of Knuth–Morris–Pratt (KMP) Algorithm
Golang program for implementation of Binary Search