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


Golang sort.IntsAreSorted函数代码示例

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


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

示例1: main

func main() {

	// Sort methods are specific to the builtin type;
	// here's an example for strings. Note that sorting is
	// in-place, so it changes the given slice and doesn't
	// return a new one.
	strs := []string{"c", "a", "b"}

	// We can also use `sort` to check if a slice is
	// already in sorted order.
	fmt.Println("Strings:", strs)
	fmt.Println("Sorted: ", sort.StringsAreSorted(strs))

	sort.Strings(strs)

	fmt.Println("Strings:", strs)
	fmt.Println("Sorted: ", sort.StringsAreSorted(strs))

	fmt.Println()

	// An example of sorting `int`s.
	ints := []int{7, 2, 4}

	fmt.Println("Ints:   ", ints)
	fmt.Println("Sorted: ", sort.IntsAreSorted(ints))

	sort.Ints(ints)

	fmt.Println("Ints:   ", ints)
	fmt.Println("Sorted: ", sort.IntsAreSorted(ints))
}
开发者ID:jaymecd,项目名称:go-tests,代码行数:31,代码来源:sort-1.go

示例2: TestSortableNodes

func TestSortableNodes(t *testing.T) {
	ns := []*client.Node{
		0: {CreatedIndex: 5},
		1: {CreatedIndex: 1},
		2: {CreatedIndex: 3},
		3: {CreatedIndex: 4},
	}
	// add some randomness
	for i := 0; i < 10000; i++ {
		ns = append(ns, &client.Node{CreatedIndex: uint64(rand.Int31())})
	}
	sns := sortableNodes{ns}
	sort.Sort(sns)
	cis := make([]int, 0)
	for _, n := range sns.Nodes {
		cis = append(cis, int(n.CreatedIndex))
	}
	if sort.IntsAreSorted(cis) != true {
		t.Errorf("isSorted = %v, want %v", sort.IntsAreSorted(cis), true)
	}
	cis = make([]int, 0)
	for _, n := range ns {
		cis = append(cis, int(n.CreatedIndex))
	}
	if sort.IntsAreSorted(cis) != true {
		t.Errorf("isSorted = %v, want %v", sort.IntsAreSorted(cis), true)
	}
}
开发者ID:rfrangio,项目名称:etcd-starter,代码行数:28,代码来源:discovery_test.go

示例3: main

func main() {
	rand.Seed(time.Now().UnixNano())
	var k []int
	for {
		k = rand.Perm(9)
		for i, r := range k {
			if r == 0 {
				k[i] = 9
			}
		}
		if !sort.IntsAreSorted(k) {
			break
		}
	}
	fmt.Println("Sort digits by reversing a number of digits on the left.")
	var n, score int
	for {
		fmt.Print("Digits: ", k, ". How many to reverse? ")
		i, _ := fmt.Scanln(&n)
		score++
		if i == 0 || n < 2 || n > 9 {
			fmt.Println("\n(Enter a number from 2 to 9)")
			continue
		}
		for l, r := 0, n-1; l < r; l, r = l+1, r-1 {
			k[l], k[r] = k[r], k[l]
		}
		if sort.IntsAreSorted(k) {
			fmt.Print("Digits: ", k, ".\n")
			fmt.Print("Your score: ", score, ".  Good job.\n")
			return
		}
	}
}
开发者ID:travis1230,项目名称:RosettaCodeData,代码行数:34,代码来源:number-reversal-game.go

示例4: main

func main() {
	printTracks(tracks)
	fmt.Printf("\ncustom sort\n")
	sort.Sort(customSort{tracks, func(x, y *Track) bool {
		if x.Title != y.Title {
			return x.Title < y.Title
		}
		if x.Year != y.Year {
			return x.Year < y.Year
		}
		if x.Length != y.Length {
			return x.Length < y.Length
		}
		return false
	}})

	printTracks(tracks)

	fmt.Printf("\nis sorted exercise\n")
	values := []int{3, 1, 4, 1}
	fmt.Println(sort.IntsAreSorted(values)) // "false"
	sort.Ints(values)
	fmt.Println(values)                     // "[1 1 3 4]"
	fmt.Println(sort.IntsAreSorted(values)) // "true"
	sort.Sort(sort.Reverse(sort.IntSlice(values)))
	fmt.Println(values)                     // "[4 3 1 1]"
	fmt.Println(sort.IntsAreSorted(values)) // "false"
}
开发者ID:robertkuhar,项目名称:tgpl,代码行数:28,代码来源:sorting.go

示例5: DoSort4Int

func DoSort4Int() {
	values := []int{3, 1, 4, 1}
	fmt.Println(sort.IntsAreSorted(values)) // "false"
	sort.Ints(values)
	fmt.Println(values)                     // "[1 1 3 4]"
	fmt.Println(sort.IntsAreSorted(values)) // "true"
	sort.Sort(sort.Reverse(sort.IntSlice(values)))
	fmt.Println(values)                     // "[4 3 1 1]"
	fmt.Println(sort.IntsAreSorted(values)) // "false"
}
开发者ID:elitecodegroovy,项目名称:GolangBestPractice,代码行数:10,代码来源:basic.go

示例6: TestBasic1

