Python Common sorting algorithm

Source: Internet
Author: User

#快排
1
defQ_sort (L):2left =03right = Len (l)-14 returnQ (l,left,right)5 6 defQuick_sort (l,left,right):7 ifLeft >=Right :8 returnL9Low = LeftTenHigh = Right One whileRight>Left : A whileRight>left andL[right] >=L[left]: -Right-=1 -L[right],l[left] =L[left],l[right] the whileRight>left andL[right] >=L[left]: -Left + = 1 -L[right],l[left] =L[left],l[right] -Q (l,low,left-1) +Q (l,left+1, High) - returnL

#堆排序
1
defHeap_sort (L):2n = Len (l)#n-1 last node label3First = (n/2-1)#last non-leaf node4 forStartinchRange (first,-1,-1): 5Max_heap (l,start,n-1)6 7 forEndinchRange (n-1,0,-1):8L[end],l[0] =L[0],l[end]9Max_heap (l,0,end-1)#Remove the last node label forward refactoringTen returnL One A - defmax_heap (l,start,end): -Root =Start the whileTrue: -Child = root*2 + 1 - ifChild > End: Break - ifChild+1 <= End andL[child] < l[child+1]: +Child + = 1 - ifL[root] <L[child]: +L[root],l[child] =L[child],l[root] ARoot = Child at Else: - Break
#堆排序取前100大
1
defHeap_sort (L):2n =Len (l)3First = n/2-1#last non-leaf node4 forStartinchRange (first,-1,-1):5Min_heap (l,start,n-1)6 7 forEndinchRange (n-1,0,-1):8L[end], l[0] =l[0], L[end]9Min_heap (l,0,end-1)Ten returnL One A - defmin_heap (l,start,end): -Root =Start the while1: -Child = Root*2+1 - ifChild > End: Break - ifChild+1 <= End andL[child] > L[child+1]: +Child+=1 - ifL[root] >L[child]: +L[root],l[child] =L[child],l[root] ARoot = Child at Else: - Break - - defqu (x): #x为传入数列, Z is the list of the top 100 outputs -z = Heap_sort (x[:100]) - Print 'Z:', Z in forIinchx[100:]: - ifi > z[99]: toz[99]=I + Heap_sort (z) - returnZ


#归并排序
1
defMerge_sort (L):2 ifLen (l) <= 1:3 returnL4num = Len (l)/25left =Merge_sort (L[:num])6right =Merge_sort (l[num:])7 returnmerge (Left,right)8 9 defmerge (left,right):Teni=j=0 OneR=[] A whileI<len (left) andj<Len (right): - ifLeft[i] <=Right[j]: - r.append (Left[i]) theI+=1 - Else: - r.append (Right[j]) -J+=1 +R + =Left[i:] -R + =Right[j:] + returnR

 1  #   bubbling  2  def   Maopao (seq):  3  for  i in
      Range (0,len (seq) -1 4  for  J in  range (0,len (SEQ)-1- i):  5  if
        Seq[j] > Seq[j+1]:  6  seq[j ],seq[j+1]=seq[j+1],seq[j]  7  return  seq 
1 #Insert2 defDirectinsert (seq):3      forIinchRange (1, Len (seq)):4Temp, J =Seq[i], I5          whileJ>0 andTemp < Seq[j-1]:6SEQ[J] = seq[j-1]7j = J-18SEQ[J] =Temp9     returnseqTen  One #Insert (start from middle) A defDI (seq): -      forIinchRange (1, Len (seq)): -Temp, j,k = Seq[i], I, I/2 the         ifTemp <Seq[k]: -              whileK>0 andTemp < Seq[k-1]: -Seq[k] = seq[k-1] -K = K-1 +SEQ[K] =Temp -         Else: +              whileJ>0 andTemp < Seq[j-1]: ASEQ[J] = seq[j-1] atj = J-1 -SEQ[J] =Temp -     returnSeq
1 #Direct Select Sort2 defSelect_sort (L):3      forIinchRange (0,len (l)):4Min =I5          forJinchRange (i+1, Len (l)):6             ifL[min] >L[j]:7Min =J8L[min],l[i] =L[i],l[min]9     returnL

Reference: http://www.cnblogs.com/jztan/p/5878630.html

Python Common sorting algorithm

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.