From db9faf98e1160b947ef21cce2d9629f3130376c3 Mon Sep 17 00:00:00 2001 From: NIngCoder Date: Sat, 26 Nov 2022 00:58:39 +0800 Subject: [PATCH] sort --- codes/python/chapter_sorting/bubble_sort.py | 4 +-- .../python/chapter_sorting/insertion_sort.py | 2 +- codes/python/chapter_sorting/merge_sort.py | 15 ++++----- codes/python/chapter_sorting/quick_sort.py | 32 +++++++++---------- 4 files changed, 26 insertions(+), 27 deletions(-) diff --git a/codes/python/chapter_sorting/bubble_sort.py b/codes/python/chapter_sorting/bubble_sort.py index 8bbee89..938ee83 100644 --- a/codes/python/chapter_sorting/bubble_sort.py +++ b/codes/python/chapter_sorting/bubble_sort.py @@ -10,7 +10,7 @@ import os.path as osp sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) -"""冒泡排序""" +""" 冒泡排序 """ def bubble_sort(nums): n = len(nums) # 外循环:待排序元素数量为 n-1, n-2, ..., 1 @@ -22,7 +22,7 @@ def bubble_sort(nums): nums[j], nums[j + 1] = nums[j + 1], nums[j] -"""冒泡排序(标志优化)""" +""" 冒泡排序(标志优化) """ def bubble_sort_with_flag(nums): n = len(nums) # 外循环:待排序元素数量为 n-1, n-2, ..., 1 diff --git a/codes/python/chapter_sorting/insertion_sort.py b/codes/python/chapter_sorting/insertion_sort.py index 0a46af1..37f6833 100644 --- a/codes/python/chapter_sorting/insertion_sort.py +++ b/codes/python/chapter_sorting/insertion_sort.py @@ -10,7 +10,7 @@ import os.path as osp sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) -"""插入排序""" +""" 插入排序 """ def insertion_sort(nums): # 外循环:base = nums[1], nums[2], ..., nums[n-1] for i in range(1, len(nums)): diff --git a/codes/python/chapter_sorting/merge_sort.py b/codes/python/chapter_sorting/merge_sort.py index 98a2c5f..312d205 100644 --- a/codes/python/chapter_sorting/merge_sort.py +++ b/codes/python/chapter_sorting/merge_sort.py @@ -19,19 +19,19 @@ def merge(nums, left, mid, right): # 初始化辅助数组 借助 copy模块 tmp = nums[left:right + 1] # 左子数组的起始索引和结束索引 - leftStart, leftEnd = left - left, mid - left + left_start, left_end = left - left, mid - left # 右子数组的起始索引和结束索引 - rightStart, rightEnd = mid + 1 - left, right - left + right_start, right_end = mid + 1 - left, right - left # i, j 分别指向左子数组、右子数组的首元素 - i, j = leftStart, rightStart + i, j = left_start, right_start # 通过覆盖原数组 nums 来合并左子数组和右子数组 for k in range(left, right + 1): # 若 “左子数组已全部合并完”,则选取右子数组元素,并且 j++ - if i > leftEnd: + if i > left_end: nums[k] = tmp[j] j += 1 # 否则,若 “右子数组已全部合并完” 或 “左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++ - elif j > rightEnd or tmp[i] <= tmp[j]: + elif j > right_end or tmp[i] <= tmp[j]: nums[k] = tmp[i] i += 1 # 否则,若 “左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ @@ -39,14 +39,13 @@ def merge(nums, left, mid, right): nums[k] = tmp[j] j += 1 - -"""归并排序""" +""" 归并排序 """ def merge_sort(nums, left, right): # 终止条件 if left >= right: return # 当子数组长度为 1 时终止递归 # 划分阶段 - mid = left + right >> 1 # 计算中点 + mid = (left + right) // 2 # 计算中点 merge_sort(nums, left, mid) # 递归左子数组 merge_sort(nums, mid + 1, right) # 递归右子数组 # 合并阶段 diff --git a/codes/python/chapter_sorting/quick_sort.py b/codes/python/chapter_sorting/quick_sort.py index 658d6a5..dec5726 100644 --- a/codes/python/chapter_sorting/quick_sort.py +++ b/codes/python/chapter_sorting/quick_sort.py @@ -10,9 +10,9 @@ import os.path as osp sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) -"""快速排序类""" -class quick_sort(object): - """哨兵划分""" +""" 快速排序类 """ +class QuickSort(object): + """ 哨兵划分 """ def partition(self, nums, left, right): # 以 nums[left] 作为基准数 i, j = left, right @@ -27,7 +27,7 @@ class quick_sort(object): nums[i], nums[left] = nums[left], nums[i] return i # 返回基准数的索引 - """快速排序""" + """ 快速排序 """ def quick_sort(self, nums, left, right): # 子数组长度为 1 时终止递归 if left >= right: @@ -39,10 +39,10 @@ class quick_sort(object): self.quick_sort(nums, pivot + 1, right) -"""快速排序类(中位基准数优化)""" -class quick_sort_median(): +""" 快速排序类(中位基准数优化)""" +class QuickSortMedian(): - """选取三个元素的中位数""" + """ 选取三个元素的中位数 """ def median_three(self, nums, left, mid, right): # 使用了异或操作来简化代码 # 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1 @@ -52,7 +52,7 @@ class quick_sort_median(): return mid return right - """哨兵划分(三数取中值)""" + """ 哨兵划分(三数取中值) """ def partition(self, nums, left, right): # 以 nums[left] 作为基准数 med = self.median_three(nums, left, (left + right) // 2, right) @@ -71,7 +71,7 @@ class quick_sort_median(): nums[i], nums[left] = nums[left], nums[i] return i # 返回基准数的索引 - """快速排序""" + """ 快速排序 """ def quick_sort(self, nums, left, right): # 子数组长度为 1 时终止递归 if left >= right: return @@ -82,9 +82,9 @@ class quick_sort_median(): self.quick_sort(nums, pivot + 1, right) -"""快速排序类(尾递归优化)""" -class quick_sort_tail_call(): - """哨兵划分""" +""" 快速排序类(尾递归优化) """ +class QuickSortTailCall(): + """ 哨兵划分 """ def partition(self, nums, left, right): # 以 nums[left] 作为基准数 i, j = left, right @@ -99,7 +99,7 @@ class quick_sort_tail_call(): nums[i], nums[left] = nums[left], nums[i] return i # 返回基准数的索引 - """快速排序(尾递归优化)""" + """ 快速排序(尾递归优化) """ def quick_sort(self, nums, left, right): # 子数组长度为 1 时终止 while left < right: @@ -117,15 +117,15 @@ class quick_sort_tail_call(): if __name__ == '__main__': # 快速排序 nums = [4, 1, 3, 1, 5, 2] - quick_sort().quick_sort(nums, 0, len(nums) - 1) + QuickSort().quick_sort(nums, 0, len(nums) - 1) print("快速排序完成后 nums = ", nums) # 快速排序(中位基准数优化) nums1 = [4, 1, 3, 1, 5, 2] - quick_sort_median().quick_sort(nums1, 0, len(nums1) - 1) + QuickSortMedian().quick_sort(nums1, 0, len(nums1) - 1) print("快速排序(中位基准数优化)完成后 nums = ", nums) # 快速排序(尾递归优化) nums2 = [4, 1, 3, 1, 5, 2] - quick_sort_tail_call().quick_sort(nums, 0, len(nums2) - 1) + QuickSortTailCall().quick_sort(nums, 0, len(nums2) - 1) print("快速排序(尾递归优化)完成后 nums = ", nums)