Basic Sorting algorithm (Golang implementation)

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

Bubble sort

Func bubblesort (Vector []int)  {    fmt. Println ("Bubblesort")     fmt. Println (vector)     for i := 0; i < len (vector);  i++  {        tag := true //  for Pruning          //  Each trip will be the largest number of bubbles         for  j := 0; j < len (vector)-i-1; j++ {      &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;IF&NBSP;VECTOR[J]&NBSP;&GT;&NBSP;VECTOR[J+1]&NBSP;{&NBSP;/*VECTOR[J]  < vector[j+1]*/                 temp := vector[j]                 vector[j] = vector[j+1]                 vector[j+1] = temp                 tag = false             }        }         if tag {             break //0~len (vector)-I did not happen exchange instructions have been ordered         }         fmt. Println (vector)     }}

Insert Sort

Func insertsort (Vector []int)  {    fmt. Println ("Insertsort")     fmt. Println (vector)     for i := 1; i < len (vector);  i++  {        //  each trip does not meet the conditions to choose I for Sentinel preservation, the Sentinel inserted 0~i-1 ordered sequence (0~i-1 is always orderly)         if vector[i] < vector[i-1] { /* vector[i] > vector[i-1]*/             temp := vector[i]            // Move back until you find the right position for the sentry             j := i -  1            for ; j >=  0 && vector[j] > temp; j-- { /*vector[j] <  temp*/                vector[j+1] =  vector[j]            }             //insertion position before and after is orderly, and finally orderly              vector[j+1] = temp         }        fmt. Println (vector)     }}

Select sort

Func selectsort (Vector []int)  {    fmt. Println ("Selectsort")     fmt. Println (vector)     for i := 0; i < len (vector);  i++  {        //  Select the smallest element          k := i        for j := i +  1; j < len (vector); j++ {             if vector[k] > vector[j] {                 k = j             }        }         //  Exchange         if k !=  i {            temp := vector[i]             vector[i] = vector[k]             vector[k] = temp         }        fmt. Println (vector)     }}

Binary selection sort

Func binaryselectsort (Vector []int)  {    fmt. Println ("Selectsort")     fmt. Println (vector)     n := len (vector)     for i :=  0; i < n/2; i++ {        //  Select the smallest element and the largest element         k := i         t := n - i - 1         for j := i + 1; j <= n-i-1; j++ {             if vector[k] > vector[j] {                 k = j             }             if vector[t] < vector[j] {                 t = j             }        }         //  Exchange         if k !=  i {            temp := vector [I]            vector[i] = vector[k]             vector[k] = temp         }        if t !=  n-i-1 {            temp := vector[ n-i-1]            vector[n-i-1] = vector[t]             vector[t] = temp         }        fmt. Println (vector)     }}


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.