Naloga 2 WIP

This commit is contained in:
Gašper Dobrovoljc 2024-12-26 19:55:12 +01:00
parent bd363ae662
commit d0243b20ba
No known key found for this signature in database
GPG Key ID: 0E7E037018CFA5A5
8 changed files with 514 additions and 0 deletions

29
naloge/naloga2/.gitignore vendored Normal file
View File

@ -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

8
naloge/naloga2/.idea/.gitignore generated vendored Normal file
View File

@ -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

View File

@ -0,0 +1,12 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="PyUnresolvedReferencesInspection" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ignoredIdentifiers">
<list>
<option value="tests.smoke.test_absences_sliding.TestAbsencesSliding.*" />
</list>
</option>
</inspection_tool>
</profile>
</component>

6
naloge/naloga2/.idea/misc.xml generated Normal file
View File

@ -0,0 +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">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
naloge/naloga2/.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/naloga2.iml" filepath="$PROJECT_DIR$/naloga2.iml" />
</modules>
</component>
</project>

6
naloge/naloga2/.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/../.." vcs="Git" />
</component>
</project>

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -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;
}
}