本文整理汇总了Golang中sort.Interface.Len方法的典型用法代码示例。如果您正苦于以下问题:Golang Interface.Len方法的具体用法?Golang Interface.Len怎么用?Golang Interface.Len使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sort.Interface
的用法示例。
在下文中一共展示了Interface.Len方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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)
}
}
}
示例2: 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)
}
}
}
示例3: heapSort
func heapSort(a sort.Interface) {
helper := HeapHelper{a, a.Len()}
heap.Init(&helper)
for helper.length > 0 {
heap.Pop(&helper)
}
}
示例4: 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
}
示例5: 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)
}
示例6: Sort
func Sort(data sort.Interface) {
done := make(chan bool)
go parallelQuickSort(data, 0, data.Len()-1, done)
<-done
}
示例7: insertSort
func insertSort(data sort.Interface) {
r := data.Len() - 1
for i := 1; i <= r; i++ {
for j := i; j > 0 && data.Less(j, j-1); j-- {
data.Swap(j, j-1)
}
}
}
示例8: Shuffle
// Shuffle sorts data in a randomized order. It uses the default
// Source in math/rand, so if clients want to manipulate the outcome,
// they should call the appropriate functions in math/rand.
//
// TODO: Add ability to use custom Sources.
func Shuffle(data sort.Interface) {
length := data.Len()
for i := 0; i < length; i++ {
i2 := rand.Intn(i + 1)
data.Swap(i, i2)
}
}
示例9: 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)
}
}
}
示例10: NewWithIndex
func NewWithIndex(sorter sort.Interface) *WithIndex {
n := sorter.Len()
newToOld := make([]int, n)
for i := range newToOld {
newToOld[i] = i
}
return &WithIndex{sorter, newToOld}
}
示例11: Insertion
func Insertion(c sort.Interface) {
var l = c.Len()
for i := 1; i < l; i++ {
for j := i; j > 0 && c.Less(j, j-1); j-- {
c.Swap(j, j-1)
}
}
}
示例12: insertionSort
// Insertion sort
func insertionSort(data sort.Interface) {
n := data.Len()
for i := 1; i < n; i++ {
for j := i; j > 0 && data.Less(j, j-1); j-- {
data.Swap(j, j-1)
}
}
}
示例13: reverse
func reverse(seq sort.Interface, firstIndex int) {
lastIndex := seq.Len() - 1
numSwap := (lastIndex - firstIndex + 1) / 2
for i := 0; i < numSwap; i++ {
seq.Swap(firstIndex+i, lastIndex-i)
}
}
示例14: IsPalindrome
func IsPalindrome(s sort.Interface) bool {
for i, j := 0, s.Len()-1; i < j; i, j = i+1, j-1 {
if s.Less(i, j) || s.Less(j, i) {
return false
}
}
return true
}
示例15: InsertionSort
// Proposition. For randomly ordered arrays of length N with with distinct keys,
// insertion sort uses ~N2/4 compares and ~N2/4 exchanges on the average.
// The worst case is ~ N2/2 compares and ~ N2/2 exchanges and the best case is N-1 compares and 0 exchanges.
func InsertionSort(a sort.Interface) {
n := a.Len()
for i := 1; i < n; i++ {
for j := i; j > 0 && a.Less(j, j-1); j-- {
a.Swap(j, j-1)
}
}
}