Python 排序学习
2021-04-21 00:46
786 查看
冒泡排序
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 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
插入排序
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
希尔排序
def shellSort(arr): import math 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 }
归并排序
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
快速排序
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]
堆排序
def buildMaxHeap(arr): import math for i 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
计数排序
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
相关文章推荐
- Python Numpy学习(二)排序
- 学习Python第5天_dict&set&tuple&排序算法(冒泡排序、选择排序)
- 学习bisect模块保持列表排序 分类: python 2013-01-22 10:39 200人阅读 评论(0) 收藏
- python学习------csv文件排序
- python数据结构学习笔记-2016-10-24-01-排序列表
- Python学习——对输入的数字进行排序
- python列表排序 分类: python基础学习 python 2013-07-17 11:19 884人阅读 评论(0) 收藏
- python数据结构学习笔记-2016-11-26-01-链表排序
- 学习python笔记(一),列表嵌套的频率统计与排序
- 【python学习笔记】17:numpy数组排序
- python学习——采用sorted方法对字典的键、值进行排序
- Python学习笔记_数据排序方法
- 【Python学习系列二十二】pandas数据筛选和排序
- 算法学习(1):排序算法-插入排序及python实现
- python_day2_学习代码(循环结构、函数、文件、猜数字游戏、按照value排序,返回一个排好序的字典等)
- sorted()排序详解 分类: python基础学习 python Module 2013-09-04 17:18 1372人阅读 评论(0) 收藏
- Python学习之列表排序
- python 学习(排序)
- python学习13-搜索和排序
- Python 排序---sort与sorted学习