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