This commit is contained in:
NIngCoder 2022-11-26 00:46:30 +08:00
parent 43fd01a62f
commit 818fb54efb
4 changed files with 45 additions and 45 deletions

View file

@ -7,31 +7,32 @@ Author: timi (xisunyy@163.com)
from include import *
import sys
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
for i in range(n-1, -1, -1):
for i in range(n - 1, -1, -1):
# 内循环:冒泡操作
for j in range(i):
# 交换 nums[j] 与 nums[j + 1]
if nums[j] > nums[j+1]:
nums[j], nums[j+1] = nums[j+1], nums[j]
if nums[j] > nums[j + 1]:
nums[j], nums[j + 1] = nums[j + 1], nums[j]
"""冒泡排序(标志优化)"""
def bubble_sort_with_flag(nums):
n = len(nums)
# 外循环:待排序元素数量为 n-1, n-2, ..., 1
for i in range(n-1, -1, -1):
for i in range(n - 1, -1, -1):
flag = False # 初始化标志位
# 内循环:冒泡操作
for j in range(i):
# 交换 nums[j] 与 nums[j + 1]
if nums[j] > nums[j+1]:
nums[j], nums[j+1] = nums[j+1], nums[j]
if nums[j] > nums[j + 1]:
nums[j], nums[j + 1] = nums[j + 1], nums[j]
flag = True # 记录交换元素
if not flag:
break # 此轮冒泡未交换任何元素,直接跳出

View file

@ -7,6 +7,7 @@ Author: timi (xisunyy@163.com)
from include import *
import sys
import os.path as osp
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
"""插入排序"""
@ -14,12 +15,12 @@ def insertion_sort(nums):
# 外循环base = nums[1], nums[2], ..., nums[n-1]
for i in range(1, len(nums)):
base = nums[i]
j = i-1
j = i - 1
# 内循环:将 base 插入到左边的正确位置
while j >= 0 and nums[j] > base:
nums[j+1] = nums[j] # 1. 将 nums[j] 向右移动一位
nums[j + 1] = nums[j] # 1. 将 nums[j] 向右移动一位
j -= 1
nums[j+1] = base # 2. 将 base 赋值到正确位置
nums[j + 1] = base # 2. 将 base 赋值到正确位置
if __name__ == '__main__':

View file

