Sortieren: Unterschied zwischen den Versionen
Aus EINI
Marius (Diskussion | Beiträge) (→Bubblesort) |
Marius (Diskussion | Beiträge) (→Insertionsort) |
||
| Zeile 25: | Zeile 25: | ||
===Insertionsort=== | ===Insertionsort=== | ||
| − | Insertionsort | + | <source lang="java"> |
| + | Insertionsort(int[] A) | ||
| + | for (int i = 1; i < A.length; i++){ | ||
| + | int insort = A[i]; | ||
| + | int j = i; | ||
| + | while (j > 1 && A[i-j] > insort){ | ||
| + | A[j] = A[j-1]; | ||
| + | j--; | ||
| + | } | ||
| + | A[j] = insort; | ||
| + | } | ||
| + | </source> | ||
===Mergesort=== | ===Mergesort=== | ||
===Quicksort=== | ===Quicksort=== | ||
Version vom 28. November 2015, 15:25 Uhr
Das Sortieren ist ein Standardproblem der Informatik
Inhaltsverzeichnis
Sortieralgoritmen
Bubblesort
bubbleSort(int[] A)
for(int n = A.length; n > 1; n--) {
for (int i = 0; i < n-1; i++) {
if (A[i] > A[i+1]){
int swap = A[i];
A[i] = A[i+1];
A[i+1] = swap;
}
}
}
Heapsort
weitere Sortieralgorithmen, die nicht Teil dieser Veranstaltung sind
Insertionsort
Insertionsort(int[] A)
for (int i = 1; i < A.length; i++){
int insort = A[i];
int j = i;
while (j > 1 && A[i-j] > insort){
A[j] = A[j-1];
j--;
}
A[j] = insort;
}