(3)Python 程式碼
1 def bubbleSort(arr): 2 for i in range(1, len(arr)): 3 for j in range(0, len(arr)-i): 4 if arr[j] > arr[j+1]: 5 arr[j], arr[j + 1] = arr[j + 1], arr[j] 6 return arr
(1)演演算法步驟
(2)動圖演示
(3)Python 程式碼
1 def selectionSort(arr): 2 for i in range(len(arr) - 1): 3 # 記錄最小數的索引 4 minIndex = i 5 for j in range(i + 1, len(arr)): 6 if arr[j] < arr[minIndex]: 7 minIndex = j 8 # i 不是最小數時,將 i 和最小數進行交換 9 if i != minIndex: 10 arr[i], arr[minIndex] = arr[minIndex], arr[i] 11 return arr
(1)演演算法步驟
(2)動圖演示
(3)Python 程式碼
1 def insertionSort(arr): 2 for i in range(len(arr)): 3 preIndex = i-1 4 current = arr[i] 5 while preIndex >= 0 and arr[preIndex] > current: 6 arr[preIndex+1] = arr[preIndex] 7 preIndex-=1 8 arr[preIndex+1] = current 9 return arr
(1)演演算法步驟
(2)動圖演示
(3)Python 程式碼
1 def shellSort(arr): 2 import math 3 gap=1 4 while(gap < len(arr)/3): 5 gap = gap*3+1 6 while gap > 0: 7 for i in range(gap,len(arr)): 8 temp = arr[i] 9 j = i-gap 10 while j >=0 and arr[j] > temp: 11 arr[j+gap]=arr[j] 12 j-=gap 13 arr[j+gap] = temp 14 gap = math.floor(gap/3) 15 return arr
(1)演演算法步驟
(2)動圖演示
(3)Python 程式碼
1 def mergeSort(arr): 2 import math 3 if(len(arr)<2): 4 return arr 5 middle = math.floor(len(arr)/2) 6 left, right = arr[0:middle], arr[middle:] 7 return merge(mergeSort(left), mergeSort(right)) 8 9 def merge(left,right): 10 result = [] 11 while left and right: 12 if left[0] <= right[0]: 13 result.append(left.pop(0)); 14 else: 15 result.append(right.pop(0)); 16 while left: 17 result.append(left.pop(0)); 18 while right: 19 result.append(right.pop(0)); 20 return result
快速排序的最壞執行情況是 O(n²),比如說順序數列的快排。但它的平攤期望時間是 O(nlogn),且 O(nlogn) 記號中隱含的常數因子很小,比複雜度穩定等於 O(nlogn) 的歸併排序要小很多。所以,對絕大多數順序性較弱的亂數列而言,快速排序總是優於歸併排序。
(1)演演算法步驟
(2)動圖演示
(3)Python 程式碼
1 def quickSort(arr, left=None, right=None): 2 left = 0 if not isinstance(left,(int, float)) else left 3 right = len(arr)-1 if not isinstance(right,(int, float)) else right 4 if left < right: 5 partitionIndex = partition(arr, left, right) 6 quickSort(arr, left, partitionIndex-1) 7 quickSort(arr, partitionIndex+1, right) 8 return arr 9 10 def partition(arr, left, right): 11 pivot = left 12 index = pivot+1 13 i = index 14 while i <= right: 15 if arr[i] < arr[pivot]: 16 swap(arr, i, index) 17 index+=1 18 i+=1 19 swap(arr,pivot,index-1) 20 return index-1 21 22 def swap(arr, i, j): 23 arr[i], arr[j] = arr[j], arr[i]
(2)動圖演示
(3)Python 程式碼
1 def buildMaxHeap(arr): 2 import math 3 for i in range(math.floor(len(arr)/2),-1,-1): 4 heapify(arr,i) 5 6 def heapify(arr, i): 7 left = 2*i+1 8 right = 2*i+2 9 largest = i 10 if left < arrLen and arr[left] > arr[largest]: 11 largest = left 12 if right < arrLen and arr[right] > arr[largest]: 13 largest = right 14 15 if largest != i: 16 swap(arr, i, largest) 17 heapify(arr, largest) 18 19 def swap(arr, i, j): 20 arr[i], arr[j] = arr[j], arr[i] 21 22 def heapSort(arr): 23 global arrLen 24 arrLen = len(arr) 25 buildMaxHeap(arr) 26 for i in range(len(arr)-1,0,-1): 27 swap(arr,0,i) 28 arrLen -=1 29 heapify(arr, 0) 30 return arr
1 def countingSort(arr, maxValue): 2 bucketLen = maxValue+1 3 bucket = [0]*bucketLen 4 sortedIndex =0 5 arrLen = len(arr) 6 for i in range(arrLen): 7 if not bucket[arr[i]]: 8 bucket[arr[i]]=0 9 bucket[arr[i]]+=1 10 for j in range(bucketLen): 11 while bucket[j]>0: 12 arr[sortedIndex] = j 13 sortedIndex+=1 14 bucket[j]-=1 15 return arr
1 def bucket_sort(s): 2 """桶排序""" 3 min_num = min(s) 4 max_num = max(s) 5 # 桶的大小 6 bucket_range = (max_num-min_num) / len(s) 7 # 桶陣列 8 count_list = [ [] for i in range(len(s) + 1)] 9 # 向桶陣列填數 10 for i in s: 11 count_list[int((i-min_num)//bucket_range)].append(i) 12 s.clear() 13 # 回填,這裡桶內部排序直接呼叫了sorted 14 for i in count_list: 15 for j in sorted(i): 16 s.append(j) 17 18 if __name__ == __main__ : 19 a = [3.2,6,8,4,2,6,7,3] 20 bucket_sort(a) 21 print(a) # [2, 3, 3.2, 4, 6, 6, 7, 8]
1 def RadixSort(list): 2 i = 0 #初始為個位排序 3 n = 1 #最小的位數置為1(包含0) 4 max_num = max(list) #得到帶排序陣列中最大數 5 while max_num > 10**n: #得到最大數是幾位數 6 n += 1 7 while i < n: 8 bucket = {} #用字典構建桶 9 for x in range(10): 10 bucket.setdefault(x, []) #將每個桶置空 11 for x in list: #對每一位進行排序 12 radix =int((x / (10**i)) % 10) #得到每位的基數 13 bucket[radix].append(x) #將對應的陣列元素加入到相 #應位基數的桶中 14 j = 0 15 for k in range(10): 16 if len(bucket[k]) != 0: #若桶不為空 17 for y in bucket[k]: #將該桶中每個元素 18 list[j] = y #放回到陣列中 19 j += 1 20 i += 1 21 return list