python知识:几个排序算法的python实现
生活随笔
收集整理的這篇文章主要介紹了
python知识:几个排序算法的python实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
將數據結構的10個排序用python實現,以便快速查找或學習。
1 冒泡排序
###冒泡排序 def bubbleSort(arr):length = len(arr)for i in range(length-1):for j in range(length-1-i):if arr[j]>arr[j+1]:arr[j+1],arr[j] = arr[j],arr[j+1]return arr2 選擇排序?
#選擇排序 def selectionSort(arr):length = len(arr)for i in range(length-1):minIndex = ifor j in range(i+1,length):if arr[j]<arr[minIndex]:minIndex = j arr[i],arr[minIndex] = arr[minIndex],arr[i]return arr3 插入排序?
#插入排序 def insertSort(arr):length = len(arr)for i in range(1,length):preIndex = i-1current = arr[i]while(preIndex>=0 and arr[preIndex]>current):arr[preIndex+1] = arr[preIndex]preIndex -= 1arr[preIndex+1] = currentreturn arr?4希爾排序
##希爾排序 def shellSort(arr):length = len(arr)gap = 1while gap<(length/3):gap = gap*3 +1while gap >= 1:for i in range(gap,length):j = iwhile j >=gap and arr[j]<arr[j-gap]:arr[j],arr[j-gap] = arr[j-gap],arr[j]j -=gapgap = gap//3return arr5??歸并排序
#歸并排序 def mergeSort(arr):length = len(arr)if length<2:return arrmiddle = length//2left = mergeSort(arr[:middle])right = mergeSort(arr[middle:])return merge(left,right) def merge(left,right):l,r = 0,0result = []while (l<len(left) and r<len(right) ):if left[l] < right[r]:result.append(left[l])l += 1else:result.append(right[r])r +=1result += left[l:]result += right[r:]return result?6 快速排序
##快速排序 def quickSort(arr):if len(arr)<2:return arrelse:key = arr[0]low = [value for value in arr[1:] if value < key]high = [value for value in arr[1:] if value > key]equal = [value for value in arr[1:] if value == key]return quickSort(low)+equal+quickSort(high)7 堆排序?
##堆排序 def heapSort(arr):from collections import dequeL = deque(arr)L.appendleft(0)length = len(L)-1first = length//2for i in range(first):heap_adjust(L,first-i,length)for i in range(length-1):L = swap(L,1,length-i)heap_adjust(L,1,length-i-1)return [L[i] for i in range(1,len(L))] def heap_adjust(L,start,end):temp = L[start]i = startj = 2*iwhile j<=end:if (j<end) and L[j]<L[j+1]:j+=1if temp < L[j]:L[i] = L[j]i = jj = 2*ielse:breakL[i] = temp def swap(L,i,j):L[i],L[j] =L[j],L[i]return L8 計數排序?
##計數排序 def countingSort(arr):maxValue = max(arr)bucket = [0]*(maxValue+1)sortIndex = 0for i in arr:bucket[i] +=1for j in range(maxValue+1):while bucket[j]>0:arr[sortIndex] = jbucket[j] -=1sortIndex +=1return arr9 桶排序
##桶排序 def bucketSort(arr):maxValue = max(arr)bucket = [0]*(maxValue+1)for i in arr:bucket[i] +=1sort_nums = []for i in range(len(bucket)):if bucket[i] !=0:for j in range(bucket[i]):sort_nums.append(i)return sort_nums?10 基數排序
##基數排序 def radixSort(arr):import mathradix = 10k = int(math.ceil(math.log(max(arr),radix))) #math.ceil 向上取整 bucket = [[] for i in range(radix)]for i in range(1,k+1):for val in arr:bucket[val%(radix**i)//(radix**(i-1))].append(val)del arr[:]for each in bucket:arr.extend(each)bucket = [[] for i in range(radix)]return arr輸入測試序列:
arr = [31,38,5,15,26,36,27,2,13]
bucketSort(arr)?
總結
以上是生活随笔為你收集整理的python知识:几个排序算法的python实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux:几个重要的文件处理命令
- 下一篇: 机器学习:如何用相关性实现特征选择?