move comb_sort to separate package
Signed-off-by: Alexander Kuleshov <kuleshovmail@gmail.com>move-sorts-to-separate-packages
parent
22a9e841eb
commit
63d2d69629
@ -1,45 +1,34 @@
|
|||||||
package main
|
package comb_sort
|
||||||
|
|
||||||
/*
|
import (
|
||||||
* Comb sort - https://en.wikipedia.org/wiki/Combsort
|
sortable "github.com/0xAX/go-algorithms/sort"
|
||||||
*/
|
)
|
||||||
|
|
||||||
import "fmt"
|
func Sort(data sortable.Sortable) sortable.Sortable {
|
||||||
|
gap := data.Len()
|
||||||
import "github.com/0xAX/go-algorithms"
|
|
||||||
|
for {
|
||||||
func main() {
|
if gap > 1 {
|
||||||
arr := utils.RandArray(10)
|
gap = gap * 100 / 124
|
||||||
fmt.Println("Initial array is:", arr)
|
}
|
||||||
fmt.Println("")
|
|
||||||
|
for i := 0; ; {
|
||||||
tmp := 0
|
|
||||||
gap := len(arr)
|
if data.Less(i+gap, i) {
|
||||||
|
data.Swap(i, i+gap)
|
||||||
for {
|
}
|
||||||
if gap > 1 {
|
|
||||||
gap = gap * 100 / 124
|
i++
|
||||||
}
|
|
||||||
|
if i+gap >= data.Len() {
|
||||||
for i := 0 ; ; {
|
break
|
||||||
|
}
|
||||||
if arr[i] > arr[i + gap] {
|
}
|
||||||
tmp = arr[i]
|
|
||||||
arr[i] = arr[i + gap]
|
if gap == 1 {
|
||||||
arr[i + gap] = tmp
|
break
|
||||||
}
|
}
|
||||||
|
}
|
||||||
i++
|
|
||||||
|
return data
|
||||||
if i + gap >= len(arr){
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if gap == 1 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println("Sorted array is: ", arr)
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,25 @@
|
|||||||
|
package comb_sort
|
||||||
|
|
||||||
|
import (
|
||||||
|
sortable "github.com/0xAX/go-algorithms/sort"
|
||||||
|
"math/rand"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSort(t *testing.T) {
|
||||||
|
var s = make(sortable.IntSlice, 10)
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
s[i] = rand.Intn(i + 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
Sort(&s)
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
if i != 9 {
|
||||||
|
if s[i] > s[i+1] {
|
||||||
|
t.Fatal("s[i] > s[i + 1]", s[i], s[i+1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue