python之(26)中級總結(4)九大排序
阿新 • • 發佈:2020-12-07
1、氣泡排序
# 氣泡排序 def bubbleSort(arr): for i in range(1, len(arr)): for j in range(0, len(arr) - i): if arr[j] > arr[j + 1]: arr[j], arr[j + 1] = arr[j + 1], arr[j] return arr # 氣泡排序反向遍歷 def bubbleReverseSort(arr): # 反向遍歷 for i in range(len(arr) - 1, 0, -1):for j in range(0, i): if arr[j] > arr[j + 1]: arr[j], arr[j + 1] = arr[j + 1], arr[j] return arr
2、桶排序
# 桶排序 def bucket_sort_simplify(arr, max_num): """ 簡化版 """ buf = {i: [] for i in range(int(max_num) + 1)} # 不能使用[[]]*(max+1),這樣新建的空間中各個[]是共享記憶體的 arr_len = len(arr)for i in range(arr_len): num = arr[i] buf[int(num)].append(num) # 將相應範圍內的資料加入到[]中 arr = [] for i in range(len(buf)): if buf[i]: arr.extend(sorted(buf[i])) # 這裡還需要對一個範圍內的資料進行排序,然後再進行輸出 return arr
3、堆排序
import math # 堆排序 def buildMaxHeap(arr): fori in range(math.floor(len(arr) / 2), -1, -1): heapify(arr, i) def heapify(arr, i): left = 2 * i + 1 right = 2 * i + 2 largest = i if left < arrLen and arr[left] > arr[largest]: largest = left if right < arrLen and arr[right] > arr[largest]: largest = right if largest != i: swap(arr, i, largest) heapify(arr, largest) def swap(arr, i, j): arr[i], arr[j] = arr[j], arr[i] def heapSort(arr): global arrLen arrLen = len(arr) buildMaxHeap(arr) for i in range(len(arr) - 1, 0, -1): swap(arr, 0, i) arrLen -= 1 heapify(arr, 0) return arr
4、計數排序
# 計數排序 def countingSort(arr, maxValue): bucketLen = maxValue + 1 bucket = [0] * bucketLen sortedIndex = 0 arrLen = len(arr) for i in range(arrLen): if not bucket[arr[i]]: bucket[arr[i]] = 0 bucket[arr[i]] += 1 for j in range(bucketLen): while bucket[j] > 0: arr[sortedIndex] = j sortedIndex += 1 bucket[j] -= 1 return arr
5、插入排序
# 插入排序 def insertionSort(arr): for i in range(len(arr)): preIndex = i - 1 current = arr[i] while preIndex >= 0 and arr[preIndex] > current: arr[preIndex + 1] = arr[preIndex] preIndex = 1 arr[preIndex + 1] = current return arr
6、歸併排序
# 歸併排序 def mergeSort(arr): import math if len(arr) < 2: return arr middle = math.floor(len(arr) / 2) left, right = arr[0:middle], arr[middle:] return merge(mergeSort(left), mergeSort(right)) def merge(left, right): result = [] while left and right: if left[0] <= right[0]: result.append(left.pop(0)) else: result.append(right.pop(0)); while left: result.append(left.pop(0)) while right: result.append(right.pop(0)); return result
7、快速排序
# 快速排序 def quickSort(arr, left=None, right=None): left = 0 if not isinstance(left, (int, float)) else left right = len(arr) - 1 if not isinstance(right, (int, float)) else right if left < right: partitionIndex = partition(arr, left, right) quickSort(arr, left, partitionIndex - 1) quickSort(arr, partitionIndex + 1, right) return arr def partition(arr, left, right): pivot = left index = pivot + 1 i = index while i <= right: if arr[i] < arr[pivot]: swap(arr, i, index) index += 1 i += 1 swap(arr, pivot, index - 1) return index - 1 def swap(arr, i, j): arr[i], arr[j] = arr[j], arr[i]
8、選擇排序
# 選擇排序 def selectionSort(arr): for i in range(len(arr) - 1): # 記錄最小數的索引 minIndex = i for j in range(i + 1, len(arr)): if arr[j] < arr[minIndex]: minIndex = j # i 不是最小數時,將 i 和最小數進行交換 if i != minIndex: arr[i], arr[minIndex] = arr[minIndex], arr[i] return arr
9、希爾排序
# 希爾排序 import math # 希爾排序 def shellSort(arr): gap = 1 while gap < len(arr) / 3: gap = gap * 3 + 1 while gap > 0: for i in range(gap, len(arr)): temp = arr[i] j = i - gap while j >= 0 and arr[j] > temp: arr[j + gap] = arr[j] j -= gap arr[j + gap] = temp gap = math.floor(gap / 3) return arr
10、測試
from DataStructure.SoftUtil.bubbleSort import bubbleSort, bubbleReverseSort from DataStructure.SoftUtil.bucket_sort_simplify import bucket_sort_simplify from DataStructure.SoftUtil.buildMaxHeap import heapSort from DataStructure.SoftUtil.countingSort import countingSort from DataStructure.SoftUtil.insertionSort import insertionSort from DataStructure.SoftUtil.mergeSort import mergeSort from DataStructure.SoftUtil.quickSort import quickSort from DataStructure.SoftUtil.selectionSort import selectionSort from DataStructure.SoftUtil.shellSort import shellSort if __name__ == '__main__': arr = [1, 4, 3, 7, 8, 2, 5] print(bubbleSort(arr)) print(bubbleReverseSort(arr)) print(selectionSort(arr)) print(insertionSort(arr)) print(shellSort(arr)) print(mergeSort(arr)) print(quickSort(arr)) print(heapSort(arr)) print(countingSort(arr)) print(bucket_sort_simplify(arr, max(arr)))
結果:
C:\Anaconda3\envs\FlinkUdf\python.exe C:/app/FlinkUdf/src/main/Python/DataStructure/Imp/SoftIml.py Traceback (most recent call last): File "C:/app/FlinkUdf/src/main/Python/DataStructure/Imp/SoftIml.py", line 21, in <module> print(countingSort(arr)) TypeError: countingSort() missing 1 required positional argument: 'maxValue' [1, 2, 3, 4, 5, 7, 8] [1, 2, 3, 4, 5, 7, 8] [1, 2, 3, 4, 5, 7, 8] [1, 2, 3, 4, 5, 7, 8] [1, 2, 3, 4, 5, 7, 8] [1, 2, 3, 4, 5, 7, 8] [1, 2, 3, 4, 5, 7, 8] [1, 2, 3, 4, 5, 7, 8] Process finished with exit code 1