def partition_v2_print(arr, low, high): if low >= high: return -1 pi = low li = low + 1 ri = high print("original list: ", arr) while ri >= li: print("\n[in loop] -- pi: ", pi, "li: ", li, "ri: ", ri) if arr[li] > arr[pi]: swap(arr, ri, li) print("\nswapped list: ", arr) ri -= 1 else: li += 1 print("\n[out of loop] -- arr: ", arr) print("[out of loop] -- pi: ", pi, "li: ", li, "ri: ", ri) pi = li - 1 swap(arr, low, pi) print("\n[final] arr:", arr) print("[final] pi: ", pi) return pi
def selection_sort(arr): for i in range(0, len(arr)): min_position = i for j in range(i + 1, len(arr)): if arr[j] < arr[min_position]: min_position = j swap(arr, i, min_position) return
def insertion_sort(arr): if len(arr) == 1: return for i in range(1, len(arr)): for j in range(i, 0, -1): if arr[j] < arr[j - 1]: swap(arr, j, j - 1) else: break return
def insertion_sort(arr): if len(arr) == 1: # 因为要从第一个元素之后的元素迭代,所以如果整个序列长度为1,则直接返回 return for i in range(1, len(arr)): # 此处也是倒着访问List,但不是从尾巴开始的,而是从当前位置开始的,因为是两两交换,所以此处代码与bubbleSort有些相似 for j in range(i, 0, -1): if arr[j] < arr[j - 1]: swap(arr, j, j - 1) else: break return
def bubble_sort(arr): for i in range(0, len(arr) - 1): swapped = False for j in range(len(arr) - 1, i, -1): if arr[j] < arr[j - 1]: swap(arr, j, j - 1) swapped = True if not swapped: return return
def selection_sort(arr): # startPosition是本次迭代的起始位置下标,与前述步骤中的k相对应:startPosition == k - 1 for start_position in range(0, len(arr)): min_position = start_position # minPosition用来记录本次迭代中最小数值所在位置下标 # 和其后所有位置上的数字比较,如果有更小的数字,则用该位置替代当前的minPosition for i in range(start_position + 1, len(arr)): if (arr[i] < arr[min_position]): min_position = i # 经过一轮比较,当前的minPosition已经是当前待排序数字中的最小值,将它和本次迭代第一个位置上的数字交换 swap(arr, start_position, min_position) return
def partition_v2(arr, low, high): if low >= high: return -1 pi = low li = low + 1 ri = high while ri >= li: if arr[li] > arr[pi]: swap(arr, ri, li) ri -= 1 else: li += 1 pi = li - 1 swap(arr, low, pi) return pi
def bubble_sort(arr): for i in range(0, len(arr) - 1): for j in range(len(arr) - 1, i, -1): if arr[j] < arr[j - 1]: swap(arr, j, j - 1) return