Created
November 20, 2017 20:31
-
-
Save Kwisses/1b8870bcff6f9609d0286cfc32570de9 to your computer and use it in GitHub Desktop.
InsertionSort - [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
package insertionsort; | |
class IS7 { | |
private static void printArray(int[] array) { | |
for(int i: array) { | |
System.out.print(i + " "); | |
} | |
System.out.println(); | |
} | |
private static void insertionSort(int[] array) { | |
int key, value; | |
for(int i=1; i < array.length; i++) { | |
key = i; | |
value = array[i]; | |
while(key > 0 && value < array[key - 1]) { | |
array[key] = array[key - 1]; | |
key--; | |
} | |
array[key] = value; | |
} | |
} | |
public static void main(String args[]) { | |
int[] array = { 5, 4, 3, 2, 1 }; | |
printArray(array); | |
insertionSort(array); | |
printArray(array); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment