def bubbleSort(nums):
size = len(nums)
for i in range(size):
for j in range(1, size - 1):
if nums[j - 1] > nums[j]:
nums[j - 1], nums[j] = nums[j], nums[j - 1]
return nums
def insertSort(nums):
size = len(nums)
for i in range(size - 1):
while i >= 0 and nums[i + 1] < nums[i]:
nums[i + 1], nums[i] = nums[i], nums[i + 1]
i -= 1
return nums
def shellSort(nums):
size = len(nums)
gap = size // 2
while gap > 0:
for i in range(gap, size):
for j in range(i, gap - 1, -gap):
if nums[j] < nums[j - gap]:
nums[j], nums[j - gap] = nums[j - gap], nums[j]
else:
break
gap //= 2
return nums
def selectSort(nums):
size = len(nums)
for i in range(size - 1):
minIndex = i
for j in range(i + 1, size):
if nums[j] < nums[minIndex]:
minIndex = j
nums[i], nums[minIndex] = nums[minIndex], nums[i]
return nums
globalNums = [5, 3, 6, 4, 1, 2, 8, 7, 9]
def quickSort(nums):
size = len(nums)
if size <= 1:
return nums
pivot = nums[0]
left = [nums[i] for i in range(1, size) if nums[i] < pivot]
right = [nums[i] for i in range(1, size) if nums[i] >= pivot]
return quickSort(left) + [pivot] + quickSort(right)
python排序算法
最新推荐文章于 2024-08-26 18:56:03 发布