sort
This commit is contained in:
parent
43fd01a62f
commit
818fb54efb
@ -7,6 +7,7 @@ Author: timi (xisunyy@163.com)
|
|||||||
from include import *
|
from include import *
|
||||||
import sys
|
import sys
|
||||||
import os.path as osp
|
import os.path as osp
|
||||||
|
|
||||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||||
|
|
||||||
"""冒泡排序"""
|
"""冒泡排序"""
|
||||||
|
@ -7,6 +7,7 @@ Author: timi (xisunyy@163.com)
|
|||||||
from include import *
|
from include import *
|
||||||
import sys
|
import sys
|
||||||
import os.path as osp
|
import os.path as osp
|
||||||
|
|
||||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||||
|
|
||||||
"""插入排序"""
|
"""插入排序"""
|
||||||
|
@ -4,12 +4,11 @@ Created Time: 2022-11-25
|
|||||||
Author: timi (xisunyy@163.com)
|
Author: timi (xisunyy@163.com)
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import copy
|
|
||||||
from include import *
|
from include import *
|
||||||
import sys
|
import sys
|
||||||
import os.path as osp
|
import os.path as osp
|
||||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
|
||||||
|
|
||||||
|
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||||
|
|
||||||
"""
|
"""
|
||||||
合并左子数组和右子数组
|
合并左子数组和右子数组
|
||||||
@ -18,7 +17,7 @@ sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
|||||||
"""
|
"""
|
||||||
def merge(nums, left, mid, right):
|
def merge(nums, left, mid, right):
|
||||||
# 初始化辅助数组 借助 copy模块
|
# 初始化辅助数组 借助 copy模块
|
||||||
tmp = copy.deepcopy(nums[left:right+1])
|
tmp = nums[left:right + 1]
|
||||||
# 左子数组的起始索引和结束索引
|
# 左子数组的起始索引和结束索引
|
||||||
leftStart, leftEnd = left - left, mid - left
|
leftStart, leftEnd = left - left, mid - left
|
||||||
# 右子数组的起始索引和结束索引
|
# 右子数组的起始索引和结束索引
|
||||||
|
@ -7,12 +7,11 @@ Author: timi (xisunyy@163.com)
|
|||||||
from include import *
|
from include import *
|
||||||
import sys
|
import sys
|
||||||
import os.path as osp
|
import os.path as osp
|
||||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
|
||||||
|
|
||||||
|
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||||
|
|
||||||
"""快速排序类"""
|
"""快速排序类"""
|
||||||
class quick_sort(object):
|
class quick_sort(object):
|
||||||
|
|
||||||
"""哨兵划分"""
|
"""哨兵划分"""
|
||||||
def partition(self, nums, left, right):
|
def partition(self, nums, left, right):
|
||||||
# 以 nums[left] 作为基准数
|
# 以 nums[left] 作为基准数
|
||||||
@ -43,7 +42,7 @@ class quick_sort(object):
|
|||||||
"""快速排序类(中位基准数优化)"""
|
"""快速排序类(中位基准数优化)"""
|
||||||
class quick_sort_median():
|
class quick_sort_median():
|
||||||
|
|
||||||
# 选取三个元素的中位数
|
"""选取三个元素的中位数"""
|
||||||
def median_three(self, nums, left, mid, right):
|
def median_three(self, nums, left, mid, right):
|
||||||
# 使用了异或操作来简化代码
|
# 使用了异或操作来简化代码
|
||||||
# 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1
|
# 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1
|
||||||
@ -85,7 +84,6 @@ class quick_sort_median():
|
|||||||
|
|
||||||
"""快速排序类(尾递归优化)"""
|
"""快速排序类(尾递归优化)"""
|
||||||
class quick_sort_tail_call():
|
class quick_sort_tail_call():
|
||||||
|
|
||||||
"""哨兵划分"""
|
"""哨兵划分"""
|
||||||
def partition(self, nums, left, right):
|
def partition(self, nums, left, right):
|
||||||
# 以 nums[left] 作为基准数
|
# 以 nums[left] 作为基准数
|
||||||
@ -115,6 +113,7 @@ class quick_sort_tail_call():
|
|||||||
self.quick_sort(nums, pivot + 1, right) # 递归排序右子数组
|
self.quick_sort(nums, pivot + 1, right) # 递归排序右子数组
|
||||||
right = pivot - 1 # 剩余待排序区间为 [left, pivot - 1]
|
right = pivot - 1 # 剩余待排序区间为 [left, pivot - 1]
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# 快速排序
|
# 快速排序
|
||||||
nums = [4, 1, 3, 1, 5, 2]
|
nums = [4, 1, 3, 1, 5, 2]
|
||||||
|
Loading…
Reference in New Issue
Block a user