├── README.md ├── bubble_sort.py ├── heap_sort.py ├── insert_sort.py ├── merge_sort.py ├── quick_sort.py ├── select_sort.py └── shell_sort.py /README.md: -------------------------------------------------------------------------------- 1 | # Seven-sorting-methods 2 | 3 | - This module aims to introduce the popular sorting methods including seven different schemes. All the implementation code are conducted by python 3 language. 4 | - 本文使用python 3实现常用的排序算法,包括:冒泡排序、直接插入排序、直接选择排序、希尔排序、归并排序、快速排序、堆排序。 5 | -------------------------------------------------------------------------------- /bubble_sort.py: -------------------------------------------------------------------------------- 1 | ''' 2 | 3 | README 4 | 5 | This module aims to introduce the popular sorting method--bubble sort 6 | and three different kind of schemes are conducted by python 3 language. 7 | 8 | 9 | ''' 10 | 11 | # -*- coding:utf-8 -*- 12 | 13 | import math 14 | 15 | class BubbleSortMethod(object): 16 | # construction method 17 | def __init__(self,array): 18 | self.array=array 19 | 20 | # the most common method, the maximum number in the array is swapped to the last position during an iteration. 21 | def bubble_sort1(self): 22 | array=self.array 23 | n=len(array) 24 | for i in range(n): 25 | for j in range(n-i-1): 26 | if array[j] > array[j+1]: 27 | tmp=array[j] 28 | array[j]=array[j+1] 29 | array[j+1]=tmp 30 | 31 | return array 32 | 33 | # set a tag when each swapping operation occurs 34 | def bubble_sort2(self): 35 | array=self.array 36 | n=len(array) 37 | swap_tag=True 38 | for i in range(n): 39 | if swap_tag: 40 | swap_tag=False 41 | for j in range(n-i-1): 42 | if array[j] > array[j+1]: 43 | tmp=array[j] 44 | array[j]=array[j+1] 45 | array[j+1]=tmp 46 | swap_tag=True # it means that the array has't been soted completely yet 47 | else: 48 | break 49 | return array 50 | 51 | # record the position where the last swapping operation occurs during the iterations 52 | def bubble_sort3(self): 53 | array=self.array 54 | n=len(array) 55 | swap_index=n 56 | while swap_index>0: 57 | k=swap_index 58 | swap_index=0 # if no swapping operation occus, the loop will break 59 | for j in range(k-1): 60 | if array[j] > array[j+1]: 61 | tmp=array[j] 62 | array[j]=array[j+1] 63 | array[j+1]=tmp 64 | swap_index=j 65 | 66 | return array 67 | 68 | 69 | 70 | if __name__=='__main__': 71 | 72 | testlist=[2,5,7,1,9,4,6,0,12,35,7] 73 | bubble=BubbleSortMethod(testlist) # new object of BubbleSortMethod class 74 | sortlist=bubble.bubble_sort1() 75 | print(sortlist) 76 | sortlist=bubble.bubble_sort2() 77 | print(sortlist) 78 | sortlist=bubble.bubble_sort3() 79 | print(sortlist) 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /heap_sort.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | 3 | def min_heap_sort(input_list): 4 | 5 | def heap_adjust(input_list, parent, length): 6 | temp = input_list[parent] 7 | child=2 * parent + 1 8 | 9 | while child < length: 10 | if child+1 < length and input_list[child+1] < input_list[child]: 11 | child += 1 12 | if temp <= input_list[child]: 13 | break 14 | input_list[parent] = input_list[child] 15 | parent = child 16 | child = 2 * parent + 1 17 | input_list[parent] = temp 18 | 19 | sorted_list = input_list 20 | length = len(sorted_list) 21 | 22 | # 初始化堆 23 | for i in range(0, length // 2 + 1)[::-1]: 24 | heap_adjust(sorted_list, i, length) 25 | 26 | # 27 | for j in range(1, length)[::-1]: 28 | sorted_list[j],sorted_list[0]=sorted_list[0],sorted_list[j] 29 | 30 | heap_adjust(sorted_list, 0, j) 31 | print('第%d趟排序:' % (length - j), end = '') 32 | print(sorted_list) 33 | 34 | return sorted_list 35 | 36 | if __name__ == '__main__': 37 | input_list = [6, 4, 8, 9, 2, 3, 1] 38 | print('排序前:', input_list) 39 | sorted_list = min_heap_sort(input_list) 40 | print('排序后:', sorted_list) 41 | 42 | 43 | -------------------------------------------------------------------------------- /insert_sort.py: -------------------------------------------------------------------------------- 1 | ''' 2 | 3 | README 4 | 5 | This module aims to introduce the popular sorting method--direct insert sort 6 | and three different kinds of implementation schemes are conducted by python 3 language. 7 | 8 | 9 | ''' 10 | 11 | # -*- coding:utf-8 -*- 12 | 13 | import math 14 | import copy as cp 15 | 16 | class InsertSortMethod(): 17 | # construction method 18 | def __init__(self, array): 19 | self.array=array 20 | 21 | # the original method that insert the number to the proper position of sorted subset one by one 22 | def insert_sort1(self): 23 | array=cp.deepcopy(self.array) 24 | n=len(array) 25 | for i in range(n-1): 26 | # firstly, array[0] is the sorted subset 27 | if array[i+1]=0: 31 | if array[j]>temp: 32 | array[j+1]=array[j] 33 | j=j-1 34 | else: 35 | break 36 | array[j+1]=temp 37 | return array 38 | 39 | # set a tag when each swapping operation occurs 40 | def insert_sort2(self): 41 | array=self.array[:] 42 | n=len(array) 43 | for i in range(n-1): 44 | # firstly, array[0] is the sorted subset 45 | if array[i+1]=0: 48 | if array[j]>array[j+1]: 49 | # swap operation 50 | temp=array[j] 51 | array[j]=array[j+1] 52 | array[j+1]=temp 53 | j=j-1 54 | else: 55 | break 56 | return array 57 | 58 | 59 | if __name__=='__main__': 60 | 61 | testlist=[2,5,7,1,9,4,6,0,12,35,7,3,30] 62 | insert=InsertSortMethod(testlist) # new object of InsertSortMethod class 63 | sortlist=insert.insert_sort1() 64 | print(sortlist) 65 | print(insert.array) 66 | sortlist=insert.insert_sort2() 67 | print(sortlist) 68 | print(insert.array) 69 | 70 | 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /merge_sort.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | 3 | import math 4 | import copy as cp 5 | 6 | 7 | def merge_sort(array, start, end): 8 | if startbase_value): 17 | j=j-1 18 | if i0: 10 | i=gap 11 | while i=0: 15 | if array[j]>array[j+gap]: 16 | # swap operation 17 | array[j],array[j+gap]=array[j+gap],array[j] 18 | j=j-gap 19 | else: 20 | break 21 | i=i+1 22 | gap=gap//2 23 | 24 | 25 | if __name__=='__main__': 26 | 27 | testlist=[2,5,7,1,9,4,6,0,12,35,7,3,30] 28 | shell_sort(testlist) 29 | print(testlist) 30 | 31 | 32 | 33 | 34 | 35 | 36 | --------------------------------------------------------------------------------