Go language quicksort implementation. Fast Sorting Method Code implementation

Source: Internet
Author: User
This is a created article in which the information may have evolved or changed.
Package Mainimport ("FMT" "Math/rand" "Time") Func main () {var Z []intfor I: = 0; i < 1000000; i++ {z = append (z, Rand. INTN (100000))}sort (z)}func sort (list []int) {If Len (list) <= 1 {return//exit condition}i, J: = 0, Len (list) -1index: = 1//indicates first comparison The index location of the. Key: = List[0]//The reference value for the first comparison. Select the first number if LIST[INDEX] > key {list[i], list[j] = list[j], list[i]j--//To replace the position of the number with the end of the large value, Make the number greater than the reference value at the back of} else {list[i], list[index] = List[index], list[i]i++//represents taking a small value with the previous replacement position so that the number less than the reference value is in front index++}sort (list[:i]// Processing the array preceding the reference value sort (list[i+1:]) processing the arrays following the reference value}

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.