|
| 1 | +""" |
| 2 | +排序 - 冒泡排序(简单O(N**2)) / 归并排序(高级O(N*log2N)) |
| 3 | +冒泡排序 |
| 4 | +34, 99, 52, 11, 47, 68, 50, 84 |
| 5 | +34, 52, 11, 47, 68, 50, 84, 99 |
| 6 | +34, 11, 47, 52, 50, 68, 84 |
| 7 | +11, 34, 47, 50, 52, 68 |
| 8 | +
|
| 9 | +快速排序 |
| 10 | +34, 99, 52, 11, 47, 68, 50, 84 |
| 11 | +{34, 11, 47}, {50}, {99, 52, 68, 84} |
| 12 | +{11}, {34}, {47}, {50}, {52, 68, 84}, {99} |
| 13 | +{11}, {34}, {47}, {50}, {52}, {68, 84}, {99} |
| 14 | +{11}, {34}, {47}, {50}, {52}, {68}, {84}, {99} |
| 15 | +
|
| 16 | +归并排序 - 分治法(divide-and-conquer) |
| 17 | +34, 99, 52, 11, 47, 68, 50, 84 |
| 18 | +{34, 99, 52, 11}, {47, 68, 50, 84} |
| 19 | +{34, 99}, {52, 11}, {47, 68}, {50, 84} |
| 20 | +{34}, {99}, {52}, {11}, {47}, {68}, {50}, {84} |
| 21 | +{34, 99}, {11, 52}, {47, 68}, {50, 84} |
| 22 | +{11, 34, 52, 99}, {47, 50, 68, 84} |
| 23 | +{11, 34, 47, 50, 52, 68, 84, 99} |
| 24 | +
|
| 25 | +在使用分治法的时候通常都会使用到递归调用这种编程手段 |
| 26 | +一个函数直接或间接的调用了自身就称之为递归调用 |
| 27 | +""" |
| 28 | + |
| 29 | + |
| 30 | +# 9 1 2 3 4 5 6 7 8 |
| 31 | +# 2 3 4 5 6 7 8 9 1 |
| 32 | +# *前面的参数称为位置参数, *后面的参数称为命名关键字参数 |
| 33 | +# 所谓命名关键字参数就是调用函数时必须以"参数名=参数值"的形式传入参数 |
| 34 | +def bubble_sort(origin_items, *, comp=lambda x, y: x > y): |
| 35 | + """冒泡排序""" |
| 36 | + items = origin_items[:] |
| 37 | + length = len(items) |
| 38 | + for i in range(1, length): |
| 39 | + swapped = False |
| 40 | + for j in range(0, length - i): |
| 41 | + if comp(items[j], items[j + 1]): |
| 42 | + items[j], items[j + 1] = items[j + 1], items[j] |
| 43 | + swapped = True |
| 44 | + if swapped: |
| 45 | + swapped = False |
| 46 | + for j in range(length - i - 1, i - 1, -1): |
| 47 | + if comp(items[j - 1], items[j]): |
| 48 | + items[j - 1], items[j] = items[j], items[j - 1] |
| 49 | + swapped = True |
| 50 | + if not swapped: |
| 51 | + break |
| 52 | + return items |
| 53 | + |
| 54 | + |
| 55 | +def merge(list1, list2, comp=lambda x, y: x <= y): |
| 56 | + """"有序合并(将两个有序的列表合并成一个新的有序的列表)""" |
| 57 | + list3 = [] |
| 58 | + index1, index2 = 0, 0 |
| 59 | + while index1 < len(list1) and index2 < len(list2): |
| 60 | + if comp(list1[index1], list2[index2]): |
| 61 | + list3.append(list1[index1]) |
| 62 | + index1 += 1 |
| 63 | + else: |
| 64 | + list3.append(list2[index2]) |
| 65 | + index2 += 1 |
| 66 | + list3 += list1[index1:] |
| 67 | + list3 += list2[index2:] |
| 68 | + return list3 |
| 69 | + |
| 70 | + |
| 71 | +def merge_sort(origin_items, comp=lambda x, y: x <= y): |
| 72 | + """归并排序""" |
| 73 | + if len(origin_items) <= 1: |
| 74 | + return origin_items[:] |
| 75 | + mid = len(origin_items) // 2 |
| 76 | + left = merge_sort(origin_items[:mid], comp) |
| 77 | + right = merge_sort(origin_items[mid:], comp) |
| 78 | + return merge(left, right, comp) |
| 79 | + |
| 80 | + |
| 81 | +class Person: |
| 82 | + """人""" |
| 83 | + |
| 84 | + def __init__(self, name, age): |
| 85 | + self.name = name |
| 86 | + self.age = age |
| 87 | + |
| 88 | + def __repr__(self): |
| 89 | + return f'{self.name}: {self.age}' |
| 90 | + |
| 91 | + |
| 92 | +def main(): |
| 93 | + # list1 = [12, 35, 48, 87, 92] |
| 94 | + # list2 = [39, 44, 50, 60, 77, 88] |
| 95 | + # list3 = merge(list1, list2) |
| 96 | + # print(list3) |
| 97 | + items = [34, 99, 52, 11, 47, 50, 84] |
| 98 | + print(items) |
| 99 | + print(merge_sort(items)) |
| 100 | + # items = ['hi', 'hello', 'orange', 'watermelon', 'zoo', 'pitaya'] |
| 101 | + # items = [ |
| 102 | + # Person("LuoHao", 38), Person("Baiyuanfang", 25), |
| 103 | + # Person("Zhangsanfeng", 120), Person("Lee", 18) |
| 104 | + # ] |
| 105 | + # new_items = bubble_sort(items, comp=lambda x, y: len(x) > len(y)) |
| 106 | + # new_items = bubble_sort(items, comp=lambda x, y: x.age > y.age) |
| 107 | + # print(items) |
| 108 | + # print(new_items) |
| 109 | + |
| 110 | + |
| 111 | +if __name__ == '__main__': |
| 112 | + main() |
| 113 | + |
0 commit comments