Golang sort package used, and three simple sorting algorithms bubbling, inserting, selecting exercises

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The core type of the sort package is sort. Interface:

Type Interface Interface {//Len is the number of elements in the collection.    Len () int/Less reports whether the element with//index I should sort before the element with index J.    Less (i, J int) bool//Swap swaps the elements with indexes I and J. Swap (i, J int)}

Interface is a cool feature of Golang, Rob Pike said the interface is a bit like uinx pipe, the different code fragments are connected together, this contract is the interface specification,

Provides a collection of specified features, regardless of your internal implementation. var s sort. Interface, S is an abstract interface type, the specific type U provided Len (), less (), Swap (), s = u, you can assign the U-type variable s, also reflected in the function is S, the return value is the place of S.

Sort. Ints, sort. float64s, sort. Strings is a handy package for libraries, such as sort. Ints is implemented in this way:

Func Ints (A []int) {Sort (Intslice (a))}
Type Intslice []int func (P intslice) len () int {return Len (p)} func (P intslice) Less (i, J int) bool { return p[i] < P[j]} func (P intslice) Swap (i, J int) {P[i], p[j] = P[j], P[i]}

Call sort internally. Sort (), the parameter of the function is the abstract sort. Interface type, []int type does not implement Len (), Less (), Swap (), and its alias type

Intslice implemented, so Intslice (a), type conversions to meet the interface. For reverse ordering there is a clever interface to use:

Type reverse struct { Interface }

Func (R reverse) Less (i, J int) bool {

return r.interface.less (J, i)

}

Func Reverse (data Interface) Interface {

return &reverse{data}

}

The Golang language specification is described as follows:

Given A struct type S And a type named T , promoted methods is included in the method set of the struct as follows:

    • If S contains an anonymous field T , the method sets of S and *S both include promoted methods with receiver T. The method set of *S also includes promoted methods with receiver *T .

The method set of a interface type is it interface.

The reverse struct type is S, Interface is T, reverse contains the anonymous field Interface, Interface is the interface type, its method set is itself, Len (), Swap (), less () is promoted to revers E, while reverse also defines its own less () (internal reference is an implementation of the embedded field, a rollover comparison), the reverse type has the old Len (), swap () implementation, and the new less () implementation, so the sort is satisfied. Interface, Reverse's pointer type contains reverse's method set, so

The Export function reverse () returns the reverse pointer to satisfy the interface interface.

package mainimport  (     "FMT"      "sort") func   Bubblesort (Data  sort. Interface) {    r := data. Len () -1    for i := 0; i < r ; i++{         for j := r; j > i; j--{             if data. Less (j, j-1) {                 data. Swap (j, j-1)             }         }    }}func insertsort (Data sort. Interface) {    r := data. Len () -1    for i := 1; i <= r; i++{          For j := i; j > 0 && data. Less (j, j-1);  j--{            data. Swap (j, j-1)         }    }}func   Selectsort (Data sort. Interface) {    r := data. Len () -1    for i := 0; i < r; i++{         min := i        for j:=  i+1; j <= r; j++ {             if data. Less (J, min)  {  min = j }        }         data. Swap (I, min)     }}func main () {    nums := []int{ 120, 33,44,1,23,90,87,13,57,43,42}    //Standard library qsort  Positive order (actually qsort combined Heapsort,insertsort)      sort. Ints (nums)     fmt. Println (nums)     //reverse order Qsort    sort. Sort (sort. Reverse (sort. Intslice (nums)))     fmt. Println (nums)     //positive sequence bubble     bubblesort (sort. Intslice (nums))     fmt. Println (nums)     //reverse order bubble    bubblesort (sort. Reverse (sort. Intslice (nums)))     fmt. Println (nums)     //positive sequence insert    insertsort (sort. Intslice (nums))     fmt. Println (nums)     //reverse order inert    insertsort (sort. Reverse (sort. Intslice (nums)))     fmt. Println (nums)     //positive sequence select    selectsort (sort. Intslice (nums))     fmt. PrinTLN (nums)      //reverse order select     selectsort (sort. Reverse (sort. Intslice (nums)))     fmt. Println (Nums)}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.