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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
| from LinkedList import ListNode from LinkedList import LinkedList from typing import Optional
class LinkedListSort: @staticmethod def bubbleSort(head: ListNode): node_i = head tail = None while node_i.next: node_j = head flag = False while node_j and node_j.next != tail: if node_j.val > node_j.next.val: node_j.val, node_j.next.val = node_j.next.val, node_j.val flag = True node_j = node_j.next if not flag: return tail = node_j node_i = node_i.next return
@staticmethod def selectSort(head: ListNode): node_i = head while node_i and node_i.next: min_node = node_i node_j = node_i.next while node_j: if node_j.val < min_node.val: min_node = node_j node_j = node_j.next if min_node != node_i: min_node.val, node_i.val = node_i.val, min_node.val node_i = node_i.next return @staticmethod def insertSort(head: ListNode) -> Optional[ListNode]: if not head or not head.next: return head dummy_head = ListNode(-1, head) sorted_list = head curr = head.next while curr: if curr.val >= sorted_list.val: sorted_list = sorted_list.next else: prev = dummy_head while prev.next.val <= curr.val: prev = prev.next sorted_list.next = curr.next curr.next = prev.next prev.next = curr curr = sorted_list.next return dummy_head.next @staticmethod def merge(left: ListNode, right: ListNode): dummy_head = ListNode(-1) curr = dummy_head while left and right: if left.val <= right.val: curr.next = left left = left.next else: curr.next = right right = right.next curr = curr.next if left: curr.next = left elif right: curr.next = right return dummy_head.next @staticmethod def mergeSort(head: ListNode): if not head or not head.next: return head left, right = head, head.next while right and right.next: left = left.next right = right.next.next left_head, right_head = head, left.next left.next = None return LinkedListSort.merge(LinkedListSort.mergeSort(left_head), LinkedListSort.mergeSort(right_head)) @staticmethod def partition(left: ListNode, right: ListNode): if left == right or left.next == right: return left pivot = left.val node_i, node_j = left, left.next while node_j != right: if node_j.val < pivot: node_i = node_i.next node_i.val, node_j.val = node_j.val, node_i.val node_j = node_j.next left.val, node_i.val = node_i.val, left.val return node_i @staticmethod def quickSort(left: ListNode, right: Optional[ListNode]): if left == right or left.next == right: return left pi = LinkedListSort.partition(left, right) LinkedListSort.quickSort(left, pi) LinkedListSort.quickSort(pi.next, right) return left @staticmethod def countingSort(head: ListNode): list_min, list_max = float('inf'), float('-inf') curr = head while curr: if curr.val < list_min: list_min = curr.val elif curr.val > list_max: list_max = curr.val curr = curr.next
size = list_max - list_min + 1 curr = head counts = [ 0 for _ in range(size)] while curr: counts[curr.val-list_min] += 1 curr = curr.next print(counts, size, list_min) dummy_head = ListNode(-1) curr = dummy_head for i in range(size): while counts[i]: curr.next = ListNode(i+list_min) counts[i] -= 1 curr = curr.next return dummy_head.next @staticmethod def putinBucket(buckets, idx, val): if not buckets[idx]: buckets[idx] = ListNode(val) return node = ListNode(val, buckets[idx]) buckets[idx] = node @staticmethod def bucketSort(head: ListNode, bucket_size = 5): if not head: return head list_min, list_max = float('inf'), float('-inf') curr = head while curr: if curr.val < list_min: list_min = curr.val elif curr.val > list_max: list_max = curr.val curr = curr.next bucket_cnts = (list_max-list_min) // bucket_size + 1 buckets = [None for _ in range(bucket_cnts)] curr = head while curr: idx = (curr.val-list_min) // bucket_size LinkedListSort.putinBucket(buckets, idx, curr.val) curr = curr.next dummy_head = ListNode(-1) curr = dummy_head for bucket_head in buckets: bucket_curr = LinkedListSort.mergeSort(bucket_head) while bucket_curr: curr.next = bucket_curr bucket_curr = bucket_curr.next curr = curr.next return dummy_head.next def radixSort(head: ListNode): max_len = 0 curr = head while curr: max_len = max(max_len, len(str(curr.val))) curr = curr.next for i in range(max_len): curr = head buckets = [[] for i in range(10)] while curr: buckets[curr.val // (10**i) % 10].append(curr.val) curr = curr.next dummy_head = ListNode(-1) curr = dummy_head for bucket in buckets: for num in bucket: curr.next = ListNode(num) curr = curr.next head = dummy_head.next return head L = LinkedList() L.create([5, 3, 4, 2, 1, 7, 6, 8, 9])
res = LinkedListSort.radixSort(L.dummy_head.next) L.showList(res)
|