Outline eight sorting algorithms Needless to say, the basic programmer algorithm must be mastered, and now summarize the deepening of memory. is the classification, name, time space complexity, and stability of the eight sorting algorithms.
Code below is the classic eight sorting algorithm Java and Python code, are based on the classic algorithm book "Introduction to the algorithm" pseudo-code implementation, I attached a note in the Key statement section. The implementation of the eight-sorting algorithm (ascending), preceded by Java, followed by Python, is described in the order in. The sort function for Java is written in a class, and the Python sort function is written directly. Direct Insert Sort
public class Insertsort {//Insert sort, best case O (n), worst case O (n^2), stable original sort public int[] sort (int[] arr) {for (int i=0;i<arr.length;i+ +) {int key = Arr[i];int J = i-1;for (; j>=0 && arr[j]>key;j--) arr[j+1] = arr[j];//Note! Here is not the exchange, but the larger than the key to move back arr[j+1] = Key;//key and then insert the appropriate position}return arr;}
def insertsort (arr): #插入排序, the worst is O (n^2), preferably O (n) #升序排列一个数组, in descending order change the while statement to Arr[i]<key if (arr==none or Len (arr) <2): return arr for J in Range (1,len (arr)): key = arr[j] #待插入的数 i = j-1 while (i>=0 and Arr[i]>key): #从后往前比较待插入的数和当前数, will arr[j-1], arr[j-2] ... Move right until you find the appropriate location for ARR[J] arr[i+1] = arr[i] I-= 1 arr[i+1] = key# [Arr[j] is inserted at that location return arr
Hill sort
public Class Shellsort {//Hill sort, best case O (n), worst case O (n^2), average case is better than direct insert sort, average condition is O (n^1.3)//unstable, original sort public int[] sort (int[] arr) {int n = arr.length;for (int gap = n; gap > 0; gap/=2) {for (int i = Gap;i < n;i++) {if (Arr[i] < Arr[i-gap]) {//if smaller than the previous element, then Insert sort with step gap int key = Arr[i];int J = i-gap;while (j>=0 && arr[j] > key) {arr[j+gap] = arr[j];j-= gap;} ARR[J+GAP] = key;}}} return arr;}}
def shellsort (arr): #改进版的插入排序-Hill sort, worst O (n^2), preferably O (n), average O (n^1.3) gap = Len (arr)/2 while gap > 0: For I in range (Gap,len (arr)): if arr[i] < Arr[i-gap]: #后面的元素比前面的小, insert sort with gap for step key = Arr[i] j = i-gap
while j>=0 and Arr[j]>key: arr[j+gap] = arr[j] J-= Gap Arr[j+gap] = key #步长减一半 Gap/= 2
return arr
Direct Select sort
public class Selectsort {//select sort, worst and best are O (n^2), unstable, original sort public int[] sort (int[] arr) {int n = arr.length;if (n<2) return a rr;for (int i=0;i<n-1;i++) {int index = i;for (int j=i+1;j<n;j++) {if (Arr[j]<arr[index]) {index = j;// Find the subscript for the smallest of the remaining elements}}//the smallest number in the remaining element with the current number in exchange int tmp = Arr[i];arr[i] = Arr[index];arr[index] = tmp;} return arr;}}
def selectsort (arr): #选择排序, the worst is O (n^2) #升序排列一个数组, in descending order change the while statement to Arr[i]<key if (Arr==none or Len (arr ) <2): return arr for J in Range (Len (arr)): index = j for I in range (J,len (arr)): if arr[i] < arr [index]: index = i# Find the remaining minimum element #未排序中最小的元素跟arr [j] Interchange TMP = Arr[j] arr[j] = Arr[index] Arr[index] = TMP return arr
Heap Sort
public class Heapsort {//maintains a maximum heap, making it the element of the I node as root public void maxheapify (int[] arr,int i,int heap_size) {int L = 2*i+1;//left child node in T r = L+1;int largest = i;if (Ldef maxheapify (arr,i,heap_size): #维护一个最大堆, let Arr[i] value in the maximum heap down, so that I is the root node subtree is the largest heap, O (LGN) L = 2*i+1# subscript I starting from 0, so the left child node subscript is the I+1 r = L + # right child node largest = i if L
Bubble sortpublic class Popsort {public int[] Sort (int[] arr) {int n = arr.length;if (n<2) return arr;for (Int. i=0;i<n;i++) for (int j=n-1;j>i;j--) {if (arr[j-1]>arr[j]) {int tmp = Arr[j];arr[j] = arr[j-1];arr[j-1] = tmp;}} return arr;}}
def popsort (arr): n = len (arr) if n < 2: return arr for I in range (n): for J in Range (n-1,i,-1):
if arr[j-1] > Arr[j]: arr[j-1],arr[j] = arr[j],arr[j-1] return arr
Quick Sortpublic Class QuickSort {public int getpartition (int[] arr,int low,int high) {int Tmp;int index = low-1;for (int i = Low;idef quickSort (Arr,low,high): #随机选择基准元素的快速排序, achieve desired time complexity O (NLGN) #注意high是最大下标 if low < high: mid = Getpartition (Arr,low,high) #对基准元素两边的数组快排 quickSort (arr,low,mid-1) quickSort (Arr,mid+1,high) def getpartition (Arr,low,high): #随机选择一个数并与arr [high] swap to prevent worst case #将arr [high] as a baseline to sort rand = Random.randint (Low,high) Arr[high],arr[rand] = Arr[rand],arr[high] index = low-1 for I in range (low, High): if arr[i] <= Arr[high]: #保证index前面的数都比key小 Index + = 1 arr[index],arr[i] = Arr[i],arr[index] Arr[index+1],arr[high] = arr[high],arr[index+1] return index+1
Merge sortpublic Class MergeSort {public void Sort (int[] arr,int Low,int high) {if (Lowdef mergesort (arr,p,r): #p17, merge Sort, O (NLGN). Note that R is the largest subscript of an array if p<r: q = Int ((P+R)/2) mergesort (Arr,p, Q) mergesort (arr,q+1,r) merge (arr,p,q,r) def merge (arr,p,q,r): #合并两个有序数组, arr1[p...q] and arr2[q+ 1...R] n1 = q-p+1# The length of the left array (because the median of Q is rounded down, so q-p+1) N2 = r-q# The length of the right array arr1 = [] arr2 = [] for I in range (N1): arr1.append (Arr[p+i]) for J in Range (N2): #注意j的范围 arr2.append (arr[q+1+j]) Arr1.append (float (' inf ')) #放置一个无穷大的数作为 "Sentinel value" arr2.append (float (' inf ')) #print (arr1,arr2) i,j = 0,0 For K in range (p,r+1): #注意k的范围 if arr1[i] <= arr2[j]: arr[k] = Arr1[i] i + = 1 else: arr [k] = Arr2[j] j + = 1
Radix sort this with Java write too troublesome, all kinds of trouble ArrayList operation ~ ~ So only the Python versiondef cntdigit (Arr,radix): #获取数组元素的最大位数 maxnum = arr[0] for x in arr: if x > maxnum: maxnum = x CNT = 0 while (maxnum! = 0) : maxnum/= Radix cnt + 1 return cntdef radixsort (arr,radix=10): k = cntdi Git (arr,radix) #获取最大位数 bucket = [[] for I in range (radix)] for I in range (1,k+1): For J in arr: #bucket [x] stores the number from low to high I bits X, in an array of 543,i=1 when there is bucket[3] bucket[j/(radix** (i-1))% (radix)].append (j) del arr[:]# First Initialize arr #print (bucket) for z in bucket: #当前位数的数组按顺序放入arr中 arr + = Z del z[:] return arr
Algorithm Basics-Java and Python implementations of the classical eight-order algorithm