diff --git "a/\\" "b/\\" new file mode 100644 index 0000000..39844ec --- /dev/null +++ "b/\\" @@ -0,0 +1,77 @@ +#!/usr/bin/env python3 + +import sys + +def heap_extract_max(A): + m = A[0] + A[0] = A.pop() + + size = len(A) + i = 0 + exit = not size == 0 + while exit: + left = heap_left(size, i) + print("left =", left) + if left == None: + break + + right = heap_right(size, i) + print("right =", right) + if right == None: + if A[i] < A[left]: + A[i], A[left] = A[left], A[i] + break + + ma = max_3(A[i], A[left], A[right]) + if ma == A[i]: + break + elif ma == A[left]: + A[i], A[left] = A[left], A[i] + i = left + else: + A[i], A[right] = A[right], A[i] + i = right + + return m + +def max_3(a, b, c): + if a > b: + if a > c: + return a + else: + return c + else: + if b > c: + return b + else: + return c + +def heap_parent(n): + n = n + 1 + if n == 1: + return None + else: + return (n // 2) - 1 + +def heap_left(size, n): + n = n + 1 + l = n * 2 + if l > size: + return None + else: + return l - 1 + +def heap_right(size, n): + n = n + 1 + l = n * 2 + 1 + if l > size: + return None + else: + return l - 1 + +if __name__ == "__main__": + args = [int(x) for x in sys.argv[1:]] + print(args) + print(heap_extract_max(args)) + print(args) + diff --git a/heapsort.py b/heapsort.py index e4a4fc9..e442cd9 100755 --- a/heapsort.py +++ b/heapsort.py @@ -1,24 +1,41 @@ #!/usr/bin/env python3 import sys +import math -def heap_extract_max(A): - m = A[0] - A[0] = A.pop() +def heap_sort(A): + heap_build_max_heap(A) - size = len(A) + print("Initial heap:") + heap_print(A, len(A)) + print() + + for i in range(len(A)-1, 0, -1): + A[0], A[i] = A[i], A[0] + heap_max_heapify(A, i) + + print("\nNew heap: length =", i) + heap_print(A, i) + print() + +def heap_max_heapify(A, size): + if size < 2: + return + + print("size =", size) i = 0 - exit = size == 0 - while exit: - left = heap_left(i) - if left == None: - break - right = heap_right(i) - if right == None: + left = heap_left(size, i) + print("left =", left) + + while left is not None: + right = heap_right(size, i) + print("right =", right) + + if right is None: if A[i] < A[left]: A[i], A[left] = A[left], A[i] - break + break ma = max_3(A[i], A[left], A[right]) if ma == A[i]: @@ -30,7 +47,38 @@ def heap_extract_max(A): A[i], A[right] = A[right], A[i] i = right - return m + left = heap_left(size, i) + print("left =", left) + + +def heap_build_max_heap(A): + if len(A) < 2: + return + for i in range(math.ceil(len(A)/2), 1, -1): + heap_max_heapify(A, i) + + +def heap_print(A, size): + i = 1 + newline = False + while i <= size: + print(A[i-1], end='\t') + newline = False + i = i + 1 + if is_power_2(i): + print() + newline = True + if not newline: + print() + + +def is_power_2(n): + while n > 1: + if n % 2 == 1: + return False + n = n // 2 + return True + def max_3(a, b, c): if a > b: @@ -44,29 +92,35 @@ def max_3(a, b, c): else: return c + def heap_parent(n): n = n + 1 if n == 1: return None - else + else: return (n // 2) - 1 + def heap_left(size, n): n = n + 1 l = n * 2 - if l > size + if l > size: return None - else + else: return l - 1 + def heap_right(size, n): n = n + 1 l = n * 2 + 1 - if l > size + if l > size: return None - else + else: return l - 1 + if __name__ == "__main__": args = [int(x) for x in sys.argv[1:]] + heap_sort(args) + print(args) diff --git a/notes.md b/notes.md index 402579a..294312b 100644 --- a/notes.md +++ b/notes.md @@ -143,3 +143,37 @@ def right(x): **Max heap property**: for all i > 1 A[parent(i)] >= A[i] +# Data structures + +Way to organize information + +A data structure has an interface (functions to work with the DS) + +A data structure has data and meta-data (like size, length). + +## Stack (LIFO) + +### Operations: +- `push(S, x)` (put one element, move TOS) +- `pop(S)` (remove element in TOS, move TOS) +- `stack-empty(S)` (returns TRUE if stack is empty) + +## Queue (FIFO) + +## Structure + +- Based on array +- `length` +- `head` +- `tail` + +Queue has always 1 cell free to avoid confusion with full/empty + +## Dictionary + +Data structure for fast search + +## Direct-access table + +Implementation of dictionary +