Created
September 2, 2019 19:17
-
-
Save Unkn0wnCat/148b66f43d677f41873b855f98a87800 to your computer and use it in GitHub Desktop.
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
import java.util.Arrays; | |
public class insertionSort { | |
public static void main(String[] args) { | |
int[] unsorted = {20, 66, 80, 33, 71, 1, 40, 64, 75, 14}; | |
int[] sorted = insertionSort(unsorted); | |
} | |
public static int[] insertionSort(int[] input) { | |
int[] array = input; | |
for (int i = 0; i < array.length; i++) { | |
int current = array[i]; | |
int pos = i; | |
while(pos > 0 && array[pos-1] > current) { | |
array[pos] = array[pos-1]; | |
pos--; | |
} | |
array[pos] = current; | |
} | |
return array; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment