本文整理汇总了Golang中sort.Interface.Less方法的典型用法代码示例。如果您正苦于以下问题:Golang Interface.Less方法的具体用法?Golang Interface.Less怎么用?Golang Interface.Less使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sort.Interface
的用法示例。
在下文中一共展示了Interface.Less方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: insertionSort
// Simple insertion sort for smaller data collections.
func insertionSort(data sort.Interface, lo, hi int) {
for i := lo + 1; i < hi+1; i++ {
for j := i; j > lo && data.Less(j, j-1); j-- {
data.Swap(j, j-1)
}
}
}
示例2: partialSort
// Partial quicksort algorithm due to Martínez (2004),
// http://www.cs.upc.edu/~conrado/research/reports/ALCOMFT-TR-03-50.pdf
func partialSort(data sort.Interface, k, lo, hi int) {
for hi-lo > 5 {
p := medianOfThree(data, lo, hi)
p = partition(data, lo, hi, p)
if p < k-1 {
partialSort(data, k, p+1, hi)
}
hi = p
}
// Finish off with a selection sort.
if hi-lo-1 < k {
k = hi - lo - 1
}
for ; k > 0; k-- {
min := lo
for i := lo + 1; i < hi; i++ {
if data.Less(i, min) {
min = i
}
}
data.Swap(lo, min)
lo++
}
}
示例3: InsertionSort
// InsertionSort 插入排序,时间复杂度O(n^2)。
//
// 这个实现是拷贝的sort包下的 insertionSort()。
func InsertionSort(data sort.Interface, start, end int) {
for i := start + 1; i < end; i++ {
for j := i; j > start && data.Less(j, j-1); j-- {
data.Swap(j, j-1)
}
}
}
示例4: HeapSort
// HeapSort sorts given data and has next properties:
//
// - Not stable
// - O(1) extra space
// - O(n*lg(n)) time
// - Not really adaptive
//
func HeapSort(data sort.Interface) {
// down restores order of heap.
down := func(root, n int) {
for {
lch := 2*root + 1
if lch >= n || lch < 0 { // child < 0 when int overflow.
break
}
if rch := lch + 1; rch < n && data.Less(lch, rch) { // lch+1 == 2*root + 2 // Right child.
lch = rch
}
if !data.Less(root, lch) { // Heap is ordered.
return
}
data.Swap(root, lch)
root = lch
}
}
// Heapify (build a max heap).
for i := (data.Len() - 1) / 2; i >= 0; i-- {
down(i, data.Len())
}
// Pop elements, largest first, into end of data.
// Loop invariant: data[i:] contains the data.Len()-1-i largest elements
// of maxHeap and the maxHeap contains i+1 smallest elements.
for i := data.Len() - 1; i >= 0; i-- {
data.Swap(0, i)
down(0, i)
}
}
示例5: isort
func isort(A sort.Interface, a, b int) {
for j := a + 1; j < b; j++ {
for i := j; i > a && A.Less(i, i-1); i-- {
A.Swap(i, i-1)
}
}
}
示例6: partition
// partition the subarray a[lo .. hi] by returning an index j
// so that a[lo .. j-1] <= a[j] <= a[j+1 .. hi]
func partition(a sort.Interface, lo, hi int) int {
i, j := lo+1, hi
// v = a[lo]
for {
for a.Less(i, lo) {
if i == hi {
break
}
i++
}
for a.Less(lo, j) {
if j == lo {
break
}
j--
}
if i >= j {
break
}
a.Swap(i, j)
i++
j--
}
// put v = a[j] into position
a.Swap(lo, j)
// with a[lo .. j-1] <= a[j] <= a[j+1 .. hi]
return j
}
示例7: Insert
// Insert adds in-order element of sort.Interface at index Tree.Len()
// It doesn't check for equality, so duplicates are inserted in
// stable order.
func (t *Tree) Insert(data sort.Interface) {
ix := len(t.nodes)
if ix == MaxSize {
panic("tree size exceed maximum")
}
t.nodes = append(t.nodes, node{null, null, null, 1})
if ix == 0 {
t.root, t.min, t.max = 0, 0, 0
return
}
var dir direction
cur := t.root
curnode := &t.nodes[cur]
for {
dir = direction(!data.Less(ix, int(cur)))
if curnode.link(dir) == null {
break
}
cur = curnode.link(dir)
curnode = &t.nodes[cur]
}
node := &t.nodes[ix]
node._parent = index(cur)
curnode.set_link(dir, ix)
if dir == right {
if cur == t.max {
t.max = ix
}
} else if cur == t.min {
t.min = ix
}
t.balance(cur)
}
示例8: check
func check(t *testing.T, data sort.Interface, tree *Tree, ix int) int8 {
node := &tree.nodes[ix]
l, r := int(node._left), int(node._right)
var lh, rh int8
if l != null {
if data.Less(ix, l) {
t.Fatalf("%d < %d", ix, l)
}
lh = check(t, data, tree, l)
} else {
lh = 0
}
if r != null {
if data.Less(r, ix) {
t.Fatalf("%d < %d", r, ix)
}
rh = check(t, data, tree, r)
} else {
rh = 0
}
bal := lh - rh
if bal < -1 || bal > 1 || tree.bal(ix) != bal {
t.Fatalf("height fails: %d [%d, %d]",
ix, lh, rh)
}
return max_i8(lh, rh) + 1
}
示例9: TimSort
// TimSort sorts the data defined by sort.Interface.
func TimSort(a sort.Interface) (err error) {
indexes := make([]int, a.Len())
for i := 0; i < len(indexes); i++ {
indexes[i] = i
} // for i
err = Ints(indexes, func(i, j int) bool {
return a.Less(i, j)
})
if err != nil {
return err
} // if
for i := 0; i < len(indexes); i++ {
j := indexes[i]
if j == 0 {
continue
} // if
for k := i; j != i; {
a.Swap(j, k)
k, j, indexes[j] = j, indexes[j], 0
} // for j
} // for i
return nil
}
示例10: insertionSort
func insertionSort(a sort.Interface) {
for i := 1; i < a.Len(); i++ {
for j := i; j > 0 && a.Less(j, j-1); j-- {
a.Swap(j-1, j)
}
}
}
示例11: insertionSort
// Insertion sort
func insertionSort(data sort.Interface, a, b int) {
for i := a + 1; i < b; i++ {
for j := i; j > a && data.Less(j, j-1); j-- {
data.Swap(j, j-1)
}
}
}
示例12: Quicksort
func Quicksort(sortable sort.Interface) {
partition := func(sortable sort.Interface, lo, hi int) int {
pivot := hi
i := lo
for j := lo; j < hi; j++ {
if sortable.Less(j, pivot) {
sortable.Swap(i, j)
i++
}
}
sortable.Swap(i, hi)
return i
}
var quicksortRecursive func(sortable sort.Interface, lo, hi int)
quicksortRecursive = func(sortable sort.Interface, lo, hi int) {
if lo < hi {
p := partition(sortable, lo, hi)
quicksortRecursive(sortable, lo, p-1)
quicksortRecursive(sortable, p+1, hi)
}
}
quicksortRecursive(sortable, 0, sortable.Len()-1)
}
示例13: randLess
// Make a random comparison with certain probability of correctness
func randLess(i, j int, cmp sort.Interface, pCorrect float64) bool {
if rand.Float64() <= pCorrect {
return cmp.Less(i, j)
} else {
return !cmp.Less(i, j)
}
}
示例14: InsertionSort
// InsertionSort performs a simple insertion sort on the sort interface. In the
// case of ByDist it performs generally as fast as sort.Sort() except that it
// can exploit temporal coherence improving performance dramatically when the
// objects have not moved much.
func InsertionSort(data sort.Interface) {
for i := 0; i < data.Len(); i++ {
for j := i; j > 0 && data.Less(j, j-1); j-- {
data.Swap(j, j-1)
}
}
}
示例15: Insertion
// Insertion sort
func Insertion(a sort.Interface) {
for i := 2; i < a.Len(); i++ {
// insert a[j] into sorted slice a[0:j]
for j := i; j > 0 && a.Less(j, j-1); j-- {
a.Swap(j, j-1)
}
}
}