Disclaimer: All sorting algorithms defined in this article define the function to sort the inputs from small to large
Symbol Explanation:
N : number of input data
Θ (n): the same order of n Infinity
First, select the sort
Def Selectsort (a): for I in range (0,len (a)-1): minindex=i for J in Range (I+1,len (a)): if a[j]<a[ Minindex]: minindex=j if Minindex!=i: Temp=a[minindex] a[minindex]=a[i] a[i]=temp Return Aa=[2,5,3,8,6,1,4,9]selectsort (a) print (a)
After you go, select the minimum number after the current position to swap with the current position
Computational complexity Theta (n^2)
Second, bubble sort
Def Bubblesort (a): K=len (a) for I in range (0,k-1): for J in Range (k-1,i,-1): if a[j]<a[j-1]:< C15/>A[J-1],A[J]=A[J],A[J-1] return Aa=[2,5,3,8,6,7,1,4,9]bubblesort (a) print (a)
Swapping adjacent non-sequential elements repeatedly from the go
Computational complexity Theta (n^2)
Third, insert sort
Def Insertsort (a): for J in Range (1,len (a)): Key=a[j] i=j-1 while (I>=0 and A[i]>key): a[i+ 1]=a[i] i=i-1 a[i+1]=keya=[2,5,3,8,6,1,4,9]insertsort (a) print (a)
From the go, the next data is inserted into an array that is already sorted in the previous sequence.
Best case: Has been ordered from small to large, computational complexity theta (N)
Worst case scenario: input order from large to small, computational complexity theta (n^2)
When the amount of data is very large, you do not have to look for a forward, can be distributed according to two, reduce the amount of computation
Iv. Merging and sorting
def mergesort (a): N=len (a) if n==1: return a mid= (n+1)//2 b=mergesort (A[0:mid]) c= MergeSort (A[mid:n]) i=0 j=0 b.append (float ("INF")) c.append (float ("INF" )) for K in range (0,n): if B[I]<C[J]: a[k]=b[i] i+=1 else: a[k]=c[j] j+=1 return aa=[ 2,5,3,8,6,7,1,4,9]mergesort (a) print (a)
Divide and conquer the strategy, first divides the sub-sequence, after merges the sort
Computational Complexity Theta (Nlog (n))
Note: Non-site operations
Five, heap sorting
def heapadjust (lst,k,n): While (2*k+1<n): j=2*k+1 if J+1<n and lst[j]<lst[j+1]: j=j+1 If LST[J]>LST[K]: lst[k],lst[j]=lst[j],lst[k] k=j else: break return lstdef heapsort (LST): N=len (LST) for I in range (n//2-1,-1,-1): lst=heapadjust (lst,i,n) for I in range (n-1,0,-1): Lst[0],lst[i]=lst[i],lst[0] lst=heapadjust (lst,0,i) return Lsta=[1,5,2,8,3,4,6,9,7]heapsort (a) Print (a)
Code Comment: http://blog.csdn.net/zhangzhengyi03539/article/details/44889951
Constructs a large root heap, and then the top element of the heap is exchanged with the last element of the sequence, the sequence length is reduced by one, and a large heap is built on the heap top element
Computational complexity Theta (NLOGN)
I think this algorithm can be improved because each heap top element is exchanged with the last element, and the last element is the element on a large heap of leaves, which is the smallest element in the branch, so that there are many steps that need to be iterated in the process of building a large root team over the top elements of the heap.
Six, quick sort
Example one:
def QuickSort (a,start,end): Key=a[start] i=start+1 j=end while i<j: while A[i]<=key and I <j: i+=1 while A[j]>key and j>i-1: j-=1 if i<j: a[i],a[j]=a[j],a[i] A[start], A[j]=a[j],a[start] if J-1>start: QuickSort (a,start,j-1) if j+1<end: QuickSort (a,j+1, End) return Aa=[1,5,2,8,3,4,6,9,7]quicksort (A,0,len (a)-1) print (a)
Example two:
def QuickSort (a,start,end): key=a[end] i=start-1 for J in Range (Start,end): if A[j]<key: i+= 1 a[i],a[j]=a[j],a[i] a[i+1],a[end]=a[end],a[i+1] if I>start: QuickSort (a,start,i) if i+2 <end: QuickSort (a,i+2,end) return Aa=[1,5,2,8,3,4,6,9,7]quicksort (A,0,len (a)-1) print (a)
Code Comment http://blog.csdn.net/zhangzhengyi03539/article/details/41180337
Divide and conquer strategy, the best case of course is two points each time
Average situation/Expected condition: Computational complexity theta (NLOGN)
Worst case scenario: Computational complexity theta (n^2)
For example two, consider a situation in which the last one of the input sequence is the largest, and then the exchange operation is performed on each of the J loops. There is a quick sort of random version, each time from the input randomly pick a number and the number of keys in exchange, and then perform a quick sort.
Sorting algorithm Summary (python)