Last active
April 27, 2020 21:15
-
-
Save wiccy46/ff93a49e5d5ff47b6ecb07c0d96f0490 to your computer and use it in GitHub Desktop.
[data structure] A collection of data structure related snippet #python #datastructure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Quick-Union | |
worst case is still O(N^2) if tree too tall | |
""" | |
"""Find root""" | |
# id is your tree array | |
def root(i): | |
while i != id[i]: | |
id[i] = id[id[i]]; # one pass variant | |
i = id[i] | |
return i | |
def union(p, q): | |
i = root(p) | |
j = root(q) | |
id[i] = j | |
""" BIG O cheatsheet | |
----------------------+----------+------------+----------+--------------+ | |
| | Insert | Delete | Search | Space Usage | | |
+----------------------+----------+------------+----------+--------------+ | |
| Unsorted array | O(1) | O(1) | O(n) | O(n) | | |
| Value-indexed array | O(1) | O(1) | O(1) | O(n) | | |
| Sorted array | O(n) | O(n) | O(log n) | O(n) | | |
| Unsorted linked list | O(1)* | O(1)* | O(n) | O(n) | | |
| Sorted linked list | O(n)* | O(1)* | O(n) | O(n) | | |
| Balanced binary tree | O(log n) | O(log n) | O(log n) | O(n) | | |
| Heap | O(log n) | O(log n)** | O(n) | O(n) | | |
| Hash table | O(1) | O(1) | O(1) | O(n) | | |
+----------------------+----------+------------+----------+--------------+ | |
* The cost to add or delete an element into a known location in the list | |
(i.e. if you have an iterator to the location) is O(1). If you don't | |
know the location, then you need to traverse the list to the location | |
of deletion/insertion, which takes O(n) time. | |
** The deletion cost is O(log n) for the minimum or maximum, O(n) for an | |
arbitrary element. | |
""" | |
"""Heapsort | |
""" | |
def heapify(arr, n, i): | |
largest = i # Initialize largest as root | |
l = 2 * i + 1 # left = 2*i + 1 | |
r = 2 * i + 2 # right = 2*i + 2 | |
# See if left child of root exists and is | |
# greater than root | |
if l < n and arr[i] < arr[l]: | |
largest = l | |
# See if right child of root exists and is | |
# greater than root | |
if r < n and arr[largest] < arr[r]: | |
largest = r | |
# Change root, if needed | |
if largest != i: | |
arr[i],arr[largest] = arr[largest],arr[i] # swap | |
# Heapify the root. | |
heapify(arr, n, largest) | |
# The main function to sort an array of given size | |
def heapSort(arr): | |
n = len(arr) | |
# Build a maxheap. | |
# Basically heapify find the largest. | |
for i in range(n, -1, -1): | |
heapify(arr, n, i) | |
# One by one extract elements | |
for i in range(n-1, 0, -1): | |
arr[i], arr[0] = arr[0], arr[i] # swap | |
heapify(arr, i, 0) | |
# Driver code to test above | |
arr = [ 12, 11, 13, 5, 6, 7] | |
heapSort(arr) # Inplace peration |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment