Python implements insert sort, bubble sort, merge sort

Source: Internet
Author: User

1 defInsertsort (A):2' Insert sort algorithm: Pass in a list, sort the numbers in list '3 Print(' Insert sort before list element order: ', A)4Length=len (A)5For I in Range (1,length): #从第二个开始6Key=a[i]7J=i-18While J>=0 and A[j]>key:9A[J+1]=A[J]TenJ=j-1 OneA[j+1]=key A Print(' Insert sorted list element order: ', A) -#插入排序时间复杂度: n^2, Spatial complexity: 1, same element remains relatively invariant (relative position unchanged) -  the defBablesort (A): -' Bubble sort algorithm: Pass in a list, sort the elements in list ' -     Print(' Order before bubble sort: ', A) -Length=len (A) +For I in Range (1,length): -#rang =range (i) +#for J in reversed (rang): AFor j in Range (i,0,-1): #range逆序遍历 at             ifA[j-1]> A[j]: -TEMP=A[J] -A[J]=A[J-1] -A[j-1]=temp -  -     Print(' Bubble sort order: ', A) in# Bubble Sort Time complexity: n^2, Spatial complexity 1, same element remains relatively invariant -  to# merge sort, pass in a list, sort the list elements + defMergeSort (alist): -     Print("splitting", Alist) the     ifLen (alist) >1: *MID = Len (alist)//2# The exact division, taking the largest integer less than or equal to the result, which is equivalent to rounding the result down $Lefthalf = Alist[:mid]Panax NotoginsengRighthalf = Alist[mid:] -  theMergeSort (lefthalf) #递归调用左半部分 +MergeSort (righthalf) #递归调用右半部分 A  the#合并过程 +I=0 -J=0 $K=0 $While I < Len (lefthalf) and J < Len (righthalf): -             ifLefthalf[i] < Righthalf[j]: -Alist[k]=lefthalf[i] theI=i+1 -             Else:WuyiALIST[K]=RIGHTHALF[J] theJ=j+1 -K=k+1 Wu  -While I < Len (lefthalf): AboutAlist[k]=lefthalf[i] $I=i+1 -K=k+1 -  -While J < Len (righthalf): AALIST[K]=RIGHTHALF[J] +J=j+1 theK=k+1 -     Print("Merging", Alist) $# merge sorting algorithm time complexity: N*LGN, Spatial complexity: N, same element preserving order invariance the  the  the if__name__== '__main__‘: thelista=[1,5,7,3,4,6,7,8,9,9,15,10,4] -Alist = [54,26,93,17,77,31,44,55,20] in#InsertSort (ListA) the#BableSort (ListA) theMergeSort (alist)

Reference: http://interactivepython.org/courselib/static/pythonds/SortSearch/TheMergeSort.html

Python implements insert sort, bubble sort, merge sort

Related Article

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.