3 - 排序算法
一、插入排序
1、直接插入排序
# -*- coding: utf-8 -*- def InsertSort(List): # 获取列表长度 length = len(List) for i in range(1, length): #i代表每一轮的当前值,j代表当前值的前一个值 j = i - 1 # 如果当前值小于前一个元素,则将当前值作为一个临时变量存储,将前一个元素后移一位 if (List[i] < List[j]): temp = List[i] List[i] = List[j] # 继续往前寻找,如果有比临时变量大的数字,则后移一位,直到找到比临时变量小的元素或者达到列表第一个元素 j = j - 1 while j >= 0 and List[j] > temp: List[j + 1] = List[j] j = j - 1 # 将临时变量赋值给合适位置 List[j + 1] = temp List = [49, 38, 65, 97, 76, 13, 27, 49] InsertSort(List) print(List)
2、折半插入排序
二、希尔排序
def ShellInsetSort(array, len_array, dk): for i in range(dk, len_array): # 从下标为dk的数进行直接插入排序 position = i current_val = array[position] # 要插入的数 index = i j = int(index / dk) # index与dk的商 index = index - j * dk # while True: # 找到第一个的下标,在增量为dk中,第一个的下标index必然 0<=index<dk # index = index - dk # if 0<=index and index <dk: # break # position>index,要插入的数的下标必须得大于第一个下标 while position > index and current_val < array[position-dk]: array[position] = array[position-dk] # 往后移动 position = position-dk else: array[position] = current_val def ShellSort(array, len_array): # 希尔排序 dk = int(len_array/2) # 增量 while(dk >= 1): ShellInsetSort(array, len_array, dk) print(">>:",array) dk = int(dk/2) if __name__ == "__main__": array = [49, 38, 65, 97, 76, 13, 27, 49, 55, 4] print(">:", array) ShellSort(array, len(array))
三、冒泡排序
list = [1, 2, 3, 4, 5, 6] for t in range(len(list)-1): for i in range(0, len(list)-1): tmp = list[i] if list[i] < list[i+1]: list[i] = list[i+1] list[i+1] =tmp print(list)
四、快速排序
def sub_sort(array,low,high): # 实现分组 key = array[low] while low < high: while low < high and array[high] >= key: high -= 1 while low < high and array[high] < key: array[low] = array[high] low += 1 array[high] = array[low] array[low] = key return low def quick_sort(array,low,high): if low < high: key_index = sub_sort(array,low,high) quick_sort(array,low,key_index) quick_sort(array,key_index+1,high) if __name__ == '__main__': array = [8,10,9,6,4,16,5,13,26,18,2,45,34,23,1,7,3] print(array) quick_sort(array,0,len(array)-1) print(array)
五、选择排序
def selectedSort(myList): #获取list的长度 length = len(myList) #一共进行多少轮比较 for i in range(0,length-1): #默认设置最小值得index为当前值 smallest = i #用当先最小index的值分别与后面的值进行比较,以便获取最小index for j in range(i+1,length): #如果找到比当前值小的index,则进行两值交换 if myList[j]<myList[smallest]: tmp = myList[j] myList[j] = myList[smallest] myList[smallest]=tmp #打印每一轮比较好的列表 print("Round ",i,": ",myList) myList = [1,4,5,0,6] print("Selected Sort: ") selectedSort(myList)
六、堆排序
暂时不做实现
七、归并排序
def merge(a, b): c = [] h = j = 0 while j < len(a) and h < len(b): if a[j] < b[h]: c.append(a[j]) j += 1 else: c.append(b[h]) h += 1 if j == len(a): for i in b[h:]: c.append(i) else: for i in a[j:]: c.append(i) return c def merge_sort(lists): if len(lists) <= 1: return lists middle = int(len(lists)/2) left = merge_sort(lists[:middle]) right = merge_sort(lists[middle:]) return merge(left, right) if __name__ == '__main__': a = [4, 7, 8, 3, 5, 9] print(merge_sort(a))
八、基数排序
def radix_sort(s): """基数排序""" i = 0 # 记录当前正在排拿一位,最低位为1 max_num = max(s) # 最大值 j = len(str(max_num)) # 记录最大值的位数 while i < j: bucket_list =[[] for _ in range(10)] #初始化桶数组 for x in s: bucket_list[int(x / (10**i)) % 10].append(x) # 找到位置放入桶数组 print(bucket_list) s.clear() for x in bucket_list: # 放回原序列 for y in x: s.append(y) i += 1 if __name__ == '__main__': a = [51,7,12,336,2,67,16,16,553] radix_sort(a) print(a)
认真理解排序算法思想,然后编码实现,排序算法是灵魂!