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

View File

@ -3,19 +3,19 @@ package de.vivi.list;
import java.util.Iterator;
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 int currentIndex = 0;
// public ArrayByteListIterator() {
// array = new ArrayByteList();
// }
public ArrayByteListIterator(ArrayByteList array) {
this.array = array;
}
@Override
public boolean hasNext() {
return array.get(currentIndex) != 0;
return currentIndex < array.size();
}
@Override

View File

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

View File

@ -27,7 +27,8 @@ public class Main {
list.add((byte) 5);
list.add((byte) -86);
list.add((byte) 3);
// list.add((byte) 5);
list.add((byte) 5);
list.add((byte) -100);
// System.out.println(list);
@ -71,9 +72,9 @@ public class Main {
// list.clear();
// System.out.println(list);
for (byte value : list) {
System.out.println(value);
}
// for (byte value : list) {
// System.out.print("Hello it " + value + " ");
// }
}
@ -152,7 +153,7 @@ public class Main {
// ByteSort bogo = new BogoSort();
// sort the lists with the sorting algorithm
bubble.sort(list.copy());
// bubble.sort(list.copy());
System.out.println(bubble.sort(list.copy()));
// merge.sort(list.copy());
// bogo.sort(list.copy());