simo 1 mese fa
parent
commit
3513ab585a

BIN
258/heapLab/HeapTest.class


+ 125 - 0
258/heapLab/HeapTest.java

@@ -0,0 +1,125 @@
+import java.util.Arrays;
+
+public class HeapTest {
+
+    public static void main(String[] args) {
+        // int length = 2;
+        // while (length < 10000000) {
+        //     System.out.print("Length " + length + " - ");
+        //     testHeap(length);
+        //     length *= 2;
+        // }
+        // O(n) - Every time length doubles, time complexity doubles with it
+        // Length 2 - Loop went 1 times
+        // Length 4 - Loop went 2 times
+        // Length 8 - Loop went 3 times
+        // Length 16 - Loop went 4 times
+        // Length 32 - Loop went 5 times
+        // Length 64 - Loop went 6 times
+        // Length 128 - Loop went 7 times
+        // Length 256 - Loop went 8 times
+        // Length 512 - Loop went 9 times
+        // Length 1024 - Loop went 10 times
+        // Length 2048 - Loop went 11 times
+        // Length 4096 - Loop went 12 times
+        // Length 8192 - Loop went 13 times
+        // Length 16384 - Loop went 14 times
+        // Length 32768 - Loop went 15 times
+        // Length 65536 - Loop went 16 times
+        // Length 131072 - Loop went 17 times
+        // Length 262144 - Loop went 18 times
+        // Length 524288 - Loop went 19 times
+        // Length 1048576 - Loop went 20 times
+        // Length 2097152 - Loop went 21 times
+        // Length 4194304 - Loop went 22 times
+        // Length 8388608 - Loop went 23 times
+        //
+        //
+        // BUBBLE SORT RESULTS
+        // sortTest(20);
+        // sortTest(50000);
+        // //100% usage of 1 CPU core for this btw
+        // sortTest(100000);
+        // sortTest(200000);
+        // Elapsed Time For Length 20: 0
+        // Elapsed Time For Length 50000: 3052
+        // Elapsed Time For Length 100000: 13250
+        // Elapsed Time For Length 200000: 49951
+        //
+        //
+        // HEAP SORT
+        sortTest(20);
+
+        sortTest(200000);
+
+        sortTest(2000000);
+
+        sortTest(20000000);
+
+        sortTest(40000000);
+
+        // Elapsed Time For Length 20: 0
+        // Elapsed Time For Length 200000: 21
+        // Elapsed Time For Length 2000000: 196
+        // Elapsed Time For Length 20000000: 7919
+        // Elapsed Time For Length 40000000: 24130
+        //
+        // Looks like heap sort is much faster than bubble sort in this instance
+    }
+
+    public static void testHeap(int length) {
+        IntMinHeap x = new IntMinHeap();
+
+        for (int random : generateRandom(length)) {
+            x.add(random);
+        }
+
+        x.addCountedLoops(-1);
+    }
+
+    public static void sortTest(int length) {
+        int[] array = generateRandom(length);
+
+        if (length < 25) System.out.println(Arrays.toString(array));
+
+        long timeOne = System.currentTimeMillis();
+
+        heapSort(array);
+
+        long timeTwo = System.currentTimeMillis();
+
+        if (length < 25) System.out.println(Arrays.toString(array));
+
+        System.out.println(
+            "Elapsed Time For Length " + length + ": " + (timeTwo - timeOne)
+        );
+    }
+
+    public static void heapSort(int[] array) {
+        IntMinHeap heap = new IntMinHeap(array.length);
+
+        for (int num : array) {
+            heap.add(num);
+        }
+
+        for (int i = 0; i < array.length; i++) {
+            array[i] = heap.remove();
+        }
+    }
+
+    public static int[] generateRandom(int length) {
+        int[] a = new int[length];
+        for (int i = 0; i < length; i++) a[i] = (int) (Math.random() *
+            length *
+            10);
+        return a;
+    }
+
+    public static void bubbleSort(int[] a) {
+        for (int i = 0; i < a.length - 1; i++) {
+            for (int j = 1; j < a.length - i; j++) {
+                if (a[j - 1] > a[j]) IntMinHeap.swap(a, j - 1, j);
+            }
+        }
+    }
+}

BIN
258/heapLab/IntMinHeap.class


+ 99 - 0
258/heapLab/IntMinHeap.java

@@ -0,0 +1,99 @@
+public class IntMinHeap {
+
+    private int[] heap;
+    private int size;
+
+    public IntMinHeap(int initialCapacity) {
+        heap = new int[initialCapacity];
+    }
+
+    public IntMinHeap() {
+        this(10);
+    }
+
+    public void add(int n) {
+        if (size == heap.length) expandHeap();
+        heap[size] = n;
+        int child = size++;
+        int parent = parent(child);
+        while (child > 0 && heap[parent] > heap[child]) {
+            swap(heap, child, parent);
+            child = parent;
+            parent = parent(child);
+        }
+    }
+
+    public void addCountedLoops(int n) {
+        if (size == heap.length) expandHeap();
+        heap[size] = n;
+        int child = size++;
+        int parent = parent(child);
+        int i = 0;
+        while (child > 0 && heap[parent] > heap[child]) {
+            swap(heap, child, parent);
+            child = parent;
+            parent = parent(child);
+            i++;
+        }
+
+        System.out.println("Loop went " + i + " times");
+    }
+
+    public int remove() {
+        int top = heap[0];
+        heap[0] = heap[--size];
+        int parent = 0;
+        int child = smallerChild(parent);
+        while (child != -1 && heap[parent] > heap[child]) {
+            swap(heap, parent, child);
+            parent = child;
+            child = smallerChild(parent);
+        }
+        return top;
+    }
+
+    private int smallerChild(int parent) {
+        int left = leftChild(parent);
+        int right = rightChild(parent);
+        if (left < size && right < size) return heap[left] < heap[right]
+            ? left
+            : right;
+        else if (left < size) return left;
+        else return -1;
+    }
+
+    public int size() {
+        return size;
+    }
+
+    @Override
+    public String toString() {
+        String s = "[";
+        for (int i = 0; i < size; i++) s += heap[i] + ", ";
+        return s.substring(0, s.length() - 2) + "]";
+    }
+
+    public static void swap(int[] a, int i, int j) {
+        int temp = a[i];
+        a[i] = a[j];
+        a[j] = temp;
+    }
+
+    private void expandHeap() {
+        int[] oldHeap = heap;
+        heap = new int[size * 2];
+        for (int i = 0; i < size; i++) heap[i] = oldHeap[i];
+    }
+
+    private int parent(int child) {
+        return (child - 1) / 2;
+    }
+
+    private int leftChild(int parent) {
+        return parent * 2 + 1;
+    }
+
+    private int rightChild(int parent) {
+        return parent * 2 + 2;
+    }
+}