Golang排序包 - 模糊排序错误

时间:2016-04-12 15:44:01

标签: sorting go fuzzy

我尝试修改标准排序方法并添加一定的随机性来排序Less接口。 什么时候

if (u[i] - u[j]) <= 0

if u[i] < u[j]

它按预期工作 但是

if (u[i] - u[j]) <= rv

条件在几次执行后会产生恐慌

package main
import ( 
    "crypto/rand"
    "fmt"
    "math/big"
    "sort"
)

type FuzzySorter []float64

func (u FuzzySorter) Len() int {
    return len(u)
}
func (u FuzzySorter) Swap(i, j int) {
    u[i], u[j] = u[j], u[i]
}
func (u FuzzySorter) Less(i, j int) bool {
    pom, _ := rand.Int(rand.Reader, big.NewInt(int64(2)))
    rv := float64(pom.Int64())
    if (u[i] - u[j]) <= rv {
        return true
    } else {
        return false
    }

}
func (u FuzzySorter) Sort() FuzzySorter {
    sort.Sort(u)
    return u
}

func main() {
    unsorted := FuzzySorter{
        0,
        1,
        1,
        1,
        1,
        6,
        0,
        4,
        6,
        1,
        1,
        1,
        0,
        2,
        8,
        1,
        5,
        4,
        6,
        6,
        6,
        16,
        12,
        6,
        1,
        1,
        1,
        0,
        0,
        11,
        2,
        14,
        16,
        6,
        12,
        0,
        4,
        1,
        0,
        16,
        2,
        6,
        0,
        0,
        0,
        0,
        1,
        11,
        1,
        0,
        2,
        1,
        1,
        1,
        1,
        0,
        1,
        12,
        10,
        1,
        5,
        2,
        6,
        4,
        1,
        0,
        0,
        11,
        1,
        1,
        2,
        2,
        1,
        0,
        0,
        1,
        0,
        1,
        17,
        2,
        1,
        1,
        2,
        0,
        3,
        7,
        1,
        5,
        1,
        0,
        1,
        0,
        0,
        0,
        1,
        3,
        1,
        1,
        1,
        2,
        1,
        0,
        3,
        1,
        6,
        1,
        1,
        0,
        1,
        12,
        0,
        1,
        1,
        0,
        1,
        0,
        0,
        6,
        1,
        2,
        2,
        0,
        0,
        2,
        1,
        1,
        0,
        4,
        4,
        1,
        1,
        1,
        0,
        1,
        1,
        1,
        2,
        0,
        0,
        1,
        0,
        1,
        2,
        1,
        2,
        1,
        1,
        0,
        0,
        4,
        1,
        0,
        1,
        0,
        1,
        1,
        3,
        1,
        0,
    }
    unsorted.Sort()
    fmt.Println(unsorted)

}

https://play.golang.org/p/4AxNRN4VD7

恐慌信息

panic: runtime error: index out of range

goroutine 1 [running]:
panic(0x176ba0, 0x1040a010)
    /usr/local/go/src/runtime/panic.go:464 +0x700
main.FuzzySorter.Less(0x10456000, 0x9f, 0x9f, 0x19, 0xffffffff, 0x4, 0x1, 0xd)
    /tmp/sandbox201242525/main.go:21 +0x140
main.(*FuzzySorter).Less(0x10434140, 0x19, 0xffffffff, 0x5c, 0x1, 0x10434140)
    <autogenerated>:3 +0xc0
sort.doPivot(0xfef741b0, 0x10434140, 0x19, 0x9f, 0x7, 0x19)
    /usr/local/go/src/sort/sort.go:128 +0x280
sort.quickSort(0xfef741b0, 0x10434140, 0x19, 0x9f, 0xe, 0xfef741b0)
    /usr/local/go/src/sort/sort.go:195 +0xa0
sort.Sort(0xfef741b0, 0x10434140)
    /usr/local/go/src/sort/sort.go:229 +0x80
main.FuzzySorter.Sort(0x10456000, 0x9f, 0x9f, 0x1, 0x0, 0x0, 0x0, 0x1777a0)
    /tmp/sandbox201242525/main.go:29 +0xa0
main.main()
    /tmp/sandbox201242525/main.go:195 +0xc0

2 个答案:

答案 0 :(得分:0)

据我所知,Go sort实现它期望进行两次负面比较,例如。 Less(i, j)Less(j, i)都返回false,它将其视为平等,但不是正面的。 E.g Less(i, j)Less(j, i)不能都返回true。因此,您可以轻松地以逻辑正确和确定的方式实现所需的结果,只需

if (u[i] - u[j]) < -1 {
        return true
    } else {
        return false
    }

https://play.golang.org/p/VcKI9uzcM9

答案 1 :(得分:0)

从Go 1.8开始,有一种更简单的方法可以对切片进行排序,而不需要您定义新类型。您只需创建一个Less(匿名)lambda。

a := []int{5, 3, 4, 7, 8, 9}
sort.Slice(a, func(i, j int) bool {
    return a[i] < a[j]
})
for _, v := range a {
    fmt.Println(v)
}

这将按升序排序,如果你想要相反,只需写a[i] < a[j]