1
0
forked from pabulaner/vivi

Bubblesort overworked und MergeSort

This commit is contained in:
happymeal2024 2025-04-28 13:43:27 +02:00
parent e28f39412b
commit 7704486f5b
3 changed files with 72 additions and 26 deletions

View File

@ -1,28 +1,19 @@
package de.vivi.list; package de.vivi.list;
import java.util.Arrays;
public class BubbleSort implements ByteSort { public class BubbleSort implements ByteSort {
private byte bubbleTemp;
private int a;
@Override @Override
public ByteList sort(ByteList list) { public ByteList sort(ByteList list) {
int n = list.size();
while (a < list.size() * list.size()) { for (int i = 0; i < n - 1; i++)
for (int i = 0; i < list.size() - 1; i++) { for (int j = 0; j < n - i - 1; j++)
if (list.get(i + 1) < list.get(i)) { if (list.get(j) > list.get(j + 1)) {
bubbleTemp = list.get(i + 1); byte temp = list.get(j);
list.set(i + 1, list.get(i)); list.set(j, list.get(j + 1));
list.set(i, bubbleTemp); list.set(j + 1, temp);
} }
a++;
}
}
System.out.println(a + " Anzahl runden");
return list; return list;
} }
} }

View File

@ -10,10 +10,10 @@ public class Main {
public static void main(String[] args) { public static void main(String[] args) {
ByteList array = new ArrayByteList(); ByteList array = new ArrayByteList();
ByteList linked = new LinkedByteList(); ByteList linked = new LinkedByteList();
// ByteList combined = new CombinedByteList(16); // ByteList combined = new CombinedByteList(16); // Paul meinte das kann weg gelassen werden
// testListVivi(array); testListVivi(array);
testListVivi(linked); //testListVivi(linked);
// testSortVivi(array); testSortVivi(array);
//testList(array); //testList(array);
// testList(linked); // testList(linked);
// testList(combined); // testList(combined);
@ -31,9 +31,9 @@ public class Main {
list.add((byte) 5); list.add((byte) 5);
list.add((byte) -100); list.add((byte) -100);
//
System.out.println(list); // System.out.println(list);
System.out.println(list.copy()); // System.out.println(list.copy());
// list.remove(4); // list.remove(4);
// list.contains(5); // list.contains(5);
// System.out.println(list.contains(2)); // System.out.println(list.contains(2));
@ -156,12 +156,14 @@ public class Main {
private static void testSortVivi(ByteList list) { private static void testSortVivi(ByteList list) {
ByteSort bubble = new BubbleSort(); ByteSort bubble = new BubbleSort();
// ByteSort merge = new MergeSort(); ByteSort merge = new MergeSort();
// ByteSort bogo = new BogoSort(); // ByteSort bogo = new BogoSort();
// sort the lists with the sorting algorithm // sort the lists with the sorting algorithm
// bubble.sort(list.copy()); // bubble.sort(list.copy());
System.out.println(bubble.sort(list.copy())); // System.out.println("bubblesort " + bubble.sort(list.copy()));
System.out.println();
System.out.println(merge.sort(list.copy()));
// merge.sort(list.copy()); // merge.sort(list.copy());
// bogo.sort(list.copy()); // bogo.sort(list.copy());
} }

View File

@ -0,0 +1,53 @@
package de.vivi.list;
public class MergeSort implements ByteSort {
@Override
public ByteList sort(ByteList list) {
int mid = list.size() / 2;
ByteList leftList = createSubList(list, 0, mid);
ByteList rightList = createSubList(list, mid, list.size());
ByteList mergedList = mergeList(leftList, rightList, mid);
sortMergedList(mergedList);
return mergedList;
}
private void sortMergedList(ByteList mergedList) {
for (int i = 0; i < (mergedList.size() - 1); i++)
for (int j = 0; j < mergedList.size() - i - 1; j++)
if (mergedList.get(j) > mergedList.get(j + 1)) {
byte temp = mergedList.get(j);
mergedList.set(j, mergedList.get(j + 1));
mergedList.set(j + 1, temp);
}
}
private ByteList mergeList(ByteList leftList, ByteList rightList, int mid) {
ByteList mergedList = new ArrayByteList();
for (int i = 0; i < mid; i++) {
mergedList.add(leftList.get(i));
mergedList.add(rightList.get(i));
}
return mergedList;
}
public ByteList createSubList(ByteList list, int start, int end) {
ByteList subList = new ArrayByteList();
for (int i = start; i < end; i++) {
subList.add(list.get(i));
}
return subList;
}
public ByteList sortSubList(ByteList list, int mid) {
for (int i = 0; i < mid - 1; i++)
for (int j = 0; j < mid - i - 1; j++)
if (list.get(j) > list.get(j + 1)) {
byte temp = list.get(j);
list.set(j, list.get(j + 1));
list.set(j + 1, temp);
}
return list;
}
}