example02.py 3.4 KB

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