Golang for fast Sorting

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

The principle of quick sorting is not introduced. See an interesting video on the web and you can look at it, very detailed and interesting.
Quick Sort Videos
Code: HTTPS://PLAY.GOLANG.ORG/P/FW5GTZRPJ0

Package Mainimport ("FMT") func main () {var sortarray = []int{3, $, 3, 3, A, a, a, a, a, A/V} f Mt. Println (Sortarray) qsort (sortarray, 0, Len (sortarray)-1) fmt.        Println (Sortarray)}func qsort (array []int, low, high int) {if low < high {m: = partition (array, low, high) Fmt. Println (m) qsort (array, Low, m-1) qsort (array, m+1, high)}}func partition (array []int, low, high int) in t {key: = Array[low] Tmplow: = Low Tmphigh: = high for {//finds an element less than or equal to key, the position of the element must be between Tmplow and high because the array [Tmplow] and left element less than or equal to key, will not cross for Array[tmphigh] > key {tmphigh--}//Find an element greater than key, the position of the element must be Low to tmphigh+1.         Because array[tmphigh+1] must be greater than key for Array[tmplow] <= key && Tmplow < Tmphigh {tmplow++} If Tmplow >= tmphigh {break}//Swap (Array[tmplow], Array[tmphigh]) array[tmp Low], Array[tmphigh] = Array[tmphigh], array[tMplow] FMT. Println (Array)} Array[tmplow], array[low] = Array[low], Array[tmplow] return Tmplow}
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.