Fast sequencing of data structures

Source: Internet
Author: User

The fast line should be the most important one in the sort of data structure, including the thought of Patition, and the thought of division and treatment of the whole behind, all have great reference to solve the practical problem. Fast sorting is a method of exchanging sorts, which means that if two are the same number, the two may swap positions after the fast.

1. First look at the partition function, functions named partition (data, L, R), in Data[l,r] find the number of position I, so that I left the number is less than that number, I to the right of the number is greater than the number, and return position I

def partition (data, L, r): i = LJ = Rx = data[i]while i<j:while i<j&data[j]>x:j = 1if I<j:data[i] = data[j ]i + = 1while i<j&data[i]<x:i + 1if i<j:data[j] = data[i]j-= 1data[i] = Xreturn I

2. Then the entire array is divided into recursive operation can be

def qsort (data, L, R): If L<R:I = partition (data, L, R) qsort (data, L, i-1) qsort (data, i+1, R) def quicksort (data): Qsort (d ATA, 0, Len (data)-1)

Fast sequencing of data structures

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: 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.