@ -4,12 +4,11 @@ Created Time: 2022-11-25
Author: timi (xisunyy@163.com)
'''
import copy
from include import *
import sys
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,15 +17,15 @@ sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
"""
def merge(nums, left, mid, right):
# 初始化辅助数组 借助 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
# 右子数组的起始索引和结束索引
rightStart, rightEnd = mid + 1 - left, right - left
# i, j 分别指向左子数组、右子数组的首元素
i, j = leftStart, rightStart
# 通过覆盖原数组 nums 来合并左子数组和右子数组
for k in range(left, right+1):
for k in range(left, right + 1):
# 若 “左子数组已全部合并完”,则选取右子数组元素,并且 j++
if i > leftEnd:
nums[k] = tmp[j]
@ -45,16 +44,16 @@ def merge(nums, left, mid, right):
def merge_sort(nums, left, right):
# 终止条件
if left >= right:
return # 当子数组长度为 1 时终止递归
return # 当子数组长度为 1 时终止递归
# 划分阶段
mid = left + right >> 1 # 计算中点
merge_sort(nums, left, mid) # 递归左子数组
merge_sort(nums, mid + 1, right) # 递归右子数组
merge_sort(nums, left, mid) # 递归左子数组
merge_sort(nums, mid + 1, right) # 递归右子数组
# 合并阶段
merge(nums, left, mid, right)
if __name__ == '__main__':
nums = [4, 1, 3, 1, 5, 2]
merge_sort(nums, 0, len(nums)-1)
merge_sort(nums, 0, len(nums) - 1)
print("归并排序完成后 nums = ", nums)

View file

@ -7,21 +7,20 @@ Author: timi (xisunyy@163.com)
from include import *
import sys
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):
"""哨兵划分"""
def partition(self, nums, left, right):
# 以 nums[left] 作为基准数
i, j = left, right
while i < j:
while i < j and nums[j] >= nums[left]:
j -= 1 # 从右向左找首个小于基准数的元素
j -= 1 # 从右向左找首个小于基准数的元素
while i < j and nums[i] <= nums[left]:
i += 1 # 从左向右找首个大于基准数的元素
i += 1 # 从左向右找首个大于基准数的元素
# 元素交换
nums[i], nums[j] = nums[j], nums[i]
# 将基准数交换至两子数组的分界线
@ -36,14 +35,14 @@ class quick_sort(object):
# 哨兵划分
pivot = self.partition(nums, left, right)
# 递归左子数组、右子数组
self.quick_sort(nums, left, pivot-1)
self.quick_sort(nums, pivot+1, right)
self.quick_sort(nums, left, pivot - 1)
self.quick_sort(nums, pivot + 1, right)
"""快速排序类(中位基准数优化)"""
class quick_sort_median():
# 选取三个元素的中位数
"""选取三个元素的中位数"""
def median_three(self, nums, left, mid, right):
# 使用了异或操作来简化代码
# 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1
@ -56,45 +55,44 @@ class quick_sort_median():
"""哨兵划分(三数取中值)"""
def partition(self, nums, left, right):
# 以 nums[left] 作为基准数
med = self.median_three(nums, left, (left+right)//2, right)
med = self.median_three(nums, left, (left + right) // 2, right)
# 将中位数交换至数组最左端
nums[left], nums[med] = nums[med], nums[left]
# 以 nums[left] 作为基准数
i, j = left, right
while i < j:
while i < j and nums[j] >= nums[left]:
j -= 1 # 从右向左找首个小于基准数的元素
j -= 1 # 从右向左找首个小于基准数的元素
while i < j and nums[i] <= nums[left]:
i += 1 # 从左向右找首个大于基准数的元素
i += 1 # 从左向右找首个大于基准数的元素
# 元素交换
nums[i], nums[j] = nums[j], nums[i]
# 将基准数交换至两子数组的分界线
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):
# 子数组长度为 1 时终止递归
if left >= right:return
if left >= right: return
# 哨兵划分
pivot = self.partition(nums, left, right)
# 递归左子数组、右子数组
self.quick_sort(nums, left, pivot-1)
self.quick_sort(nums, pivot+1, right)
self.quick_sort(nums, left, pivot - 1)
self.quick_sort(nums, pivot + 1, right)
"""快速排序类(尾递归优化)"""
class quick_sort_tail_call():
"""哨兵划分"""
def partition(self, nums, left, right):
# 以 nums[left] 作为基准数
i, j = left, right
while i < j:
while i < j and nums[j] >= nums[left]:
j -= 1 # 从右向左找首个小于基准数的元素
j -= 1 # 从右向左找首个小于基准数的元素
while i < j and nums[i] <= nums[left]:
i += 1 # 从左向右找首个大于基准数的元素
i += 1 # 从左向右找首个大于基准数的元素
# 元素交换
nums[i], nums[j] = nums[j], nums[i]
# 将基准数交换至两子数组的分界线
@ -108,25 +106,26 @@ class quick_sort_tail_call():
# 哨兵划分操作
pivot = self.partition(nums, left, right)
# 对两个子数组中较短的那个执行快排
if pivot-left < right-pivot:
self.quick_sort(nums, left, pivot-1) # 递归排序左子数组
left = pivot+1 # 剩余待排序区间为 [pivot + 1, right]
if pivot - left < right - pivot:
self.quick_sort(nums, left, pivot - 1) # 递归排序左子数组
left = pivot + 1 # 剩余待排序区间为 [pivot + 1, right]
else:
self.quick_sort(nums, pivot+1, right) # 递归排序右子数组
right = pivot-1 # 剩余待排序区间为 [left, pivot - 1]
self.quick_sort(nums, pivot + 1, right) # 递归排序右子数组
right = pivot - 1 # 剩余待排序区间为 [left, pivot - 1]
if __name__ == '__main__':
# 快速排序
nums = [4, 1, 3, 1, 5, 2]
quick_sort().quick_sort(nums, 0, len(nums)-1)
quick_sort().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)
quick_sort_median().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)
quick_sort_tail_call().quick_sort(nums, 0, len(nums2) - 1)
print("快速排序(尾递归优化)完成后 nums = ", nums)