From 8dd164785ed9fdf3920d75c627ad7261e12ddc85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C5=A1per=20Dobrovoljc?= Date: Sat, 28 Dec 2024 15:42:45 +0100 Subject: [PATCH] Naloga 2 --- naloge/naloga2/src/Naloga2.java | 105 +++++++++++++------------------- 1 file changed, 41 insertions(+), 64 deletions(-) diff --git a/naloge/naloga2/src/Naloga2.java b/naloge/naloga2/src/Naloga2.java index b6e5244..d211aff 100644 --- a/naloge/naloga2/src/Naloga2.java +++ b/naloge/naloga2/src/Naloga2.java @@ -123,7 +123,14 @@ class IntArray { } } - public String toStringSplit(int[] splits) { + void copyFrom(IntArray src) { + array = new int[src.array.length]; + System.arraycopy(src.array, 0, array, 0, src.size()); + size = src.size(); + } + + + public String toStringSplit(int... splits) { return toStringSplit(splits, 0, size()); } @@ -223,7 +230,7 @@ class InsertionSorter extends Sorter { moves++; array.set(j, tmp); - printTrace(array.toStringSplit(new int[]{i + 1}) + "\n"); + printTrace(array.toStringSplit(i + 1) + "\n"); } } } @@ -244,14 +251,7 @@ class SelectionSorter extends Sorter { moves += 3; array.swap(i, m); - printTrace(array.get(0)); - for (int l = 1; l < array.size(); l++) { - if (l - 1 == i) { - printTrace(" |"); - } - printTrace(" " + array.get(l)); - } - printTrace("\n"); + printTrace(array.toStringSplit(i + 1) + "\n"); } } } @@ -277,14 +277,8 @@ class BubbleSorter extends Sorter { if (i == 0) { continue; } - printTrace(array.get(0)); - for (int l = 1; l < array.size(); l++) { - if (l == i) { - printTrace(" |"); - } - printTrace(" " + array.get(l)); - } - printTrace("\n"); + + printTrace(array.toStringSplit(i) + "\n"); } } } @@ -305,14 +299,7 @@ class HeapSorter extends Sorter { moves += 3; siftDown(array, i, 0, up); - printTrace(array.get(0)); - for (int l = 1; l < array.size(); l++) { - if (l == i) { - printTrace(" |"); - } - printTrace(" " + array.get(l)); - } - printTrace("\n"); + printTrace(array.toStringSplit(i) + "\n"); } } @@ -347,15 +334,15 @@ class MergeSorter extends Sorter { @Override void sort(IntArray array, boolean up) { super.sort(array, up); - mergeSort(array, up); + array.copyFrom(mergeSort(array, up)); } IntArray mergeSort(IntArray array, boolean up) { if (array.size() <= 1) { return array; } - int middle = (array.size() - 1) / 2 + 1; + int middle = (array.size() - 1) / 2 + 1; IntArray left = array.slice(0, middle); moves += left.size(); IntArray right = array.slice(middle, array.size()); @@ -515,14 +502,25 @@ class BucketSorter extends Sorter { @Override void sort(IntArray array, boolean up) { super.sort(array, up); - int min = getMin(array); - int max = getMax(array); + + int min = array.get(0); + int max = array.get(0); + for (int i = 1; i < array.size(); i++) { + comparisons += 2; + if (array.get(i) < min) { + min = array.get(i); + comparisons--; + continue; + } + if (array.get(i) > max) { + max = array.get(i); + } + } int k = array.size() / 2; IntArray[] buckets = new IntArray[k]; for (int i = 0; i < buckets.length; i++) { - comparisons++; buckets[i] = new IntArray(); } @@ -532,13 +530,16 @@ class BucketSorter extends Sorter { bi = k - bi - 1; } moves++; - comparisons++; + comparisons += 2; buckets[bi].append(array.get(i)); } printTrace(buckets[0]); for (int i = 1; i < buckets.length; i++) { - printTrace(" | " + buckets[i]); + printTrace(" |"); + if (!buckets[i].isEmpty()) { + printTrace(" " + buckets[i]); + } } printTrace("\n"); @@ -553,48 +554,24 @@ class BucketSorter extends Sorter { insertionSort(array, up); } - int getMin(IntArray array) { - int min = array.get(0); - comparisons++; - for (int i = 1; i < array.size(); i++) { - comparisons++; - if (array.get(i) < min) { - min = array.get(i); - } - } - return min; - } - - int getMax(IntArray array) { - int max = array.get(0); - comparisons++; - for (int i = 1; i < array.size(); i++) { - comparisons++; - if (array.get(i) > max) { - max = array.get(i); - } - } - return max; - } - void insertionSort(IntArray array, boolean up) { for (int i = 1; i < array.size(); i++) { - int key = array.get(i); moves++; - int j = i - 1; - while (j >= 0) { + int tmp = array.get(i); + int j = i; + while (j > 0) { comparisons++; - if (up ? array.get(j) <= key : array.get(j) >= key) { + if (!(up ? array.get(j - 1) > tmp : array.get(j - 1) < tmp)) { break; } moves++; - array.set(j + 1, array.get(j)); + array.set(j, array.get(j - 1)); j--; } moves++; - array.set(j + 1, key); + array.set(j, tmp); - printTrace(array.toStringSplit(new int[]{i + 1}) + "\n"); + printTrace(array.toStringSplit(i + 1) + "\n"); } } } \ No newline at end of file