这里是Python的顺序查找实现: pythondef sequential_search(nums, target): for i in range(len(nums)): if nums[i] == target: return i return -1 分析: 1. 顺序查找也称为线性查找,它的工作原理是从序列的起始元素开...
这里是Python的快速排序实现: pythondef quick_sort(nums, l, r): if l = r: return i, j = l, r pivot = nums[l] while i j: while i j and nums[j] = pivot: j -= 1 nums[i] = nums[j] while i j and nums[i] = pivot: i += 1 nums[j] = nums[i]...
这里是Python的归并排序实现: pythondef merge_sort(nums): n = len(nums) if n 2: return nums mid = n // 2 left = merge_sort(nums[:mid]) right = merge_sort(nums[mid:]) return merge(left, right)def merge(left, right): tmp = [] i, j = 0...
这里是Python的选择排序实现: pythondef selection_sort(nums): n = len(nums) for i in range(n): min_idx = i for j in range(i+1, n): if nums[j] nums[min_idx]: min_idx = j nums[i], nums[min_idx] = nums[min_idx], nums[i] return numsprin...
这里是Python的冒泡排序实现: pythondef bubble_sort(nums): n = len(nums) for i in range(n): for j in range(0, n-i-1): if nums[j] nums[j+1]: nums[j], nums[j+1] = nums[j+1], nums[j] return numsprint(bubble_sort([5, 3, 2, 1]))# [1, 2,...