forked from pabulaner/vivi
Bubblesort overworked und MergeSort
This commit is contained in:
parent
e28f39412b
commit
7704486f5b
@ -1,27 +1,18 @@
|
||||
package de.vivi.list;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class BubbleSort implements ByteSort {
|
||||
|
||||
|
||||
private byte bubbleTemp;
|
||||
private int a;
|
||||
|
||||
@Override
|
||||
public ByteList sort(ByteList list) {
|
||||
int n = list.size();
|
||||
|
||||
while (a < list.size() * list.size()) {
|
||||
for (int i = 0; i < list.size() - 1; i++) {
|
||||
if (list.get(i + 1) < list.get(i)) {
|
||||
bubbleTemp = list.get(i + 1);
|
||||
list.set(i + 1, list.get(i));
|
||||
list.set(i, bubbleTemp);
|
||||
for (int i = 0; i < n - 1; i++)
|
||||
for (int j = 0; j < n - 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);
|
||||
}
|
||||
a++;
|
||||
}
|
||||
}
|
||||
System.out.println(a + " Anzahl runden");
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
@ -10,10 +10,10 @@ public class Main {
|
||||
public static void main(String[] args) {
|
||||
ByteList array = new ArrayByteList();
|
||||
ByteList linked = new LinkedByteList();
|
||||
// ByteList combined = new CombinedByteList(16);
|
||||
// testListVivi(array);
|
||||
testListVivi(linked);
|
||||
// testSortVivi(array);
|
||||
// ByteList combined = new CombinedByteList(16); // Paul meinte das kann weg gelassen werden
|
||||
testListVivi(array);
|
||||
//testListVivi(linked);
|
||||
testSortVivi(array);
|
||||
//testList(array);
|
||||
// testList(linked);
|
||||
// testList(combined);
|
||||
@ -31,9 +31,9 @@ public class Main {
|
||||
list.add((byte) 5);
|
||||
list.add((byte) -100);
|
||||
|
||||
|
||||
System.out.println(list);
|
||||
System.out.println(list.copy());
|
||||
//
|
||||
// System.out.println(list);
|
||||
// System.out.println(list.copy());
|
||||
// list.remove(4);
|
||||
// list.contains(5);
|
||||
// System.out.println(list.contains(2));
|
||||
@ -156,12 +156,14 @@ public class Main {
|
||||
|
||||
private static void testSortVivi(ByteList list) {
|
||||
ByteSort bubble = new BubbleSort();
|
||||
// ByteSort merge = new MergeSort();
|
||||
ByteSort merge = new MergeSort();
|
||||
// ByteSort bogo = new BogoSort();
|
||||
|
||||
// sort the lists with the sorting algorithm
|
||||
// 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());
|
||||
// bogo.sort(list.copy());
|
||||
}
|
||||
|
||||
53
list/src/main/java/de/vivi/list/MergeSort.java
Normal file
53
list/src/main/java/de/vivi/list/MergeSort.java
Normal 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;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user