InsertionSort nochmals geübt

main
Tim Anacker 2026-03-25 17:11:14 +01:00
parent 44d1dd85c2
commit 10438f9e7a
1 changed files with 25 additions and 30 deletions

View File

@ -1,35 +1,11 @@
public class InsertionSort { public class InsertionSort {
public static void main(String[] args) {} public static void main(String[] args) {
System.out.println("Hier ist Insertionsort");
}
public static int[] insertionSort(int[] sortieren) {
int temp = 0;
for(int i = 1; i < sortieren.length; i++) {
if(sortieren[i-1] > sortieren[i]) {
temp=sortieren[i];
sortieren[i]=sortieren[i-1];
sortieren[i-1]=temp;
int j = i;
while(j>0) {
if(sortieren[j-1]>sortieren[j]) {
temp=sortieren[j];
sortieren[j]=sortieren[j-1];
sortieren[j-1]=temp;
}
j--;
}
}
}
return sortieren;
}
public static int[] insertionSortLSG(int[] sortieren) { public static int[] insertionSortLSG(int[] sortieren) {
int temp; int temp;
@ -38,10 +14,29 @@ public class InsertionSort {
int j = i; int j = i;
while (j > 0 && sortieren[j - 1] > temp) { while (j > 0 && sortieren[j - 1] > temp) {
sortieren[j] = sortieren[j - 1]; sortieren[j] = sortieren[j - 1];
j--; j--; }
}
sortieren[j] = temp; sortieren[j] = temp;
} }
return sortieren; return sortieren;
} }
} }
public static void insertionsorttest(int[] arr){
int tmp;
for(int i =1; i <arr.length; i++){
tmp = arr[i];
int j = i;
while(j > 0 && arr[j-1]>tmp){
arr[j] = arr[j-1];
}
arr[j]=tmp;
}
}
void main() {
}