func TestBasic1(t *testing.T) {
	list := make([]int, 0)
	treap := &Treap{}

	for i := 0; i < count; i++ {
		k := i
		addIt := true
		for _, j := range list {
			if j == k {
				addIt = false
				break
			}
		}
		if addIt {
			treap.Insert(k)
			list = append(list, k)
		}
	}
	uniqeElements := len(list)

	traverse := treap.Traverse1()
	if !sort.IntsAreSorted(traverse) {
		t.Error("not sorted", traverse)
	}

	traverse = treap.Traverse2()
	if !sort.IntsAreSorted(traverse) {
		t.Error("not sorted", traverse)
	}

	traverse = treap.Traverse3()
	if !sort.IntsAreSorted(traverse) {
		t.Error("not sorted", traverse)
	}

	index := 0
	for i := 0; i < delNum; i++ {
		key := list[index]
		if !sort.IntsAreSorted(treap.Traverse3()) {
			t.Error("Traverse3 is not sorted.")
		}
		removed := treap.Remove(key)
		if !removed {
			t.Error("Could not remove key: ", key, " with index ", index)
			return
		}
		index += uniqeElements / delNum

		if index >= uniqeElements {
			break
		}
	}
}
开发者ID:pooya,项目名称:gods,代码行数:53,代码来源:treap_test.go

示例7: init

func init() {
	//!+ints
	values := []int{3, 1, 4, 1}
	fmt.Println(sort.IntsAreSorted(values)) // "false"
	sort.Ints(values)
	fmt.Println(values)                     // "[1 1 3 4]"
	fmt.Println(sort.IntsAreSorted(values)) // "true"
	sort.Sort(sort.Reverse(sort.IntSlice(values)))
	fmt.Println(values)                     // "[4 3 1 1]"
	fmt.Println(sort.IntsAreSorted(values)) // "false"
	//!-ints
}
开发者ID:y0k0ta19,项目名称:golang_training,代码行数:12,代码来源:main.go

示例8: TestAscByIndex

func TestAscByIndex(t *testing.T) {
	is := nestedIntSlice()
	AscByIndex(is, 2)
	if !sort.IntsAreSorted([]int{is[0][2], is[1][2], is[2][2], is[3][2]}) {
		t.Errorf("Nested int slice is not sorted by index 2 in child slices: %v", is)
	}
}
开发者ID:infor-cloud,项目名称:sortutil,代码行数:7,代码来源:all_test.go

示例9: sortAndCheck

func sortAndCheck(t *testing.T, xs []int, f func([]int)) {
	f(xs)

	if !sort.IntsAreSorted(xs) {
		t.Fatalf("failed sort")
	}
}
开发者ID:uwedeportivo,项目名称:coursera,代码行数:7,代码来源:quicksort_test.go

示例10: MergeSort

//Sort integer array in place. Function runs in n*log(n) time and n space.
func MergeSort(n []int) {
	if !sort.IntsAreSorted(n) {
		buffer := make([]int, len(n))
		copy(buffer, n)
		mergeSort(n, buffer)
	}
}
开发者ID:titorenko,项目名称:stanford-algo-course,代码行数:8,代码来源:mergeSort.go

示例11: TestParallelSorting

func TestParallelSorting(t *testing.T) {
	for _, pair := range tests() {
		ParallelSort(pair.input)
		if !sort.IntsAreSorted(pair.input) {
			t.Errorf("Expected array to be sorted, but it was not for %s example by ParallelSort", pair.name)
		}
	}
}
开发者ID:titorenko,项目名称:stanford-algo-course,代码行数:8,代码来源:inversions_test.go

示例12: TestInts

func TestInts(t *testing.T) {
	data := ints
	Sort(sort.IntSlice(data[0:]))
	if !sort.IntsAreSorted(data[0:]) {
		t.Errorf("sorted %v", ints)
		t.Errorf("   got %v", data)
	}
}
开发者ID:runningwild,项目名称:sorts,代码行数:8,代码来源:insertion_test.go

示例13: MergeSortCountingInversions

//Sort integer array in place. Function runs in n*log(n) time and n space.
//Returns number of inversions in input array
func MergeSortCountingInversions(n []int) int {
	if sort.IntsAreSorted(n) {
		return 0
	}
	buffer := make([]int, len(n))
	copy(buffer, n)
	return invMergeSort(n, buffer)
}
开发者ID:titorenko,项目名称:stanford-algo-course,代码行数:10,代码来源:inversions.go

示例14: intSlicesEqual

func intSlicesEqual(x, y []int) bool {
	if len(x) != len(y) {
		return false
	}
	if !sort.IntsAreSorted(x) {
		sort.Ints(x)
	}
	if !sort.IntsAreSorted(y) {
		sort.Ints(y)
	}
	for i := range x {
		if x[i] != y[i] {
			return false
		}
	}
	return true
}
开发者ID:RyanBinfeng,项目名称:kubernetes,代码行数:17,代码来源:servicecontroller.go

示例15: baseTest

func baseTest(values []int, t *testing.T) {
	actualValues := BubleSort(values)
	areSorted := sort.IntsAreSorted(actualValues)

	if areSorted == false {
		sort.Ints(values)
		t.Errorf("There're not sorted. Actual values: %v, expected values: %v", actualValues, values)
	}
}
开发者ID:slon1024,项目名称:algorithm,代码行数:9,代码来源:base_test.go


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