Fixed trace

This commit is contained in:
Gašper Dobrovoljc 2024-12-28 13:22:08 +01:00
parent 713929db84
commit 1f732920bd
No known key found for this signature in database
GPG Key ID: 0E7E037018CFA5A5
2 changed files with 51 additions and 46 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_23" default="true" project-jdk-name="homebrew-23" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="homebrew-11" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -3,11 +3,13 @@ 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+")) {
String[] data = scanner.nextLine().split("\\s+");
IntArray array = new IntArray(data.length);
for (String s : data) {
array.append(Integer.parseInt(s));
}
@ -39,8 +41,7 @@ public class Naloga2 {
sorter = new BucketSorter();
break;
default:
System.out.println("Invalid sort type");
return;
throw new Error("Invalid sort type");
}
switch (settings[0]) {
@ -50,6 +51,8 @@ public class Naloga2 {
case "count":
sorter.count(array, up);
break;
default:
throw new Error("Invalid output type");
}
}
}
@ -62,13 +65,17 @@ class IntArray {
array = new int[64];
}
IntArray(int size) {
array = new int[size];
}
private void resize() {
int[] tmp = new int[array.length * 2];
System.arraycopy(array, 0, tmp, 0, array.length);
array = tmp;
}
boolean isFull() {
private boolean isFull() {
return size == array.length;
}
@ -161,32 +168,37 @@ class IntArray {
}
}
abstract class Sorter {
int moves = 0;
int comparisons = 0;
StringBuilder trace;
boolean trace = false;
void sort(IntArray array, boolean up) {
moves = 0;
comparisons = 0;
trace = new StringBuilder();
trace.append(array.toString()).append("\n");
printTrace(array + "\n");
}
void trace(IntArray array, boolean up) {
trace = true;
sort(array, up);
System.out.print(trace.toString());
}
void count(IntArray array, boolean up) {
StringBuilder sb = new StringBuilder();
trace = false;
sort(array, up);
sb.append(moves).append(" ").append(comparisons).append(" | ");
System.out.print(moves + " " + comparisons);
sort(array, up);
sb.append(moves).append(" ").append(comparisons).append(" | ");
System.out.print(" | " + moves + " " + comparisons);
sort(array, !up);
sb.append(moves).append(" ").append(comparisons);
System.out.println(sb);
System.out.println(" | " + moves + " " + comparisons);
}
void printTrace(Object value) {
if (trace) {
System.out.print(value);
}
}
}
@ -211,14 +223,7 @@ class InsertionSorter extends Sorter {
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");
printTrace(array.toStringSplit(new int[]{i + 1}) + "\n");
}
}
}
@ -239,14 +244,14 @@ class SelectionSorter extends Sorter {
moves += 3;
array.swap(i, m);
trace.append(array.get(0));
printTrace(array.get(0));
for (int l = 1; l < array.size(); l++) {
if (l - 1 == i) {
trace.append(" |");
printTrace(" |");
}
trace.append(" ").append(array.get(l));
printTrace(" " + array.get(l));
}
trace.append("\n");
printTrace("\n");
}
}
}
@ -272,14 +277,14 @@ class BubbleSorter extends Sorter {
if (i == 0) {
continue;
}
trace.append(array.get(0));
printTrace(array.get(0));
for (int l = 1; l < array.size(); l++) {
if (l == i) {
trace.append(" |");
printTrace(" |");
}
trace.append(" ").append(array.get(l));
printTrace(" " + array.get(l));
}
trace.append("\n");
printTrace("\n");
}
}
}
@ -293,21 +298,21 @@ class HeapSorter extends Sorter {
siftDown(array, array.size(), i, up);
}
trace.append(array).append(" |\n");
printTrace(array + " |\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));
printTrace(array.get(0));
for (int l = 1; l < array.size(); l++) {
if (l == i) {
trace.append(" |");
printTrace(" |");
}
trace.append(" ").append(array.get(l));
printTrace(" " + array.get(l));
}
trace.append("\n");
printTrace("\n");
}
}
@ -355,13 +360,13 @@ class MergeSorter extends Sorter {
moves += left.size();
IntArray right = array.slice(middle, array.size());
moves += right.size();
trace.append(left).append(" | ").append(right).append("\n");
printTrace(left + " | " + right + "\n");
left = mergeSort(left, up);
right = mergeSort(right, up);
IntArray merged = merge(left, right, up);
trace.append(merged).append("\n");
printTrace(merged + "\n");
return merged;
}
@ -405,7 +410,7 @@ class QuickSorter extends Sorter {
void sort(IntArray array, boolean up) {
super.sort(array, up);
quickSort(array, 0, array.size() - 1, up);
trace.append(array).append("\n");
printTrace(array + "\n");
}
void quickSort(IntArray array, int left, int right, boolean up) {
@ -414,7 +419,7 @@ class QuickSorter extends Sorter {
}
int r = partition(array, left, right, up);
trace.append(array.toStringSplit(new int[]{r, r + 1}, left, right + 1)).append("\n");
printTrace(array.toStringSplit(new int[]{r, r + 1}, left, right + 1) + "\n");
quickSort(array, left, r - 1, up);
quickSort(array, r + 1, right, up);
@ -455,7 +460,7 @@ class RadixSorter extends Sorter {
for (int exp = 1; m / exp > 0; exp *= 10) {
countSort(array, exp, up);
trace.append(array).append("\n");
printTrace(array + "\n");
}
}
@ -470,7 +475,7 @@ class RadixSorter extends Sorter {
}
void countSort(IntArray array, int exp, boolean up) {
IntArray output = new IntArray();
IntArray output = new IntArray(array.size());
int i;
IntArray count = new IntArray();
count.fill(10, 0);
@ -531,11 +536,11 @@ class BucketSorter extends Sorter {
buckets[bi].append(array.get(i));
}
trace.append(buckets[0]);
printTrace(buckets[0]);
for (int i = 1; i < buckets.length; i++) {
trace.append(" | ").append(buckets[i]);
printTrace(" | " + buckets[i]);
}
trace.append("\n");
printTrace("\n");
int index = 0;
for (IntArray bucket : buckets) {
@ -589,7 +594,7 @@ class BucketSorter extends Sorter {
moves++;
array.set(j + 1, key);
trace.append(array.toStringSplit(new int[]{i + 1})).append("\n");
printTrace(array.toStringSplit(new int[]{i + 1}) + "\n");
}
}
}