sortieralgorithmus/src/gfn/marc/Main.java

65 lines
1.7 KiB
Java

package gfn.marc;
import java.util.Arrays;
public class Main {
static int[] array1 = {35, 7, 21, 14, 42, 28};
static int[] array2 = {14, 7, 21, 28, 35, 42};
static int[] array3 = {7, 14, 21, 28, 35, 42};
public static void main(String[] args) {
System.out.println(Arrays.toString(array1));
System.out.println(Arrays.toString(sortiereArray(array1)));
}
public static int[] sortiereArray(int[] array) {
boolean fertig = false;
int temp = 0;
int zaehlerDurchlaeufe = 0;
int zaehlerTauschvorgaenge = 0;
while (!fertig) {
for (int i = 0; i < array.length - 1; i++) {
zaehlerDurchlaeufe++;
fertig = true;
if (array[i] > array[i + 1]) {
fertig = false;
temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
zaehlerTauschvorgaenge++;
for (int j = i; j > 0; j--) {
if (array[j] < array[j - 1]) {
temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
zaehlerTauschvorgaenge++;
} else {
break;
}
}
break;
}
if (i == array.length - 1) {
fertig = true;
}
}
}
System.out.println("\nTauschvorgänge: " + zaehlerTauschvorgaenge);
System.out.println("Durchläufe: " + zaehlerDurchlaeufe);
return array;
}
}