本文整理汇总了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))
}
示例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)
}
}
示例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
}
}
}
示例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"
}
示例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"
}
示例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
}
}
}
示例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
}
示例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)
}
}
示例9: sortAndCheck
func sortAndCheck(t *testing.T, xs []int, f func([]int)) {
f(xs)
if !sort.IntsAreSorted(xs) {
t.Fatalf("failed sort")
}
}
示例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)
}
}
示例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)
}
}
}
示例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)
}
}
示例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)
}
示例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
}
示例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)
}
}