Naloga 2 WIP

This commit is contained in:
Gašper Dobrovoljc 2024-12-26 22:45:01 +01:00
parent d0243b20ba
commit 713929db84
No known key found for this signature in database
GPG Key ID: 0E7E037018CFA5A5

View File

@ -32,6 +32,12 @@ public class Naloga2 {
case "quick": case "quick":
sorter = new QuickSorter(); sorter = new QuickSorter();
break; break;
case "radix":
sorter = new RadixSorter();
break;
case "bucket":
sorter = new BucketSorter();
break;
default: default:
System.out.println("Invalid sort type"); System.out.println("Invalid sort type");
return; return;
@ -103,6 +109,13 @@ class IntArray {
return result; return result;
} }
void fill(int count, int value) {
size = 0;
for (int i = 0; i < count; i++) {
append(value);
}
}
public String toStringSplit(int[] splits) { public String toStringSplit(int[] splits) {
return toStringSplit(splits, 0, size()); return toStringSplit(splits, 0, size());
} }
@ -431,4 +444,152 @@ class QuickSorter extends Sorter {
moves += 3; moves += 3;
return r; return r;
} }
}
class RadixSorter extends Sorter {
@Override
void sort(IntArray array, boolean up) {
super.sort(array, up);
int m = getMax(array);
for (int exp = 1; m / exp > 0; exp *= 10) {
countSort(array, exp, up);
trace.append(array).append("\n");
}
}
int getMax(IntArray array) {
int max = array.get(0);
for (int i = 1; i < array.size(); i++) {
if (array.get(i) > max) {
max = array.get(i);
}
}
return max;
}
void countSort(IntArray array, int exp, boolean up) {
IntArray output = new IntArray();
int i;
IntArray count = new IntArray();
count.fill(10, 0);
for (i = 0; i < array.size(); i++) {
int c = (array.get(i) / exp) % 10;
count.set(c, count.get(c) + 1);
moves++;
comparisons++;
}
if (up) {
for (i = 1; i < 10; i++) {
count.set(i, count.get(i) + count.get(i - 1));
}
} else {
for (i = 8; i >= 0; i--) {
count.set(i, count.get(i) + count.get(i + 1));
}
}
for (i = array.size() - 1; i >= 0; i--) {
int c = (array.get(i) / exp) % 10;
output.set(count.get(c) - 1, array.get(i));
count.set(c, count.get(c) - 1);
moves++;
comparisons++;
}
for (i = 0; i < array.size(); i++) {
array.set(i, output.get(i));
}
}
}
class BucketSorter extends Sorter {
@Override
void sort(IntArray array, boolean up) {
super.sort(array, up);
int min = getMin(array);
int max = getMax(array);
int k = array.size() / 2;
IntArray[] buckets = new IntArray[k];
for (int i = 0; i < buckets.length; i++) {
comparisons++;
buckets[i] = new IntArray();
}
for (int i = 0; i < array.size(); i++) {
int bi = (int) ((array.get(i) - min) / ((max - min + 1) / (double) k));
if (!up) {
bi = k - bi - 1;
}
moves++;
comparisons++;
buckets[bi].append(array.get(i));
}
trace.append(buckets[0]);
for (int i = 1; i < buckets.length; i++) {
trace.append(" | ").append(buckets[i]);
}
trace.append("\n");
int index = 0;
for (IntArray bucket : buckets) {
for (int j = 0; j < bucket.size(); j++) {
array.set(index++, bucket.get(j));
moves++;
}
}
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) {
comparisons++;
if (up ? array.get(j) <= key : array.get(j) >= key) {
break;
}
moves++;
array.set(j + 1, array.get(j));
j--;
}
moves++;
array.set(j + 1, key);
trace.append(array.toStringSplit(new int[]{i + 1})).append("\n");
}
}
} }