Python's fast-line should look like

Source: Internet
Author: User

Quick-Arrange algorithm

? Simply put a position and then put a smaller number on the left, than his large number on the right, which is clearly a recursive definition, according to this idea can easily write the fast line of code

? Quick Platoon is I learn the ACM road first let me remember the code, the impression is very deep, the previous study is Pascal, write this to write a long string, but because and merge sort is short, also back down. Curious I click on the encyclopedia to see the Python fast line code, see the following code:

#quick SortdefQuickSort (L, Low, high): I=Low J=HighifI>=J:returnL Key=L[i] whileI<J: whileI<J andL[J]>=Key:j=J-1L[i]=L[J] whileI<J andL[i]<=Key:i=I+1L[J]=L[i] L[i]=Key QuickSort (L, Low, I-1) QuickSort (L, J+1, high)returnL

After reading the above code, the mood is complex, this is exactly what I started to learn the code of Pascal, just replaced by the Python syntax to achieve, how can this reflect the python's unique Place _ (: З"∠) _

This kind of code really is not pythonic, really conforms to Pythonic 's fast line code should be like this

def  quicksort (array): span class= "CF" >if  len  (array) <  2 : return  array  #如果数组的长度为0或1当然直接返回了  el SE : Pivot =  array[0 ] less =  [ I for  i in  array[1 :] if  I <=  pivot] Greater =  [i for  I Span class= "OP" >in  array[1 :] if  i >  Pivot] return  quicksort (less) +  [pivot] +
      quicksort (Greater)  

This is the elegant Python ah (′▽ ')?

PS. If you want to reprint, please specify the source

Like please support under ~

Python's fast-line should look like

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.