This commit is contained in:
Gašper Dobrovoljc 2024-12-28 15:42:45 +01:00
parent 1f732920bd
commit 8dd164785e
No known key found for this signature in database
GPG Key ID: 0E7E037018CFA5A5

View File

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