Esempio n. 1
0
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))
}
Esempio n. 2
0
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)
	}
}
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
		}
	}
}
Esempio n. 4
0
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"
}
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"
}
Esempio n. 6
0
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
		}
	}
}
Esempio n. 7
0
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
}
Esempio n. 8
0
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)
	}
}
Esempio n. 9
0
func sortAndCheck(t *testing.T, xs []int, f func([]int)) {
	f(xs)

	if !sort.IntsAreSorted(xs) {
		t.Fatalf("failed sort")
	}
}
Esempio n. 10
0
//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)
	}
}
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)
		}
	}
}
Esempio n. 12
0
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)
	}
}
Esempio n. 13
0
//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)
}
Esempio n. 14
0
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
}
Esempio n. 15
0
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)
	}
}
Esempio n. 16
0
func (sint *SliceInt) Max() (int, error) {
	if len(sint.Elements) == 0 {
		return 0, errors.New("List is empty")
	}
	if !sort.IntsAreSorted(sint.Elements) {
		sort.Ints(sint.Elements)
	}
	return sint.Elements[len(sint.Elements)-1], nil
}
Esempio n. 17
0
func TestSort(t *testing.T) {
	data := make([]int, 50)
	for i := range data {
		data[i] = rand.Int() % 50
	}
	treesort.Sort(data)
	if !sort.IntsAreSorted(data) {
		t.Errorf("not sorted: %s", data)
	}
}
Esempio n. 18
0
func (sint *SliceInt) Median() (int, error) {
	if len(sint.Elements) == 0 {
		return 0, errors.New("List is empty")
	}
	if !sort.IntsAreSorted(sint.Elements) {
		sort.Ints(sint.Elements)
	}
	mid := int64(float64(len(sint.Elements)) / 2)
	return sint.Elements[mid], nil
}
Esempio n. 19
0
func main() {
	for _, u := range tests {
		list := make([]int, len(u))
		//fmt.Println("Unsorted:", u)
		for _, asort := range sorts {
			copy(list, u)
			asort.fn(list)
			//fmt.Println(asort.name, " sorted list:", list)
			if !sort.IntsAreSorted(list) {
				fmt.Println("FAILED! list=", list)
				os.Exit(-1)
			}
		}
	}
	fmt.Println("PASSED")
	times := 50000
	for _, asort := range sorts {
		t := time.Now()
		for i := 0; i < times; i++ {
			for _, u := range tests {
				list := make([]int, len(u))
				copy(list, u)
				asort.fn(list)
			}
		}
		fmt.Println(asort.name, "sorted", times, "times in", time.Since(t))
	}
	fmt.Println("Strings tests")
	teststr := convert(tests)
	for _, u := range teststr {
		list := make([]string, len(u))
		//fmt.Println("Unsorted:", u)
		for _, asort := range sortstr {
			copy(list, u)
			asort.fn(list)
			//fmt.Println(asort.name, " sorted list:", list)
			if !sort.StringsAreSorted(list) {
				fmt.Println("FAILED!")
				os.Exit(-1)
			}
		}
	}
	fmt.Println("PASSED")
	for _, asort := range sortstr {
		t := time.Now()
		for i := 0; i < times; i++ {
			for _, u := range teststr {
				list := make([]string, len(u))
				copy(list, u)
				asort.fn(list)
			}
		}
		fmt.Println(asort.name, " sorted", times, "times in", time.Since(t))
	}
}
Esempio n. 20
0
func TestSearchInts(t *testing.T) {
	for _, test := range testData {
		if !sort.IntsAreSorted(test.slice) {
			t.Skip("Invalid test data")
		}
		if x := SearchInts(test.slice, test.key); x != test.x {
			t.Fatalf("SearchInts(%v, %d) = %d, want %d",
				test.slice, test.key, x, test.x)
		}
	}
}
Esempio n. 21
0
func main() {
	data := make([]int, 50)
	for i := range data {
		data[i] = rand.Int() % 50
	}
	Sort(data)

	if !sort.IntsAreSorted(data) {
		fmt.Println("not sorted: %v", data)
	}
}
Esempio n. 22
0
func internalSort() {
	strs := []string{"c", "a", "b"}
	sort.Strings(strs) // inplace
	fmt.Println(strs)

	ints := []int{4, 2, 19, 22, 11}
	sort.Ints(ints)
	fmt.Println(ints)

	fmt.Println(sort.IntsAreSorted(ints))
}
Esempio n. 23
0
// Did you get it?  Did you cut and paste from the standard library?
// Whatever.  Now show you can work it.
//
// The test program will supply slices and keys and you will write a function
// that searches and returns one of the following messages:
//
//   k found at beginning of slice.
//   k found at end of slice.
//   k found at index fx.
//   k < all values.
//   k > all n values.
//   k > lv at lx, < gv at gx.
//   slice has no values.
//
// In your messages, substitute appropritate values for k, lv, and gv;
// substitute indexes for fx, lx, and gx; substitute a number for n.
//
// In the function Message you will demonstrate a number of different ways
// to test the result of SearchInts.  Note that you would probably never need
// all of these different tests in a real program.  The point of the exercise
// is just to show that it is possible to identify a number of different
// conditions from the return value.
func TestMessage(t *testing.T) {
	for _, test := range testData {
		if !sort.IntsAreSorted(test.slice) {
			t.Skip("Invalid test data")
		}
		if res := Message(test.slice, test.key); res != test.ref {
			t.Fatalf("Message(%v, %d) =\n%q\nwant:\n%q",
				test.slice, test.key, res, test.ref)
		}
	}
}
Esempio n. 24
0
func TestSortLarge_Random(t *testing.T) {
	n := 10000
	if testing.Short() {
		n /= 100
	}
	data := make([]int, n)
	for i := 0; i < len(data); i++ {
		data[i] = rand.Intn(100)
	}
	if sort.IntsAreSorted(data) {
		t.Fatalf("terrible rand.rand")
	}

	for _, alg := range algorithms {
		sorting.Ints(data, alg)
		if !sort.IntsAreSorted(data) {
			t.Errorf("%v failed to sort 10K ints\n", util.GetFuncName(alg))
		}
	}
}
Esempio n. 25
0
File: set.go Progetto: hankji/dnf
func (set *IntSet) ToSlice() []int {
	set.RLock()
	rc := make([]int, 0, len(set.data))
	for k, _ := range set.data {
		rc = append(rc, k)
	}
	set.RLock()
	if !sort.IntsAreSorted(rc) {
		sort.IntSlice(rc).Sort()
	}
	return rc
}
Esempio n. 26
0
func DemoSort() {
	strs := []string{"qww", "afa", "q", "aaa"}
	sort.Strings(strs)
	fmt.Println("Sorted strings: ", strs)

	ints := []int{2, 5, 3, 7, 1, 8}
	sort.Ints(ints)
	fmt.Println("Sorted ints: ", ints)

	is_sorted := sort.IntsAreSorted(ints)
	fmt.Println("Is Sorted? ", is_sorted)
}
Esempio n. 27
0
func test(t *testing.T, algorithm func([]int)) {
	list := []int{5, 3, 4, 1, 7, 2, 3, 8, 5, 3, 8, 9, 0, 6, 4}
	algorithm(list)

	if !sort.IntsAreSorted(list) {
		sortedList := make([]int, len(list))
		copy(sortedList, list)
		sort.Ints(sortedList)

		t.Fatalf("%v != %v ", list, sortedList)
	}
}
func main() {
	strs := []string{"c", "a", "b"}
	fmt.Println("Strings before Sorting", strs)
	sort.Strings(strs)
	fmt.Println("Strings after sorting is:", strs)
	ints := []int{7, 2, 4}
	fmt.Println("integers before Sorting", ints)
	sort.Ints(ints)
	fmt.Println("Integers after sorting:   ", ints)
	s := sort.IntsAreSorted(ints)
	fmt.Println("Sorted: ", s)
}
Esempio n. 29
0
func TestInts(t *testing.T) {
	data := ints

	for _, alg := range algorithms {
		sorting.Ints(data[:], alg)
		if !sort.IntsAreSorted(data[:]) {
			t.Errorf("%v\n", util.GetFuncName(alg))
			t.Errorf("sorted: %v\n", ints)
			t.Errorf("   got: %v\n", data)
		}
	}
}
Esempio n. 30
0
func main() {
	strs := []string{"c", "a", "b"}
	sort.Strings(strs)
	fmt.Println("strings:", strs)

	ints := []int{7, 2, 4}
	sort.Ints(ints)
	fmt.Println("Ints:", ints)

	s := sort.IntsAreSorted(ints)
	fmt.Println("Sorted: ", s)
}