Hallo, ich hab das oben genannte Problem.
Durch den Befehl print(bubblesort(a3)); wird mein Programm frühzeitig beendet. Aber wieso?? Irgendwie ist mein Eclipse auch auf einmal so lahm. Vlt habe ich irgendwo eine Endlosschleife gebaut...
Vielen Dank schon mal im Voraus!
Hier der Code:
Durch den Befehl print(bubblesort(a3)); wird mein Programm frühzeitig beendet. Aber wieso?? Irgendwie ist mein Eclipse auch auf einmal so lahm. Vlt habe ich irgendwo eine Endlosschleife gebaut...
Vielen Dank schon mal im Voraus!
Hier der Code:
Java:
public class Praesenz03 {
public static void main(String[] args) {
int[] a1 = { 1, 2, 4, 3 };
int[] a2 = { 4, 2, 3, 1 };
int[] a3 = { 1, 2, 3, 4 };
int[] a4 = { 2, 3, 1, 4 };
int[] b1 = { 4, 3, 2, 1 };
int[] b2 = { 2, 3, 1, 4 };
int[] b3 = { 4, 3, 2, 1 };
int[] b4 = { 2, 3, 1, 4 };
print(insertionsort(a1));
print(insertionsort(b1));
print(selectionsort(a2));
print(selectionsort(b2));
print(bubblesort(a3));
print(bubblesort(b3));
print(mergesort(a4));
print(mergesort(b4));
}
public static int[] insertionsort(int[] a) {
int w;
int n = 0;
int j;
for (int i = 1; i < a.length; i++) {
j = i;
w = a[i];
while (j > 0 && a[j - 1] > w) {
a[j] = a[j - 1];
j--;
n++;
}
a[j] = w;
}
System.out.println("Anzahl der Schritte: " + n);
return a;
}
public static int[] selectionsort(int[] a) {
int k = 0;
int n = 0;
for (int i = 0; i < a.length - 1; i++) {
int min = i;
for (int j = i + 1; j < a.length; j++) {
if (a[min] > a[j])
min = j;
n++;
}
k = a[i];
a[i] = a[min];
a[min] = k;
}
System.out.println("Anzahl der Schritte: " + n);
return a;
}
public static int[] bubblesort(int[] a) {
int h;
int n = 0;
boolean swapped;
do {
swapped = false;
for (int i = 0; i < a.length - 1; i++) {
if (a[i] > a[i + 1]) {
h = a[i];
a[i] = a[i + 1];
a[i + 1] = h;
n++;
swapped = true;
}
}
} while (swapped = true);
System.out.println("Anzahl der Schritte: " + n);
return a;
}
public static int[] mergesort(int[] a) {
return a;
}
public static void print(int[] a) {
for (int x : a)
System.out.print(a[x - 1]);
System.out.println();
}
}