Created
October 19, 2023 08:12
-
-
Save SpyrexDE/04b34f83dd7b1e9a7295526cbf983532 to your computer and use it in GitHub Desktop.
InsertionSort in java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int[] insertionSort(int[] eingabe) { | |
int laenge = eingabe.length; | |
for (int i = 1; i < laenge; i++) { | |
int key = eingabe[i]; | |
int j = i - 1; | |
// Verschiebe Elemente des Arrays, die größer sind als key | |
while (j >= 0 && eingabe[j] > key) { | |
eingabe[j + 1] = eingabe[j]; // Setze größeren Wert von index i auf index j | |
j--; // Verkleinere den untersuchten Bereich | |
} | |
eingabe[j + 1] = key; // Da aktuelle eingabe[j] < key, setzte eingabe[j + 1] = key | |
} | |
return eingabe; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment