1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
| def bubble_sort(arr): n = len(arr) for i in range(n - 1): for j in range(n - 1 - i): if arr[j] > arr[j + 1]: arr[j], arr[j + 1] = arr[j + 1], arr[j] return arr
def insertion_sort(arr): n = len(arr) for i in range(1, n): key = arr[i] j = i - 1 while j >= 0 and arr[j] > key: arr[j + 1] = arr[j] j -= 1 arr[j + 1] = key return arr
def selection_sort(arr): n = len(arr) for i in range(n - 1): min_index = i for j in range(i + 1, n): if arr[j] < arr[min_index]: min_index = j arr[i], arr[min_index] = arr[min_index], arr[i] return arr
def quick_sort(arr): if len(arr) <= 1: return arr pivot = arr[0] left = [x for x in arr[1:] if x <= pivot] right = [x for x in arr[1:] if x > pivot] return quick_sort(left) + [pivot] + quick_sort(right)
def quick_sort(arr, l, r): if l >= r: return less, p = partition(arr, l, r) quick_sort(arr, l, less - 1) quick_sort(arr, p, r)
def partition(arr, l, r): random_idx = random.randint(l, r) arr[l], arr[random_idx] = arr[random_idx], arr[l] piovt = arr[l] i, j = l + 1, r + 1 less = l while i < j: if arr[i] < piovt: less += 1 arr[i], arr[less] = arr[less], arr[i] i += 1 elif arr[i] == piovt: i += 1 elif arr[i] > piovt: j -= 1 arr[i], arr[j] = arr[j], arr[i] arr[l], arr[less] = arr[less], arr[l] return less, j
def quick_sort(arr, l, r): if l >= r: return p = partition(arr, l, r) quick_sort(arr, l, p - 1) quick_sort(arr, p + 1, r)
def partition(arr, l, r): random_idx = random.randint(l, r) arr[l], arr[random_idx] = arr[random_idx], arr[l] piovt = arr[l] i, j = l + 1, r while i <= j: while i < r and arr[i] <= pivot: i += 1 while j > l and arr[j] > pivot: j -= 1 if i >= j: break arr[i], arr[j] = arr[j], arr[i] arr[l], arr[j] = arr[j], arr[l] return j
quick_sort(nums, 0, len(nums) - 1) return nums
def merge_sort(arr): if len(arr) <= 1: return arr mid = len(arr) // 2 left = merge_sort(arr[:mid]) right = merge_sort(arr[mid:]) return merge(left, right)
def merge(left, right): merged = [] i = j = 0 while i < len(left) and j < len(right): if left[i] <= right[j]: merged.append(left[i]) i += 1 else: merged.append(right[j]) j += 1 while i < len(left): merged.append(left[i]) i += 1 while j < len(right): merged.append(right[j]) j += 1 return merged
def heapify(arr, n, i): largest = i left = 2 * i + 1 right = 2 * i + 2
if left < n and arr[left] > arr[largest]: largest = left
if right < n and arr[right] > arr[largest]: largest = right
if largest != i: arr[i], arr[largest] = arr[largest], arr[i] heapify(arr, n, largest)
def heap_sort(arr): n = len(arr)
for i in range(n // 2 - 1, -1, -1): heapify(arr, n, i)
for i in range(n - 1, 0, -1): arr[i], arr[0] = arr[0], arr[i] heapify(arr, i, 0)
return arr
def shell_sort(arr): n = len(arr) gap = n // 2
while gap > 0: for i in range(gap, n): temp = arr[i] j = i while j >= gap and arr[j - gap] > temp: arr[j] = arr[j - gap] j -= gap arr[j] = temp gap //= 2
return arr
|