First come to the blog park, come to two sort algorithm hot warm-up (Golang bubble sort and quick sort)

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

The first time to come to the blog park, belong to the relatively late, after graduation has been engaged in the development of 4 years, learned something, but found that need to learn more things. First two basic sort algorithm hot warm-up

I. Bubble sort, move backwards, from small to large sort, its thought for the adjacent two numbers to compare, will be larger lag, time complexity O (n^2)

Package Mainimport (    "FMT") func main () {    array: = [11]int{56,45,9,16,2,89,78,34,102,56,99}     FMT. Print ("Pre-order:")    FMT. Println (Array)    length: = Len (array) for    I: = 0, i < length, i++ {for        J: = 0, J < Length-1-i, J + + {            If ARRAY[J] > array[j+1] {                Temp: = Array[j]                array[j] = array[j+1]                array[j+1] = temp            }        }    }    FMT. Print ("Post-order:")    FMT. Println (Array)}

Two. Quick sorting, from the back to find a minimum placed in front of the order from small to large, time complexity O (n^2)

Package Mainimport (    "FMT") func main () {    //quick sort, from behind to find a smallest placed in front of the position, from small to large sort, time complexity O (n^2)     array: = [11]int{ 56,45,9,16,2,89,78,34,102,56,99}     length:= len (array)    temp,position: = 0,0     fmt. Print ("Pre-order:")    FMT. PRINTLN (array) for    I: = 0, i < length-1; i++ {        position = I for        j: = i+1; J < length; J + + {            if Arra Y[J] < array[position] {                position = J            }        }        //Replace the current I position and position with        temp = array[i]        array [i] = Array[position]        array[position] = temp    }    fmt. Print ("Post-order:")    FMT. Println (Array)}

  

Related Article

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.