Implement bubble sort
This commit is contained in:
parent
400dd1e621
commit
2038bc3210
122
Inventar.java
122
Inventar.java
@ -5,42 +5,100 @@ public class Inventar {
|
||||
|
||||
public Inventar() {
|
||||
this.gegenstaende = new Gegenstand[10];
|
||||
gegenstaende[0] = new Gegenstand ("Bogen", 20);
|
||||
gegenstaende[1] = new Gegenstand ("Gürtel", 10);
|
||||
gegenstaende[2] = new Gegenstand ("Helm", 25);
|
||||
gegenstaende[3] = new Gegenstand ("Pfeile", 5);
|
||||
gegenstaende[4] = new Gegenstand ("Ring", 7);
|
||||
gegenstaende[5] = new Gegenstand ("Schild", 30);
|
||||
gegenstaende[6] = new Gegenstand ("Schwert", 18);
|
||||
gegenstaende[7] = new Gegenstand ("Rüstung", 22);
|
||||
gegenstaende[8] = new Gegenstand ("Stiefel", 16);
|
||||
gegenstaende[9] = new Gegenstand ("Kette", 18);
|
||||
gegenstaende[0] = new Gegenstand ("Bogen", 25);
|
||||
gegenstaende[1] = new Gegenstand ("Gürtel", 17);
|
||||
gegenstaende[2] = new Gegenstand ("Helm", 32);
|
||||
gegenstaende[3] = new Gegenstand ("Pfeile", 56);
|
||||
gegenstaende[4] = new Gegenstand ("Ring", 25);
|
||||
gegenstaende[5] = new Gegenstand ("Schild", 19);
|
||||
gegenstaende[6] = new Gegenstand ("Schwert", 8);
|
||||
gegenstaende[7] = new Gegenstand ("Rüstung", 66);
|
||||
gegenstaende[8] = new Gegenstand ("Stiefel", 29);
|
||||
gegenstaende[9] = new Gegenstand ("Kette", 6);
|
||||
gegenstaende[9] = new Gegenstand ("Kette2", 20);
|
||||
gegenstaende[9] = new Gegenstand ("Kette3", 29);
|
||||
//Gegenstand[] gegenstaendeSorted = selectionSort2(gegenstaende);
|
||||
|
||||
gegenstaende = sortArray(gegenstaende);
|
||||
//System.out.println("My insertionSort");
|
||||
//Gegenstand[] gegenstaendeSorted = insertionSort(gegenstaende);
|
||||
//for(int i = 0; i < gegenstaendeSorted.length; i++) {
|
||||
// System.out.println(gegenstaendeSorted[i].getAusdauer());
|
||||
//}
|
||||
|
||||
for(int i = 0; i < gegenstaende.length; i++) {
|
||||
System.out.println(gegenstaende[i].getName());
|
||||
//System.out.println("Mr C insertionSort");
|
||||
//Gegenstand[] gegenstaendeSortedC = insertionSort(gegenstaende);
|
||||
//for(int i = 0; i < gegenstaendeSortedC.length; i++) {
|
||||
// System.out.println(gegenstaendeSortedC[i].getAusdauer());
|
||||
//}
|
||||
|
||||
//Test bubble sort
|
||||
System.out.println("Bubble sort");
|
||||
Gegenstand[] sortedArray = bubbleSort(gegenstaende);
|
||||
for(int i = 0; i < sortedArray.length; i++) {
|
||||
System.out.println("Index " + i + " Ausdauer: " + sortedArray[i].getAusdauer());
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Test binary search:
|
||||
int position = binarySearch(gegenstaende, gegenstaende[2].getName());
|
||||
System.out.println("Found " + gegenstaende[position].getName() + " at position: " + position + " with 'ausdauer': " + gegenstaende[position].getAusdauer());
|
||||
//int position = binarySearch(gegenstaende, gegenstaende[2].getName());
|
||||
//System.out.println("Found " + gegenstaende[position].getName() + " at position: " + position + " with 'ausdauer': " + gegenstaende[position].getAusdauer());
|
||||
}
|
||||
|
||||
public Gegenstand[] sortArray(Gegenstand[] gegenstaende) {
|
||||
// Insertion sort
|
||||
public Gegenstand[] insertionSort(Gegenstand[] gegenstaende) {
|
||||
int i = 1;
|
||||
while(i < gegenstaende.length) {
|
||||
int j = i;
|
||||
|
||||
for (int index = 1; index < gegenstaende.length; index++) {
|
||||
Gegenstand entry = gegenstaende[index];
|
||||
int previousIndex = index;
|
||||
while(j > 0 && gegenstaende[j-1].getAusdauer() > gegenstaende[j].getAusdauer()) {
|
||||
Gegenstand temp = gegenstaende[j-1];
|
||||
gegenstaende[j - 1] = gegenstaende[j];
|
||||
gegenstaende[j] = temp;
|
||||
|
||||
while (previousIndex > 0 && gegenstaende[previousIndex - 1].getName().compareTo(entry.getName()) > 0) {
|
||||
gegenstaende[previousIndex] = gegenstaende[previousIndex - 1];
|
||||
previousIndex--;
|
||||
j--;
|
||||
}
|
||||
gegenstaende[previousIndex] = entry;
|
||||
i++;
|
||||
showItems(gegenstaende);
|
||||
}
|
||||
return gegenstaende;
|
||||
}
|
||||
|
||||
public void showItems(Gegenstand[] gegenstaende) {
|
||||
System.out.print("[ ");
|
||||
for(int i = 0; i < gegenstaende.length; i++) {
|
||||
System.out.print(gegenstaende[i].getAusdauer() + ", ");
|
||||
}
|
||||
System.out.println("");
|
||||
}
|
||||
|
||||
public Gegenstand[] insertionSortC(Gegenstand[] array) {
|
||||
for (int i = 1; i < array.length-1; i++) {
|
||||
Gegenstand key = array[i];
|
||||
int j = i - 1;
|
||||
|
||||
while(j >= 0 && array[j].getAusdauer() > key.getAusdauer()) {
|
||||
array[j + 1] = array[j];
|
||||
j--;
|
||||
}
|
||||
array[j+1] = key;
|
||||
showItems(array);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public Gegenstand[] selectionSort2(Gegenstand[] gegenstaende) {
|
||||
for (int i = 0; i < gegenstaende.length - 2; i++) {
|
||||
int indexDesKleinsten = i;
|
||||
for (int j = i + 1; j < gegenstaende.length - 1; j++) {
|
||||
if (gegenstaende[j].getName().compareTo(gegenstaende[indexDesKleinsten].getName()) < 0) {
|
||||
indexDesKleinsten = j;
|
||||
}
|
||||
}
|
||||
if (indexDesKleinsten != i) {
|
||||
Gegenstand helper = gegenstaende[i];
|
||||
gegenstaende[i] = gegenstaende[indexDesKleinsten];
|
||||
gegenstaende[indexDesKleinsten] = helper;
|
||||
}
|
||||
}
|
||||
return gegenstaende;
|
||||
}
|
||||
|
||||
@ -63,6 +121,20 @@ public class Inventar {
|
||||
return -1;
|
||||
}
|
||||
|
||||
public Gegenstand[] bubbleSort(Gegenstand[] gegenstaende) {
|
||||
int n = gegenstaende.length;
|
||||
for(int i = 0; i <= n-2; i++) {
|
||||
for(int j = 0; j <= n-2-i; j++) {
|
||||
if (gegenstaende[j].getAusdauer() > gegenstaende[j+1].getAusdauer()) {
|
||||
Gegenstand temp = gegenstaende[j];
|
||||
gegenstaende[j] = gegenstaende[j+1];
|
||||
gegenstaende[j+1] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
return gegenstaende;
|
||||
}
|
||||
|
||||
public void lineareSuche(String name) {
|
||||
// to do
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user