сортировка

This commit is contained in:
badkaktus 2019-10-08 21:49:56 +03:00
parent 80e08633e1
commit ee38f601f3
3 changed files with 14 additions and 13 deletions

View File

@ -37,7 +37,7 @@ WaitGroups
Атомарные счетчики (Atomic Counters) Атомарные счетчики (Atomic Counters)
Мьютексы (Mutexes) Мьютексы (Mutexes)
Управление состоянием горутин (Stateful Goroutines) Управление состоянием горутин (Stateful Goroutines)
Sorting Сортировка (Sorting)
Sorting by Functions Sorting by Functions
Panic Panic
Defer Defer

View File

@ -1,6 +1,6 @@
// Go's `sort` package implements sorting for builtins // Пакет `sort` реализует сортировку для встроенных и
// and user-defined types. We'll look at sorting for // пользовательских типов. Сначала рассмотрим сортировку
// builtins first. // встроенных типов.
package main package main
@ -11,21 +11,21 @@ import (
func 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"} strs := []string{"c", "a", "b"}
sort.Strings(strs) sort.Strings(strs)
fmt.Println("Strings:", strs) fmt.Println("Strings:", strs)
// An example of sorting `int`s. // Пример сортировки `int`'ов
ints := []int{7, 2, 4} ints := []int{7, 2, 4}
sort.Ints(ints) sort.Ints(ints)
fmt.Println("Ints: ", ints) fmt.Println("Ints: ", ints)
// We can also use `sort` to check if a slice is // Мы так же можем использовать `sort`, для
// already in sorted order. // проверки, что срез был уже отсортирован.
s := sort.IntsAreSorted(ints) s := sort.IntsAreSorted(ints)
fmt.Println("Sorted: ", s) fmt.Println("Sorted: ", s)
} }

View File

@ -1,5 +1,6 @@
# Running our program prints the sorted string and int # После запуска наша программа выведет отсортированные
# slices and `true` as the result of our `AreSorted` test. # строки и срез целых чисел и `true`, как результат
# выполнения `AreSorted`.
$ go run sorting.go $ go run sorting.go
Strings: [a b c] Strings: [a b c]
Ints: [2 4 7] Ints: [2 4 7]