diff --git a/content/post/dsa.md b/content/post/dsa.md new file mode 100644 index 0000000..6a672ba --- /dev/null +++ b/content/post/dsa.md @@ -0,0 +1,251 @@ +--- +title: "DSA - Data Structures" +description: "Data Structures" +date: 2024-09-26T12:00:00+01:00 +draft: false +--- + +# Arrays + +{{< highlight java >}} +E[] array = new E[10]; +E[] array = new E[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; +int n = array.length; + +Arrays.sort(array); +Arrays.sort(array, Collections.reverseOrder()); +Arrays.sort(array, (E e1, E e2) -> e1 - e2); + +int index = Arrays.binarySearch(array, byte element); +{{< / highlight >}} + +__Running times__: + +| Operation | Time Complexity | +| --------- | --------------- | +| Access | O(1) | +| Search | O(n) | +| Insertion | O(n) | +| Deletion | O(n) | + + +# Lists + +__ArrayList__ + +{{< highlight java >}} +ArrayList list = new ArrayList<>(); +int n = list.size(); + +list.add(E element); +list.add(int index, E element); +list.addAll(Collection collection); +list.addAll(int index, Collection collection); + +E element = list.get(int index); + +E element = list.remove(int index); +list.clear(); + +boolean isEmpty = list.isEmpty(); +boolean contains = list.contains(E element); +int index = list.indexOf(E element); + +Collections.sort(list); +Collections.sort(list, Collections.reverseOrder()); +Collections.sort(list, (E e1, E e2) -> e1 - e2); + +int index = Collections.binarySearch(list, E element); +{{< / highlight >}} + +__Running times__: + +| Operation | Time Complexity | +| --------- | --------------- | +| Access | O(1) | +| Search | O(n) | +| Insertion | O(n) | +| Deletion | O(n) | + +# Queues + +__ArrayDeque__ + +{{< highlight java >}} +ArrayDeque deque = new ArrayDeque<>(); +int n = deque.size(); + +deque.addFirst(E element); +deque.addLast(E element); + +E first = deque.peekFirst(); +E last = deque.peekLast(); + +E first = deque.removeFirst(); +E last = deque.removeLast(); + +deque.clear(); + +boolean isEmpty = deque.isEmpty(); +boolean contains = deque.contains(E element); +{{< / highlight >}} + +__Running times__: + +| Operation | Time Complexity | +| ---------- | --------------- | +| Insertion | O(1) | +| Deletion | O(1) | +| Access | O(n) | + +__PriorityQueue__ (Heap Implementation) + +{{< highlight java >}} +PriorityQueue priorityQueue = new PriorityQueue<>(); +PriorityQueue priorityQueue = new PriorityQueue<>(Collections.reverseOrder()); +PriorityQueue priorityQueue = new PriorityQueue<>((E e1, E e2) -> e1 - e2); +int size = priorityQueue.size(); + +priorityQueue.add(E element); + +E peek = priorityQueue.peek(); + +E element = priorityQueue.poll(); + +boolean isEmpty = priorityQueue.isEmpty(); +boolean contains = priorityQueue.contains(E element); +{{< / highlight >}} + +__Running times__: + +| Operation | Time Complexity | +| --------- | --------------- | +| Insertion | O(log n) | +| Deletion | O(log n) | +| Access | O(1) | + +# Sets + +__HashSet__ + +{{< highlight java >}} +HashSet set = new HashSet<>(); +int n = set.size(); + +set.add(E element); + +set.remove(E element); +set.clear(); + +boolean isEmpty = set.isEmpty(); +boolean contains = set.contains(E element); +{{< / highlight >}} + +__Running times__: + +| Operation | Time Complexity | +| --------- | --------------- | +| Insertion | O(1) | +| Deletion | O(1) | +| Search | O(1) | + +__TreeSet__ + +{{< highlight java >}} +TreeSet set = new TreeSet<>(); +TreeSet set = new TreeSet<>(Collections.reverseOrder()); +TreeSet set = new TreeSet<>((E e1, E e2) -> e1 - e2); +int n = set.size(); + +set.add(E element); + +E first = set.first(); +E last = set.last(); + +E before = set.lower(E e); +E after = set.higher(E e); + +set.remove(E element); +set.clear(); + +TreeSet sub = set.subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive); +TreeSet tail = set.tailSet(E fromElement, boolean inclusive); +TreeSet head = set.headSet(E toElement, boolean inclusive); + +boolean isEmpty = set.isEmpty(); +boolean contains = set.contains(E element); +{{< / highlight >}} + +__Running times__: + +| Operation | Time Complexity | +| --------- | --------------- | +| Insertion | O(log n) | +| Deletion | O(log n) | +| Search | O(log n) | + +# Maps + +__HashMap__ + +{{< highlight java >}} +HashMap map = new HashMap<>(); +int n = map.size(); + +map.put(K key, V value); + +V value = map.get(K key); + +map.remove(K key); +map.clear(); + +boolean isEmpty = map.isEmpty(); +boolean containsKey = map.containsKey(K key); +boolean containsValue = map.containsValue(V value); +{{< / highlight >}} + +__Running times__: + +| Operation | Time Complexity | +| --------- | --------------- | +| Insertion | O(1) | +| Deletion | O(1) | +| Search | O(1) | + +__TreeMap__ + +{{< highlight java >}} +TreeMap map = new TreeMap<>(); +TreeMap map = new TreeMap<>(Collections.reverseOrder()); +TreeMap map = new TreeMap<>((K k1, K k2) -> k1 - k2); +int n = map.size(); + +map.put(K key, V value); + +V value = map.get(K key); + +K firstKey = map.firstKey(); +K lastKey = map.lastKey(); + +K beforeKey = map.lowerKey(K key); +K afterKey = map.higherKey(K key); + +map.remove(K key); +map.clear(); + +TreeMap sub = subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive); +TreeMap tail = tailMap(K fromKey, boolean inclusive); +TreeMap head = headMap(K toKey, boolean inclusive); + +boolean isEmpty = map.isEmpty(); +boolean containsKey = map.containsKey(K key); +boolean containsValue = map.containsValue(V value); +{{< / highlight >}} + +__Running times__: + +| Operation | Time Complexity | +| --------- | --------------- | +| Insertion | O(log n) | +| Deletion | O(log n) | +| Search | O(log n) |