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