1
0
forked from pabulaner/vivi

Bubble sort

This commit is contained in:
happymeal2024 2025-04-07 18:15:43 +02:00
parent f5abcb10d3
commit 9aa1ec26db
4 changed files with 30 additions and 32 deletions

View File

@ -82,7 +82,7 @@ public class ArrayByteList implements ByteList {
@Override @Override
public ArrayByteList copy() { public ArrayByteList copy() {
ArrayByteList newArrayList = new ArrayByteList(); ArrayByteList newArrayList = new ArrayByteList();
newArrayList.array = Arrays.copyOf(array, size); newArrayList.array = Arrays.copyOf(array, array.length);
return newArrayList; return newArrayList;
} }
@ -94,11 +94,8 @@ public class ArrayByteList implements ByteList {
} }
@Override @Override
public ArrayByteListIterator<Byte> iterator() { public ArrayByteListIterator iterator() {
ArrayByteListIterator<Byte> it = new ArrayByteListIterator<Byte>(); ArrayByteListIterator it = new ArrayByteListIterator(this);
while(it.hasNext()) {
it.next();
}
return it; return it;
} }
} }

View File

@ -3,19 +3,19 @@ package de.vivi.list;
import java.util.Iterator; import java.util.Iterator;
import java.util.function.Consumer; import java.util.function.Consumer;
public class ArrayByteListIterator<Byte> implements Iterator<Byte> { public class ArrayByteListIterator implements Iterator<Byte> {
// ArrayByteList array; private ArrayByteList array;
// private Byte nextByte; // private Byte nextByte;
private int currentIndex = 0; private int currentIndex = 0;
// public ArrayByteListIterator() { public ArrayByteListIterator(ArrayByteList array) {
// array = new ArrayByteList(); this.array = array;
// } }
@Override @Override
public boolean hasNext() { public boolean hasNext() {
return array.get(currentIndex) != 0; return currentIndex < array.size();
} }
@Override @Override

View File

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

View File

@ -27,7 +27,8 @@ public class Main {
list.add((byte) 5); list.add((byte) 5);
list.add((byte) -86); list.add((byte) -86);
list.add((byte) 3); list.add((byte) 3);
// list.add((byte) 5); list.add((byte) 5);
list.add((byte) -100);
// System.out.println(list); // System.out.println(list);
@ -71,9 +72,9 @@ public class Main {
// list.clear(); // list.clear();
// System.out.println(list); // System.out.println(list);
for (byte value : list) { // for (byte value : list) {
System.out.println(value); // System.out.print("Hello it " + value + " ");
} // }
} }
@ -152,7 +153,7 @@ public class Main {
// 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(bubble.sort(list.copy()));
// merge.sort(list.copy()); // merge.sort(list.copy());
// bogo.sort(list.copy()); // bogo.sort(list.copy());