diff --git a/izzivi/.idea/inspectionProfiles/Project_Default.xml b/izzivi/.idea/inspectionProfiles/Project_Default.xml index 581c216..5f2e5cd 100644 --- a/izzivi/.idea/inspectionProfiles/Project_Default.xml +++ b/izzivi/.idea/inspectionProfiles/Project_Default.xml @@ -8,5 +8,6 @@ + \ No newline at end of file diff --git a/izzivi/src/izziv4/Izziv4.java b/izzivi/src/izziv4/Izziv4.java new file mode 100644 index 0000000..6a4bfa1 --- /dev/null +++ b/izzivi/src/izziv4/Izziv4.java @@ -0,0 +1,400 @@ +package izziv4; + +class CollectionException extends Exception { + public CollectionException(String msg) { + super(msg); + } +} + +interface Collection { + static final String ERR_MSG_EMPTY = "Collection is empty."; + + boolean isEmpty(); + + int size(); + + String toString(); +} + +interface Queue extends Collection { + T front() throws CollectionException; + + void enqueue(T x); + + T dequeue() throws CollectionException; +} + +interface PriorityQueue extends Queue { +} + +@SuppressWarnings("ALL") +class ArrayPQ implements PriorityQueue { + T[] array = (T[]) new Comparable[64]; + int size = 0; + int moves = 0; + int compares = 0; + + void resize() { + T[] newArray = (T[]) new Comparable[array.length * 2]; + System.arraycopy(array, 0, newArray, 0, array.length); + array = newArray; + } + + @Override + public T front() throws CollectionException { + if (isEmpty()) { + throw new CollectionException(ERR_MSG_EMPTY); + } + T max = array[0]; + for (int i = 0; i < size; i++) { + compares++; + if (array[i].compareTo(max) > 0) { + max = array[i]; + } + } + return max; + } + + @Override + public void enqueue(T x) { + if (size == array.length) { + resize(); + } + moves++; + array[size++] = x; + } + + @Override + public T dequeue() throws CollectionException { + if (isEmpty()) { + throw new CollectionException(ERR_MSG_EMPTY); + } + T max = array[0]; + int maxIndex = 0; + for (int i = 0; i < size; i++) { + compares++; + if (array[i].compareTo(max) > 0) { + max = array[i]; + maxIndex = i; + } + } + + for (int i = maxIndex; i < size - 1; i++) { + array[i] = array[i + 1]; + moves++; + } + size--; + + return max; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public int size() { + return size; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("["); + if (size > 0) { + sb.append(array[0]); + for (int i = 1; i < size; i++) { + sb.append(", " + array[i]); + } + } + sb.append("]"); + return sb.toString(); + } +} + +@SuppressWarnings("ALL") +class ArrayHeapPQ implements PriorityQueue { + T[] array = (T[]) new Comparable[64]; + int size = 0; + int moves = 0; + int compares = 0; + + void resize() { + T[] newArray = (T[]) new Comparable[array.length * 2]; + System.arraycopy(array, 0, newArray, 0, array.length); + array = newArray; + } + + @Override + public T front() throws CollectionException { + if (isEmpty()) { + throw new CollectionException(ERR_MSG_EMPTY); + } + return array[0]; + } + + @Override + public void enqueue(T x) { + if (size == array.length) { + resize(); + } + int i = size; + array[i] = x; + size++; + + while (true) { + int p = (i - 1) / 2; + compares++; + if (array[i].compareTo(array[p]) <= 0) { + break; + } + T tmp = array[i]; + array[i] = array[p]; + array[p] = tmp; + i = p; + moves += 3; + } + } + + @Override + public T dequeue() throws CollectionException { + if (isEmpty()) { + throw new CollectionException(ERR_MSG_EMPTY); + } + T root = array[0]; + + array[0] = array[size - 1]; + array[size - 1] = root; + moves += 2; + size--; + + int i = 0; + while (true) { + int c = 2 * i + 1; + if (c >= size) break; + + int c2 = 2 * i + 2; + compares++; + if (c2 < size && array[c2].compareTo(array[c]) > 0) { + c = c2; + } + + compares++; + if (array[c].compareTo(array[i]) < 0) break; + + T tmp = array[i]; + array[i] = array[c]; + array[c] = tmp; + moves += 3; + i = c; + } + + return root; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public int size() { + return size; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("["); + if (size > 0) { + sb.append(array[0]); + for (int i = 1; i < size; i++) { + sb.append(", " + array[i]); + } + } + sb.append("]"); + return sb.toString(); + } +} + +class Node { + T value; + Node left; + Node right; + Node parent; + + Node(T value) { + this.value = value; + } +} + +@SuppressWarnings("ALL") +class LinkedHeapPQ implements PriorityQueue { + Node root = null; + int size = 0; + int moves = 0; + int compares = 0; + + Node getNode(int index) { + if (index == 0) { + return root; + } + Node parent = getNode((index - 1) / 2); + return index % 2 == 0 ? parent.right : parent.left; + } + + @Override + public T front() throws CollectionException { + if (isEmpty()) { + throw new CollectionException(ERR_MSG_EMPTY); + } + return root.value; + } + + @Override + public void enqueue(T x) { + Node newNode = new Node(x); + if (isEmpty()) { + root = newNode; + size++; + moves++; + return; + } + + int parentIndex = (size - 1) / 2; + Node parent = getNode(parentIndex); + newNode.parent = parent; + if (size % 2 == 0) { + parent.right = newNode; + } else { + parent.left = newNode; + } + size++; + + do { + compares++; + if (newNode.value.compareTo(newNode.parent.value) < 0) { + break; + } + + T tmp = newNode.value; + newNode.value = newNode.parent.value; + newNode.parent.value = tmp; + newNode = newNode.parent; + moves += 3; + } while (newNode.parent != null); + } + + @Override + public T dequeue() throws CollectionException { + T value = root.value; + + int lastIndex = size - 1; + Node last = getNode(lastIndex); + if (last == root) { + root = null; + size = 0; + moves++; + return value; + } + + root.value = last.value; + if (lastIndex % 2 == 0) { + last.parent.right = null; + } else { + last.parent.left = null; + } + size--; + + Node node = root; + while (true) { + if (node.left == null) break; + Node child = node.left; + compares++; + if (node.right != null) { + compares++; + if (node.left.value.compareTo(node.right.value) < 0) { + child = node.right; + } + } + + compares++; + if (node.value.compareTo(child.value) > 0) break; + + T tmp = child.value; + child.value = node.value; + node.value = tmp; + node = child; + moves += 3; + } + + return value; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public int size() { + return size; + } +} + +@SuppressWarnings("ALL") +public class Izziv4 { + public static void main(String[] args) throws CollectionException { + System.out.println("Objekti: Integer"); + System.out.println("Operacije: 10000 enqueue + 10000 (dequeue+enqueue+front)"); + System.out.println(); + System.out.println("Implementacija Čas [ms] Premikov Primerjav"); + System.out.println("------------------------------------------------------------"); + + long start = System.nanoTime(); + ArrayPQ arrayPQ = new ArrayPQ<>(); + for (int i = 0; i < 10000; i++) { + arrayPQ.enqueue((int) (Math.random() * 10000)); + } + for (int i = 0; i < 10000; i++) { + arrayPQ.dequeue(); + arrayPQ.enqueue((int) (Math.random() * 10000)); + arrayPQ.front(); + } + long end = System.nanoTime(); + + System.out.println("Neurejeno polje " + (end - start) / 1000000 + " " + arrayPQ.moves + " " + arrayPQ.compares); + + + start = System.nanoTime(); + ArrayHeapPQ arrayHeapPQ = new ArrayHeapPQ<>(); + for (int i = 0; i < 10000; i++) { + arrayHeapPQ.enqueue((int) (Math.random() * 10000)); + } + for (int i = 0; i < 10000; i++) { + arrayHeapPQ.dequeue(); + arrayHeapPQ.enqueue((int) (Math.random() * 10000)); + arrayHeapPQ.front(); + } + end = System.nanoTime(); + + System.out.println("Implicitna kopica " + (end - start) / 1000000 + " " + arrayHeapPQ.moves + " " + arrayHeapPQ.compares); + + + start = System.nanoTime(); + LinkedHeapPQ linkedHeapPQ = new LinkedHeapPQ<>(); + for (int i = 0; i < 10000; i++) { + linkedHeapPQ.enqueue((int) (Math.random() * 10000)); + } + for (int i = 0; i < 10000; i++) { + linkedHeapPQ.dequeue(); + linkedHeapPQ.enqueue((int) (Math.random() * 10000)); + linkedHeapPQ.front(); + } + end = System.nanoTime(); + + System.out.println("Eksplicitna kopica " + (end - start) / 1000000 + " " + linkedHeapPQ.moves + " " + linkedHeapPQ.compares); + } +} + diff --git a/izzivi/src/izziv4/rezultati.png b/izzivi/src/izziv4/rezultati.png new file mode 100644 index 0000000..5df40ec Binary files /dev/null and b/izzivi/src/izziv4/rezultati.png differ