From d0243b20ba751b88c787439f8d818c43a41b3406 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C5=A1per=20Dobrovoljc?= Date: Thu, 26 Dec 2024 19:55:12 +0100 Subject: [PATCH] Naloga 2 WIP --- naloge/naloga2/.gitignore | 29 ++ naloge/naloga2/.idea/.gitignore | 8 + .../inspectionProfiles/Project_Default.xml | 12 + naloge/naloga2/.idea/misc.xml | 6 + naloge/naloga2/.idea/modules.xml | 8 + naloge/naloga2/.idea/vcs.xml | 6 + naloge/naloga2/naloga2.iml | 11 + naloge/naloga2/src/Naloga2.java | 434 ++++++++++++++++++ 8 files changed, 514 insertions(+) create mode 100644 naloge/naloga2/.gitignore create mode 100644 naloge/naloga2/.idea/.gitignore create mode 100644 naloge/naloga2/.idea/inspectionProfiles/Project_Default.xml create mode 100644 naloge/naloga2/.idea/misc.xml create mode 100644 naloge/naloga2/.idea/modules.xml create mode 100644 naloge/naloga2/.idea/vcs.xml create mode 100644 naloge/naloga2/naloga2.iml create mode 100644 naloge/naloga2/src/Naloga2.java diff --git a/naloge/naloga2/.gitignore b/naloge/naloga2/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/naloge/naloga2/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/naloge/naloga2/.idea/.gitignore b/naloge/naloga2/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/naloge/naloga2/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/naloge/naloga2/.idea/inspectionProfiles/Project_Default.xml b/naloge/naloga2/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..581c216 --- /dev/null +++ b/naloge/naloga2/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,12 @@ + + + + \ No newline at end of file diff --git a/naloge/naloga2/.idea/misc.xml b/naloge/naloga2/.idea/misc.xml new file mode 100644 index 0000000..eeb80f7 --- /dev/null +++ b/naloge/naloga2/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/naloge/naloga2/.idea/modules.xml b/naloge/naloga2/.idea/modules.xml new file mode 100644 index 0000000..46d59f0 --- /dev/null +++ b/naloge/naloga2/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/naloge/naloga2/.idea/vcs.xml b/naloge/naloga2/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/naloge/naloga2/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/naloge/naloga2/naloga2.iml b/naloge/naloga2/naloga2.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/naloge/naloga2/naloga2.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/naloge/naloga2/src/Naloga2.java b/naloge/naloga2/src/Naloga2.java new file mode 100644 index 0000000..168d7fd --- /dev/null +++ b/naloge/naloga2/src/Naloga2.java @@ -0,0 +1,434 @@ +import java.util.Scanner; + +public class Naloga2 { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + String[] settings = scanner.nextLine().split("\\s+"); + boolean up = settings[2].equals("up"); + + IntArray array = new IntArray(); + for (String s : scanner.nextLine().split("\\s+")) { + array.append(Integer.parseInt(s)); + } + + Sorter sorter; + + switch (settings[1]) { + case "insert": + sorter = new InsertionSorter(); + break; + case "select": + sorter = new SelectionSorter(); + break; + case "bubble": + sorter = new BubbleSorter(); + break; + case "heap": + sorter = new HeapSorter(); + break; + case "merge": + sorter = new MergeSorter(); + break; + case "quick": + sorter = new QuickSorter(); + break; + default: + System.out.println("Invalid sort type"); + return; + } + + switch (settings[0]) { + case "trace": + sorter.trace(array, up); + break; + case "count": + sorter.count(array, up); + break; + } + } +} + +class IntArray { + private int[] array; + private int size = 0; + + IntArray() { + array = new int[64]; + } + + private void resize() { + int[] tmp = new int[array.length * 2]; + System.arraycopy(array, 0, tmp, 0, array.length); + array = tmp; + } + + boolean isFull() { + return size == array.length; + } + + boolean isEmpty() { + return size == 0; + } + + int size() { + return size; + } + + void append(int element) { + if (isFull()) { + resize(); + } + array[size++] = element; + } + + int get(int index) { + return array[index]; + } + + void set(int index, int element) { + array[index] = element; + } + + void swap(int index1, int index2) { + int temp = array[index1]; + array[index1] = array[index2]; + array[index2] = temp; + } + + IntArray slice(int start, int end) { + IntArray result = new IntArray(); + for (int i = start; i < end; i++) { + result.append(array[i]); + } + return result; + } + + public String toStringSplit(int[] splits) { + return toStringSplit(splits, 0, size()); + } + + public String toStringSplit(int[] splits, int start, int end) { + if (isEmpty()) { + return ""; + } + StringBuilder sb = new StringBuilder(); + for (int split : splits) { + if (split == start) { + sb.append("| "); + } + } + sb.append(array[start]); + for (int i = start + 1; i < end; i++) { + for (int split : splits) { + if (i == split) { + sb.append(" |"); + } + } + sb.append(" ").append(array[i]); + } + for (int split : splits) { + if (end == split) { + sb.append(" |"); + } + } + return sb.toString(); + } + + @Override + public String toString() { + if (isEmpty()) { + return ""; + } + StringBuilder sb = new StringBuilder(); + sb.append(array[0]); + for (int i = 1; i < size; i++) { + sb.append(" ").append(array[i]); + } + return sb.toString(); + } +} + +abstract class Sorter { + int moves = 0; + int comparisons = 0; + StringBuilder trace; + + void sort(IntArray array, boolean up) { + moves = 0; + comparisons = 0; + trace = new StringBuilder(); + trace.append(array.toString()).append("\n"); + } + + void trace(IntArray array, boolean up) { + sort(array, up); + System.out.print(trace.toString()); + } + + void count(IntArray array, boolean up) { + StringBuilder sb = new StringBuilder(); + sort(array, up); + sb.append(moves).append(" ").append(comparisons).append(" | "); + sort(array, up); + sb.append(moves).append(" ").append(comparisons).append(" | "); + sort(array, !up); + sb.append(moves).append(" ").append(comparisons); + System.out.println(sb); + } +} + +class InsertionSorter extends Sorter { + @Override + void sort(IntArray array, boolean up) { + super.sort(array, up); + + for (int i = 1; i < array.size(); i++) { + moves++; + int tmp = array.get(i); + int j = i; + while (j > 0) { + comparisons++; + if (!(up ? array.get(j - 1) > tmp : array.get(j - 1) < tmp)) { + break; + } + moves++; + array.set(j, array.get(j - 1)); + j--; + } + moves++; + array.set(j, tmp); + + trace.append(array.get(0)); + for (int l = 1; l < array.size(); l++) { + trace.append(" ").append(array.get(l)); + if (l == i) { + trace.append(" |"); + } + } + trace.append("\n"); + } + } +} + +class SelectionSorter extends Sorter { + @Override + void sort(IntArray array, boolean up) { + super.sort(array, up); + + for (int i = 0; i < array.size() - 1; i++) { + int m = i; + for (int j = i + 1; j < array.size(); j++) { + comparisons++; + if (up ? array.get(j) < array.get(m) : array.get(j) > array.get(m)) { + m = j; + } + } + moves += 3; + array.swap(i, m); + + trace.append(array.get(0)); + for (int l = 1; l < array.size(); l++) { + if (l - 1 == i) { + trace.append(" |"); + } + trace.append(" ").append(array.get(l)); + } + trace.append("\n"); + } + } +} + +class BubbleSorter extends Sorter { + @Override + void sort(IntArray array, boolean up) { + super.sort(array, up); + + for (int i = 1; i < array.size(); i++) { + int swap = array.size() - 1; + for (int j = array.size() - 1; j >= i; j--) { + comparisons++; + if (up ? array.get(j - 1) > array.get(j) : array.get(j - 1) < array.get(j)) { + array.swap(j - 1, j); + swap = j; + moves += 3; + } + } + + i = swap; + + if (i == 0) { + continue; + } + trace.append(array.get(0)); + for (int l = 1; l < array.size(); l++) { + if (l == i) { + trace.append(" |"); + } + trace.append(" ").append(array.get(l)); + } + trace.append("\n"); + } + } +} + +class HeapSorter extends Sorter { + @Override + void sort(IntArray array, boolean up) { + super.sort(array, up); + + for (int i = array.size() / 2 - 1; i >= 0; i--) { + siftDown(array, array.size(), i, up); + } + + trace.append(array).append(" |\n"); + + for (int i = array.size() - 1; i > 0; i--) { + array.swap(0, i); + moves += 3; + siftDown(array, i, 0, up); + + trace.append(array.get(0)); + for (int l = 1; l < array.size(); l++) { + if (l == i) { + trace.append(" |"); + } + trace.append(" ").append(array.get(l)); + } + trace.append("\n"); + } + } + + void siftDown(IntArray array, int n, int i, boolean up) { + int extr = i; + int left = 2 * i + 1; + int right = 2 * i + 2; + + if (left < n) { + comparisons++; + if (up ? array.get(left) > array.get(extr) : array.get(left) < array.get(extr)) { + extr = left; + } + } + + if (right < n) { + comparisons++; + if (up ? array.get(right) > array.get(extr) : array.get(right) < array.get(extr)) { + extr = right; + } + } + + if (extr != i) { + array.swap(extr, i); + moves += 3; + siftDown(array, n, extr, up); + } + } +} + +class MergeSorter extends Sorter { + @Override + void sort(IntArray array, boolean up) { + super.sort(array, up); + mergeSort(array, up); + } + + IntArray mergeSort(IntArray array, boolean up) { + if (array.size() <= 1) { + return array; + } + int middle = (array.size() - 1) / 2 + 1; + + IntArray left = array.slice(0, middle); + moves += left.size(); + IntArray right = array.slice(middle, array.size()); + moves += right.size(); + trace.append(left).append(" | ").append(right).append("\n"); + + left = mergeSort(left, up); + right = mergeSort(right, up); + + IntArray merged = merge(left, right, up); + trace.append(merged).append("\n"); + + return merged; + } + + IntArray merge(IntArray left, IntArray right, boolean up) { + IntArray result = new IntArray(); + + int i = 0; + int j = 0; + + while (i < left.size() && j < right.size()) { + comparisons++; + if (up ? left.get(i) <= right.get(j) : left.get(i) >= right.get(j)) { + result.append(left.get(i)); + i++; + } else { + result.append(right.get(j)); + j++; + } + moves++; + } + + while (i < left.size()) { + result.append(left.get(i)); + moves++; + i++; + } + + while (j < right.size()) { + result.append(right.get(j)); + moves++; + j++; + } + + return result; + } +} + +class QuickSorter extends Sorter { + @Override + void sort(IntArray array, boolean up) { + super.sort(array, up); + quickSort(array, 0, array.size() - 1, up); + trace.append(array).append("\n"); + } + + void quickSort(IntArray array, int left, int right, boolean up) { + if (left >= right) { + return; + } + int r = partition(array, left, right, up); + + trace.append(array.toStringSplit(new int[]{r, r + 1}, left, right + 1)).append("\n"); + + quickSort(array, left, r - 1, up); + quickSort(array, r + 1, right, up); + } + + int partition(IntArray array, int left, int right, boolean up) { + int pivot = array.get(left); + moves++; + int l = left; + int r = right + 1; + while (true) { + do { + l++; + comparisons++; + } while ((up ? array.get(l) < pivot : array.get(l) > pivot) && l < right); + do { + r--; + comparisons++; + } while (up ? array.get(r) > pivot : array.get(r) < pivot); + if (l >= r) { + break; + } + array.swap(l, r); + moves += 3; + } + array.swap(left, r); + moves += 3; + return r; + } +} \ No newline at end of file