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
| class SelectSort(): def __init__(self, arr): self.start(arr) def swap(self, arr, a, b): temp = arr[a] arr[a] = arr[b] arr[b] = temp def start(self, arr): length = len(arr) for i in range(length): the_min = i for j in range(i + 1, length): if arr[j] < arr[the_min]: the_min = j self.swap(arr, the_min, i) class QuickSort(): def __init__(self, arr): self.start(arr, 0, len(arr) - 1) def swap(self, arr, a, b): temp = arr[a] arr[a] = arr[b] arr[b] = temp def start(self, array, low, high): if low > high: return pivot = low left_p = low right_p = high while left_p < right_p: while array[right_p] >= array[pivot] and left_p < right_p: right_p -= 1 while array[left_p] <= array[pivot] and left_p < right_p: left_p += 1 if left_p < right_p: self.swap(array, right_p, left_p) self.swap(array, pivot, left_p) self.start(array, low, left_p - 1) self.start(array, left_p + 1, high) class ShellSort(): def __init__(self, arr): self.start(arr) def start(self, arr): n = len(arr) gap = int(n / 2) while gap > 0: for i in range(gap, n): temp = arr[i] j = i - gap while j >= 0 and arr[j] > temp: arr[j + gap] = arr[j] j = j - gap arr[j + gap] = temp gap = int(gap / 2) class HeapSort(): def __init__(self, arr): self.start(arr) def swap(self, arr, a, b): temp = arr[a] arr[a] = arr[b] arr[b] = temp def Percdown(self, arr, now, length): replace = now * 2 while replace < length: right_child = replace + 1 if right_child < length and arr[replace] < arr[right_child]: replace = right_child if arr[replace] > arr[now]: self.swap(arr, replace, now) now = replace replace = now * 2 else: break def start(self, array): length = len(array) for i in range(length // 2, -1, -1): self.Percdown(array, i, length) for i in range(length - 1, -1, -1): self.swap(array, 0, i) self.Percdown(array, 0, i) class RadixSort(): def __init__(self, arrx): self.start(arrx) def start(self, arr): buckets = [] for i in range(10): buckets.append([]) for i in range(6): for j in arr: buckets[(j // int(pow(10, i))) % 10].append(j) arr.clear() for k in buckets: for z in k: arr.append(z) for u in buckets: u.clear() class MergeSort(): def __init__(self, arr): self.start(arr,0,len(arr)-1) def start(self, array,low,high): if low
|