Fixed trace
This commit is contained in:
		
							parent
							
								
									713929db84
								
							
						
					
					
						commit
						1f732920bd
					
				
							
								
								
									
										2
									
								
								naloge/naloga2/.idea/misc.xml
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										2
									
								
								naloge/naloga2/.idea/misc.xml
									
									
									
										generated
									
									
									
								
							@ -1,6 +1,6 @@
 | 
				
			|||||||
<?xml version="1.0" encoding="UTF-8"?>
 | 
					<?xml version="1.0" encoding="UTF-8"?>
 | 
				
			||||||
<project version="4">
 | 
					<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" />
 | 
					    <output url="file://$PROJECT_DIR$/out" />
 | 
				
			||||||
  </component>
 | 
					  </component>
 | 
				
			||||||
</project>
 | 
					</project>
 | 
				
			||||||
@ -3,11 +3,13 @@ import java.util.Scanner;
 | 
				
			|||||||
public class Naloga2 {
 | 
					public class Naloga2 {
 | 
				
			||||||
    public static void main(String[] args) {
 | 
					    public static void main(String[] args) {
 | 
				
			||||||
        Scanner scanner = new Scanner(System.in);
 | 
					        Scanner scanner = new Scanner(System.in);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        String[] settings = scanner.nextLine().split("\\s+");
 | 
					        String[] settings = scanner.nextLine().split("\\s+");
 | 
				
			||||||
        boolean up = settings[2].equals("up");
 | 
					        boolean up = settings[2].equals("up");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        IntArray array = new IntArray();
 | 
					        String[] data = scanner.nextLine().split("\\s+");
 | 
				
			||||||
        for (String s : scanner.nextLine().split("\\s+")) {
 | 
					        IntArray array = new IntArray(data.length);
 | 
				
			||||||
 | 
					        for (String s : data) {
 | 
				
			||||||
            array.append(Integer.parseInt(s));
 | 
					            array.append(Integer.parseInt(s));
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -39,8 +41,7 @@ public class Naloga2 {
 | 
				
			|||||||
                sorter = new BucketSorter();
 | 
					                sorter = new BucketSorter();
 | 
				
			||||||
                break;
 | 
					                break;
 | 
				
			||||||
            default:
 | 
					            default:
 | 
				
			||||||
                System.out.println("Invalid sort type");
 | 
					                throw new Error("Invalid sort type");
 | 
				
			||||||
                return;
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        switch (settings[0]) {
 | 
					        switch (settings[0]) {
 | 
				
			||||||
@ -50,6 +51,8 @@ public class Naloga2 {
 | 
				
			|||||||
            case "count":
 | 
					            case "count":
 | 
				
			||||||
                sorter.count(array, up);
 | 
					                sorter.count(array, up);
 | 
				
			||||||
                break;
 | 
					                break;
 | 
				
			||||||
 | 
					            default:
 | 
				
			||||||
 | 
					                throw new Error("Invalid output type");
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -62,13 +65,17 @@ class IntArray {
 | 
				
			|||||||
        array = new int[64];
 | 
					        array = new int[64];
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    IntArray(int size) {
 | 
				
			||||||
 | 
					        array = new int[size];
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private void resize() {
 | 
					    private void resize() {
 | 
				
			||||||
        int[] tmp = new int[array.length * 2];
 | 
					        int[] tmp = new int[array.length * 2];
 | 
				
			||||||
        System.arraycopy(array, 0, tmp, 0, array.length);
 | 
					        System.arraycopy(array, 0, tmp, 0, array.length);
 | 
				
			||||||
        array = tmp;
 | 
					        array = tmp;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    boolean isFull() {
 | 
					    private boolean isFull() {
 | 
				
			||||||
        return size == array.length;
 | 
					        return size == array.length;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -161,32 +168,37 @@ class IntArray {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
abstract class Sorter {
 | 
					abstract class Sorter {
 | 
				
			||||||
    int moves = 0;
 | 
					    int moves = 0;
 | 
				
			||||||
    int comparisons = 0;
 | 
					    int comparisons = 0;
 | 
				
			||||||
    StringBuilder trace;
 | 
					    boolean trace = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    void sort(IntArray array, boolean up) {
 | 
					    void sort(IntArray array, boolean up) {
 | 
				
			||||||
        moves = 0;
 | 
					        moves = 0;
 | 
				
			||||||
        comparisons = 0;
 | 
					        comparisons = 0;
 | 
				
			||||||
        trace = new StringBuilder();
 | 
					        printTrace(array + "\n");
 | 
				
			||||||
        trace.append(array.toString()).append("\n");
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    void trace(IntArray array, boolean up) {
 | 
					    void trace(IntArray array, boolean up) {
 | 
				
			||||||
 | 
					        trace = true;
 | 
				
			||||||
        sort(array, up);
 | 
					        sort(array, up);
 | 
				
			||||||
        System.out.print(trace.toString());
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    void count(IntArray array, boolean up) {
 | 
					    void count(IntArray array, boolean up) {
 | 
				
			||||||
        StringBuilder sb = new StringBuilder();
 | 
					        trace = false;
 | 
				
			||||||
        sort(array, up);
 | 
					        sort(array, up);
 | 
				
			||||||
        sb.append(moves).append(" ").append(comparisons).append(" | ");
 | 
					        System.out.print(moves + " " + comparisons);
 | 
				
			||||||
        sort(array, up);
 | 
					        sort(array, up);
 | 
				
			||||||
        sb.append(moves).append(" ").append(comparisons).append(" | ");
 | 
					        System.out.print(" | " + moves + " " + comparisons);
 | 
				
			||||||
        sort(array, !up);
 | 
					        sort(array, !up);
 | 
				
			||||||
        sb.append(moves).append(" ").append(comparisons);
 | 
					        System.out.println(" | " + moves + " " + comparisons);
 | 
				
			||||||
        System.out.println(sb);
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    void printTrace(Object value) {
 | 
				
			||||||
 | 
					        if (trace) {
 | 
				
			||||||
 | 
					            System.out.print(value);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -211,14 +223,7 @@ class InsertionSorter extends Sorter {
 | 
				
			|||||||
            moves++;
 | 
					            moves++;
 | 
				
			||||||
            array.set(j, tmp);
 | 
					            array.set(j, tmp);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            trace.append(array.get(0));
 | 
					            printTrace(array.toStringSplit(new int[]{i + 1}) + "\n");
 | 
				
			||||||
            for (int l = 1; l < array.size(); l++) {
 | 
					 | 
				
			||||||
                trace.append(" ").append(array.get(l));
 | 
					 | 
				
			||||||
                if (l == i) {
 | 
					 | 
				
			||||||
                    trace.append(" |");
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            trace.append("\n");
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -239,14 +244,14 @@ class SelectionSorter extends Sorter {
 | 
				
			|||||||
            moves += 3;
 | 
					            moves += 3;
 | 
				
			||||||
            array.swap(i, m);
 | 
					            array.swap(i, m);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            trace.append(array.get(0));
 | 
					            printTrace(array.get(0));
 | 
				
			||||||
            for (int l = 1; l < array.size(); l++) {
 | 
					            for (int l = 1; l < array.size(); l++) {
 | 
				
			||||||
                if (l - 1 == i) {
 | 
					                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) {
 | 
					            if (i == 0) {
 | 
				
			||||||
                continue;
 | 
					                continue;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            trace.append(array.get(0));
 | 
					            printTrace(array.get(0));
 | 
				
			||||||
            for (int l = 1; l < array.size(); l++) {
 | 
					            for (int l = 1; l < array.size(); l++) {
 | 
				
			||||||
                if (l == i) {
 | 
					                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);
 | 
					            siftDown(array, array.size(), i, up);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        trace.append(array).append(" |\n");
 | 
					        printTrace(array + " |\n");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        for (int i = array.size() - 1; i > 0; i--) {
 | 
					        for (int i = array.size() - 1; i > 0; i--) {
 | 
				
			||||||
            array.swap(0, i);
 | 
					            array.swap(0, i);
 | 
				
			||||||
            moves += 3;
 | 
					            moves += 3;
 | 
				
			||||||
            siftDown(array, i, 0, up);
 | 
					            siftDown(array, i, 0, up);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            trace.append(array.get(0));
 | 
					            printTrace(array.get(0));
 | 
				
			||||||
            for (int l = 1; l < array.size(); l++) {
 | 
					            for (int l = 1; l < array.size(); l++) {
 | 
				
			||||||
                if (l == i) {
 | 
					                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();
 | 
					        moves += left.size();
 | 
				
			||||||
        IntArray right = array.slice(middle, array.size());
 | 
					        IntArray right = array.slice(middle, array.size());
 | 
				
			||||||
        moves += right.size();
 | 
					        moves += right.size();
 | 
				
			||||||
        trace.append(left).append(" | ").append(right).append("\n");
 | 
					        printTrace(left + " | " + right + "\n");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        left = mergeSort(left, up);
 | 
					        left = mergeSort(left, up);
 | 
				
			||||||
        right = mergeSort(right, up);
 | 
					        right = mergeSort(right, up);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        IntArray merged = merge(left, right, up);
 | 
					        IntArray merged = merge(left, right, up);
 | 
				
			||||||
        trace.append(merged).append("\n");
 | 
					        printTrace(merged + "\n");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return merged;
 | 
					        return merged;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -405,7 +410,7 @@ class QuickSorter extends Sorter {
 | 
				
			|||||||
    void sort(IntArray array, boolean up) {
 | 
					    void sort(IntArray array, boolean up) {
 | 
				
			||||||
        super.sort(array, up);
 | 
					        super.sort(array, up);
 | 
				
			||||||
        quickSort(array, 0, array.size() - 1, 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) {
 | 
					    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);
 | 
					        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, left, r - 1, up);
 | 
				
			||||||
        quickSort(array, r + 1, right, up);
 | 
					        quickSort(array, r + 1, right, up);
 | 
				
			||||||
@ -455,7 +460,7 @@ class RadixSorter extends Sorter {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        for (int exp = 1; m / exp > 0; exp *= 10) {
 | 
					        for (int exp = 1; m / exp > 0; exp *= 10) {
 | 
				
			||||||
            countSort(array, exp, up);
 | 
					            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) {
 | 
					    void countSort(IntArray array, int exp, boolean up) {
 | 
				
			||||||
        IntArray output = new IntArray();
 | 
					        IntArray output = new IntArray(array.size());
 | 
				
			||||||
        int i;
 | 
					        int i;
 | 
				
			||||||
        IntArray count = new IntArray();
 | 
					        IntArray count = new IntArray();
 | 
				
			||||||
        count.fill(10, 0);
 | 
					        count.fill(10, 0);
 | 
				
			||||||
@ -531,11 +536,11 @@ class BucketSorter extends Sorter {
 | 
				
			|||||||
            buckets[bi].append(array.get(i));
 | 
					            buckets[bi].append(array.get(i));
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        trace.append(buckets[0]);
 | 
					        printTrace(buckets[0]);
 | 
				
			||||||
        for (int i = 1; i < buckets.length; i++) {
 | 
					        for (int i = 1; i < buckets.length; i++) {
 | 
				
			||||||
            trace.append(" | ").append(buckets[i]);
 | 
					            printTrace(" | " + buckets[i]);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        trace.append("\n");
 | 
					        printTrace("\n");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        int index = 0;
 | 
					        int index = 0;
 | 
				
			||||||
        for (IntArray bucket : buckets) {
 | 
					        for (IntArray bucket : buckets) {
 | 
				
			||||||
@ -589,7 +594,7 @@ class BucketSorter extends Sorter {
 | 
				
			|||||||
            moves++;
 | 
					            moves++;
 | 
				
			||||||
            array.set(j + 1, key);
 | 
					            array.set(j + 1, key);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            trace.append(array.toStringSplit(new int[]{i + 1})).append("\n");
 | 
					            printTrace(array.toStringSplit(new int[]{i + 1}) + "\n");
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user