# sort

In the previous map, we introduced how to traverse the map in sequence, in which the sort package is used. The sort package provides functions for sorting slices and user-defined data sets. Here are the methods commonly used in sort package.

//Sort sort data. It calls data once Len determines the length and calls O(n*log(n)) times data Less and data Swap. This function cannot guarantee the stability of sorting (that is, the relative order of equal elements is not guaranteed). func Sort(data Interface) //Stable sorts data and ensures the stability of sorting. The relative order of equal elements remains unchanged. //It calls data once Len, O(n*log(n)) times data Less and O(n*log(n)*log(n)) times data Swap. func Stable(data Interface) //IsSorted reports whether the data has been sorted. func IsSorted(data Interface) bool //Reverse wraps an Interface interface and returns a new Interface. Sorting the Interface generates a decreasing sequence. func Reverse(data Interface) Interface s := []int{5, 2, 6, 3, 1, 4} // unsorted sort.Sort(sort.Reverse(sort.IntSlice(s))) fmt.Println(s)//[6 5 4 3 2 1] //The Ints function sorts a in ascending order. func Ints(a []int) s := []int{5, 2, 6, 3, 1, 4} // unsorted sort.Ints(s) fmt.Println(s) //[1 2 3 4 5 6] //Intsareseported checks whether a has been sorted in ascending order. func IntsAreSorted(a []int) bool //SearchInts searches for X in a in ascending order and returns the index of X. If it cannot be found, the return value is where x should be inserted into a (to ensure the increasing order of a), and the return value can be len(a). func SearchInts(a []int, x int) int //The Float64s function sorts a in ascending order. func Float64s(a []float64) //Float64sareseported checks whether a has been sorted in ascending order. func Float64sAreSorted(a []float64) bool //SearchFloat64s searches for X in a in ascending order and returns the index of X. If it cannot be found, the return value is where x should be inserted into a (to ensure the increasing order of a), and the return value can be len(a). func SearchFloat64s(a []float64, x float64) int //The Strings function sorts a in ascending order. func Strings(a []string) //Stringsareseported checks whether a has been sorted in ascending order. func StringsAreSorted(a []string) bool //SearchStrings searches for X in a in ascending order and returns the index of X. If it cannot be found, the return value is where x should be inserted into a (to ensure the increasing order of a), and the return value can be len(a). func SearchStrings(a []string, x string) int

The Search function uses a dichotomy Search to find the smallest value i in the [0, n) interval that satisfies f(i)==true. That is, the Search function expects f to return false when the input is in the front part of the interval [0, n) (which can be null), and true when the input is in the rest to the end part (which can be null); the Search function will return f (i) ==The minimum value of true i. If there is no such value, the function returns n. When this is not found, the value of -1.strings is not returned Functions such as index are different. The Search function will only call f with the value in the interval [0, n).

Generally, Search is used to find the position where the value x should be inserted when inserting an ordered and indexable data structure. In this case, the parameter f (usually a closure) captures the value to be searched and the dataset to be queried.

For example, given a slice in ascending order, calling search (len (data), func (I int) bool {return data [i] > = 23}) will return the smallest index I in data, which satisfies data [i] > = 23. If the caller wants to know whether 23 is in the slice, it must also check data[i] == 23.

When searching for data in descending order, the < = operator should be used instead of the > = operator.

func Search(n int, f func(int) bool) int

The following code attempts to find the value x in an integer slice in ascending order:

x := 23 i := sort.Search(len(data), func(i int) bool { return data[i] >= x }) if i < len(data) && data[i] == x { // x is present at data[i] } else { // x is not present in data, // but i is the index where it would be inserted. }

For a more bizarre example, the following program will guess the number you hold:

func GuessingGame() { var s string fmt.Printf("Pick an integer from 0 to 100.\n") answer := sort.Search(100, func(i int) bool { fmt.Printf("Is your number <= %d? ", i) fmt.Scanf("%s", &s) return s != "" && s[0] == 'y' }) fmt.Printf("Your number is %d.\n", answer) }

### A question on leetcode

Packages on the conveyor belt must be transported from one port to another within D days.

The weight of the ith package on the conveyor belt is weights[i]. Every day, we load packages on the conveyor belt in the order of the given weight. The weight we carry will not exceed the maximum carrying weight of the ship.

Return the minimum carrying capacity of the ship that can deliver all packages on the conveyor belt within D days.

Example 1:

Input: weights = [1,2,3,4,5,6,7,8,9,10], D = 5

Output: 15

Explanation:

The minimum ship load of 15 can deliver all packages within 5 days, as follows:

Day 1: 1, 2, 3, 4, 5

Day 2: 6, 7

Day 3: 8

Day 4: 9

Day 5: 10

Please note that the goods must be shipped in the given order, so it is not allowed to use a ship with a load capacity of 14 and divide the packaging into (2, 3, 4, 5), (1, 6, 7), (8), (9), (10).

func shipWithinDays(weights []int, D int) int { // Determine the left and right boundaries of binary search left, right := 0, 0 for _, w := range weights { if w > left { left = w } right += w } return left + sort.Search(right-left, func(x int) bool { x += left day := 1 // Number of days to ship sum := 0 // The sum of the weight of the packages delivered on the current day for _, w := range weights { if sum+w > x { day++ sum = 0 } sum += w } return day <= D }) }