快速排序(python版)

快速排序

1、描述

通过一趟排序将待排记录分隔成独立的两部分,其中一部分记录的关键字均比另一部分的关键字小,则可分别对这两部分 记录继续进行排序,以达到整个序列有序。

2、思想

分治

3、代码
import random
def quick_sort(nums, start, end):
    if start >= end:
        return
    low = start
    mid = nums[start]
    high = end
    while low < high:
        while low < high and mid <= nums[high]:
            high -= 1
        nums[low] = nums[high]
        while low < high and mid > nums[low]:
            low += 1
        nums[high] = nums[low]
    nums[low] = mid 
    quick_sort(nums, start, low-1)
    quick_sort(nums, low + 1, end)
   
nums = []
for i in range(10):nums.append(random.randint(1,10))
print("the raw nums is:", nums)
quick_sort(nums, 0, len(nums) - 1)
print("the sorted nums is:", nums)

你可能感兴趣的:(数据结构)