当前位置: 首页>>代码示例>>Golang>>正文


Golang sort.Interface类代码示例

本文整理汇总了Golang中sort.Interface的典型用法代码示例。如果您正苦于以下问题:Golang Interface类的具体用法?Golang Interface怎么用?Golang Interface使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Interface类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: 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
}
开发者ID:funny-falcon,项目名称:go-tree,代码行数:27,代码来源:tree_test.go

示例2: 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)
		}
	}
}
开发者ID:zhangyuchen0411,项目名称:algorithmGo,代码行数:10,代码来源:sort.go

示例3: reverse

func reverse(data sort.Interface, a, b int) {
	for a < b {
		data.Swap(a, b)
		a++
		b--
	}
}
开发者ID:d3zd3z,项目名称:euler,代码行数:7,代码来源:permutation.go

示例4: 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)
		}
	}
}
开发者ID:s-urbaniak,项目名称:golang,代码行数:7,代码来源:sort.go

示例5: heapSort

func heapSort(a sort.Interface) {
	helper := HeapHelper{a, a.Len()}
	heap.Init(&helper)
	for helper.length > 0 {
		heap.Pop(&helper)
	}
}
开发者ID:travis1230,项目名称:RosettaCodeData,代码行数:7,代码来源:sorting-algorithms-heapsort-1.go

示例6: 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)
}
开发者ID:funny-falcon,项目名称:go-tree,代码行数:37,代码来源:tree.go

示例7: 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)
	}
}
开发者ID:jesand,项目名称:crowds,代码行数:8,代码来源:cmp.go

示例8: 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)
		}
	}
}
开发者ID:jackiedong168,项目名称:sorts,代码行数:8,代码来源:qsort.go

示例9: Sort

func Sort(data sort.Interface) {
	done := make(chan bool)

	go parallelQuickSort(data, 0, data.Len()-1, done)

	<-done
}
开发者ID:CreateChance,项目名称:the-way-to-go_ZH_CN,代码行数:7,代码来源:cglsmr.go

示例10: 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)
		}
	}
}
开发者ID:CreateChance,项目名称:the-way-to-go_ZH_CN,代码行数:8,代码来源:cglsmr.go

示例11: xcopy

func xcopy(data sort.Interface, i, j, k, l int) int {
	for i < k && j < l {
		data.Swap(i, j)
		i, j = i+1, j+1
	}
	return i
}
开发者ID:Mistobaan,项目名称:set,代码行数:7,代码来源:primitives.go

示例12: 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++
	}
}
开发者ID:pombredanne,项目名称:algo,代码行数:27,代码来源:partial.go

示例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)
	}
}
开发者ID:chai2010,项目名称:permutation,代码行数:8,代码来源:permutation.go

示例14: 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)
	}
}
开发者ID:DeedleFake,项目名称:meiro,代码行数:13,代码来源:shuffle.go

示例15: 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}
}
开发者ID:kho,项目名称:easy,代码行数:8,代码来源:sort.go


注:本文中的sort.Interface类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。