1 快速排序
参考:https://blog.csdn.net/weixin_43250623/article/details/88931925
def quick_sort(nums, start, end):
if start > end:
return
mid = nums[start]
low = start
high = end
while low < high:
while low < high and nums[high] > mid:
high = high - 1
nums[low] = nums[high]
while low < high and nums[low] < mid:
low = low + 1
nums[high] = nums[low]
nums[low] = mid
quick_sort(nums, start, low - 1)
quick_sort(nums, low + 1, end)
if __name__ == "__main__":
a = [10, 9, 8, 7, 6]
quick_sort(a, 0, len(a) - 1)
print a
2 归并排序
参考:https://blog.csdn.net/hellojoy/article/details/81835559
def merge(left, right):
retData = []
i = 0
j = 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
retData.append(left[i])
i = i + 1
else:
retData.append(right[j])
j = j + 1
while i < len(left):
retData.append(left[i])
i = i + 1
while j < len(right):
retData.append(right[j])
j = j + 1
return retData
def merge_sort(nums):
if len(nums) == 1:
return nums
mid = len(nums) / 2
left = merge_sort(nums[0:mid])
right = merge_sort(nums[mid: len(nums)])
return merge(left, right)
if __name__ == "__main__":
a = [10, 9, 8]
b = merge_sort(a)
print b
3 动态规划
最关键的是写出状态转换方程,也就是递归方程。
参考:https://blog.csdn.net/zw6161080123/article/details/80639932
面试题:
两个有序数组合并成一个
两个有序链表合并成一个
链表反转
删除所有的驼峰字符串