|
|
|
@ -4,15 +4,12 @@ Created Time: 2022-11-25
|
|
|
|
|
Author: timi (xisunyy@163.com)
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
from include import *
|
|
|
|
|
import sys
|
|
|
|
|
import os.path as osp
|
|
|
|
|
|
|
|
|
|
import sys, os.path as osp
|
|
|
|
|
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
|
|
|
|
from include import *
|
|
|
|
|
|
|
|
|
|
""" 快速排序类 """
|
|
|
|
|
class QuickSort(object):
|
|
|
|
|
|
|
|
|
|
class QuickSort:
|
|
|
|
|
""" 哨兵划分 """
|
|
|
|
|
def partition(self, nums, left, right):
|
|
|
|
|
# 以 nums[left] 作为基准数
|
|
|
|
@ -39,10 +36,8 @@ class QuickSort(object):
|
|
|
|
|
self.quick_sort(nums, left, pivot - 1)
|
|
|
|
|
self.quick_sort(nums, pivot + 1, right)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
""" 快速排序类(中位基准数优化)"""
|
|
|
|
|
class QuickSortMedian():
|
|
|
|
|
|
|
|
|
|
class QuickSortMedian:
|
|
|
|
|
""" 选取三个元素的中位数 """
|
|
|
|
|
def median_three(self, nums, left, mid, right):
|
|
|
|
|
# 使用了异或操作来简化代码
|
|
|
|
@ -68,7 +63,7 @@ class QuickSortMedian():
|
|
|
|
|
i += 1 # 从左向右找首个大于基准数的元素
|
|
|
|
|
# 元素交换
|
|
|
|
|
nums[i], nums[j] = nums[j], nums[i]
|
|
|
|
|
# 将基准数交换至两子数组的分界线
|
|
|
|
|
# 将基准数交换至两子数组的分界线
|
|
|
|
|
nums[i], nums[left] = nums[left], nums[i]
|
|
|
|
|
return i # 返回基准数的索引
|
|
|
|
|
|
|
|
|
@ -82,10 +77,8 @@ class QuickSortMedian():
|
|
|
|
|
self.quick_sort(nums, left, pivot - 1)
|
|
|
|
|
self.quick_sort(nums, pivot + 1, right)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
""" 快速排序类(尾递归优化) """
|
|
|
|
|
class QuickSortTailCall():
|
|
|
|
|
|
|
|
|
|
class QuickSortTailCall:
|
|
|
|
|
""" 哨兵划分 """
|
|
|
|
|
def partition(self, nums, left, right):
|
|
|
|
|
# 以 nums[left] 作为基准数
|
|
|
|
@ -99,7 +92,7 @@ class QuickSortTailCall():
|
|
|
|
|
nums[i], nums[j] = nums[j], nums[i]
|
|
|
|
|
# 将基准数交换至两子数组的分界线
|
|
|
|
|
nums[i], nums[left] = nums[left], nums[i]
|
|
|
|
|
return i # 返回基准数的索引
|
|
|
|
|
return i # 返回基准数的索引
|
|
|
|
|
|
|
|
|
|
""" 快速排序(尾递归优化) """
|
|
|
|
|
def quick_sort(self, nums, left, right):
|
|
|
|
@ -110,24 +103,25 @@ class QuickSortTailCall():
|
|
|
|
|
# 对两个子数组中较短的那个执行快排
|
|
|
|
|
if pivot - left < right - pivot:
|
|
|
|
|
self.quick_sort(nums, left, pivot - 1) # 递归排序左子数组
|
|
|
|
|
left = pivot + 1 # 剩余待排序区间为 [pivot + 1, right]
|
|
|
|
|
left = pivot + 1 # 剩余待排序区间为 [pivot + 1, right]
|
|
|
|
|
else:
|
|
|
|
|
self.quick_sort(nums, pivot + 1, right) # 递归排序右子数组
|
|
|
|
|
right = pivot - 1 # 剩余待排序区间为 [left, pivot - 1]
|
|
|
|
|
right = pivot - 1 # 剩余待排序区间为 [left, pivot - 1]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
""" Driver Code """
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
# 快速排序
|
|
|
|
|
nums = [4, 1, 3, 1, 5, 2]
|
|
|
|
|
QuickSort().quick_sort(nums, 0, len(nums) - 1)
|
|
|
|
|
print("快速排序完成后 nums = ", nums)
|
|
|
|
|
print("快速排序完成后 nums =", nums)
|
|
|
|
|
|
|
|
|
|
# 快速排序(中位基准数优化)
|
|
|
|
|
nums1 = [4, 1, 3, 1, 5, 2]
|
|
|
|
|
QuickSortMedian().quick_sort(nums1, 0, len(nums1) - 1)
|
|
|
|
|
print("快速排序(中位基准数优化)完成后 nums = ", nums)
|
|
|
|
|
print("快速排序(中位基准数优化)完成后 nums =", nums)
|
|
|
|
|
|
|
|
|
|
# 快速排序(尾递归优化)
|
|
|
|
|
nums2 = [4, 1, 3, 1, 5, 2]
|
|
|
|
|
QuickSortTailCall().quick_sort(nums, 0, len(nums2) - 1)
|
|
|
|
|
print("快速排序(尾递归优化)完成后 nums = ", nums)
|
|
|
|
|
print("快速排序(尾递归优化)完成后 nums =", nums)
|
|
|
|
|