python--sort

Source: Internet
Author: User
Tags shuffle

Bubble sort

#!/usr/bin/env python#-*-coding:utf-8-*-#Author:love_catdefSift (Li,low,high): I=Low J= 2 * i + 1 whileJ <=High :ifJ+1 <= High andLI[J] < li[j+1]: J+ = 1ifLI[J] >Li[i]: li[j],li[i]=Li[i],li[j] I=J J= 2 * i + 1Else:             BreakdefHeap_sort (LI): n=Len (LI) forIinchRange (n//2-1,-1,-1): Sift (li,i,n-1)     forIinchRange (n-1,-1,-1): Li[0],li[i]=Li[i],li[0] Sift (li,0,i-1)ImportRandomli= List (range (30)) Random.shuffle (LI)Print(LI) heap_sort (LI)Print(LI)#Output Results" "[7, 27, 9, 18, 20, 13, 6, 23, 3, 12, 5, 11, 2, 1, 28, 29, 0, 17, 26, 14, 8, 10, 4, 16, 15, 21, 22, 24, 19, 25][0, 1, 2 , 3, 4, 5, 6, 7, 8, 9, ten, one, A, A, (+), A, (+), (+), (+), +" "

Select sort

#!/usr/bin/env python#-*-coding:utf-8-*-#Author:love_catImportRandomImport TimedefSelect_sort (LI): forIinchRange (Len (LI)-1): Min_index=I forJinchRange (i+1, Len (LI)):ifLI[J] <Li[min_index]: Li[j],li[min_index]=Li[min_index],li[j]li= List (range (30)) Random.shuffle (LI)Print(LI) Start=time.time () Select_sort (LI)Print('Time :', Time.time ()-start)Print(LI)#Output Results" "[8, +, 9, 2, 0, 6, 1, ten,------------------"3", "+", "7", "4", "+", "+", "+", "+", "+, 5 0[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ten, one, A, A, (+), (+), (+), (+), +" "

Quick Sort

#!/usr/bin/env python#-*-coding:utf-8-*-#Author:love_catdefQuick_sort (li,left,right):ifLeft <Right:mid=partition (Li,left,right) quick_sort (Li,left,mid-1) Quick_sort (Li,mid+1, right)defpartition (li,left,right): Temp=Li[left] whileLeft <Right : whileLeft < Right andLi[right] >=Temp:right-= 1Li[left]=Li[right] whileLeft < Right andLi[left] <=Temp:left+ = 1Li[right]=Li[left] Li[left]=TempreturnLeftli= List (range (30))ImportRandomrandom.shuffle (LI)Print(LI) quick_sort (Li,0,len (LI)-1)Print(LI)#Output Results" "[0, 16, 21, 24, 2, 22, 19, 10, 5, 28, 25, 13, 18, 26, 29, 9, 23, 14, 7, 3, 17, 8, 11, 15, 6, 1, 4, 27, 20, 12][0, 1, 2 , 3, 4, 5, 6, 7, 8, 9, ten, one, A, A, (+), A, (+), (+), (+), +" "

Merge sort

#!/usr/bin/env python#-*-coding:utf-8-*-#Author:love_catdefmerge (Li,low,mid,high): I=Low J= Mid+1ltemp= []     whileI <= mid andJ <=High :ifLi[i] <Li[j]: Ltemp.append (li[i]) I+ = 1Else: Ltemp.append (Li[j]) J+ = 1 whileI <=mid:ltemp.append (li[i]) I+ = 1 whileJ <=high:ltemp.append (Li[j]) J+ = 1Li[low:high+1] =ltempdefMerge_sort (li,low,high):ifLow <High:mid= (low + high)//2Merge_sort (Li,low,mid) merge_sort (Li,mid+1, High) merge (Li,low,mid,high) Li= List (range (30))ImportRandomrandom.shuffle (LI)Print(LI) merge_sort (li, 0, Len (LI)-1)Print(LI)#Output Results" "[29, 27, 22, 5, 10, 8, 16, 25, 0, 20, 1, 14, 12, 28, 11, 19, 7, 4, 3, 18, 15, 6, 26, 17, 21, 9, 13, 24, 23, 2][0, 1, 2 , 3, 4, 5, 6, 7, 8, 9, ten, one, A, A, (+), A, (+), (+), (+), +" "

Insert Sort

#!/usr/bin/env python#-*-coding:utf-8-*-#Author:love_catImportRandomdefInsert_sort (LI): forIinchRange (Len (LI)-1): J= i + 1 whileJ >0 andLi[j]<li[j-1]: Li[j],li[j-1] = li[j-1],li[j] J-= 1bin= List (range (30)) Random.shuffle (LI)Print(LI) insert_sort (LI)Print(LI)#Output Results" "[6, 9, 0, 26, 21, 7, 1, 11, 4, 10, 14, 20, 29, 5, 8, 3, 23, 18, 17, 12, 24, 27, 25, 15, 2, 13, 22, 16, 28, 19][0, 1, 2 , 3, 4, 5, 6, 7, 8, 9, ten, one, A, A, (+), A, (+), (+), (+), +" "

Heap Sort

#!/usr/bin/env python#-*-coding:utf-8-*-#Author:love_catdefSift (Li,low,high): I=Low J= 2 * i + 1 whileJ <=High :ifJ+1 <= High andLI[J] < li[j+1]: J+ = 1ifLI[J] >Li[i]: li[j],li[i]=Li[i],li[j] I=J J= 2 * i + 1Else:             BreakdefHeap_sort (LI): n=Len (LI) forIinchRange (n//2-1,-1,-1): Sift (li,i,n-1)     forIinchRange (n-1,-1,-1): Li[0],li[i]=Li[i],li[0] Sift (li,0,i-1)ImportRandomli= List (range (30)) Random.shuffle (LI)Print(LI) heap_sort (LI)Print(LI)#Output Results" "[7, 27, 9, 18, 20, 13, 6, 23, 3, 12, 5, 11, 2, 1, 28, 29, 0, 17, 26, 14, 8, 10, 4, 16, 15, 21, 22, 24, 19, 25][0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ten, one, ten, , (+), (+), (+), (+), +" "

python--sort

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.