Skip to content

Instantly share code, notes, and snippets.

@Unkn0wnCat
Created September 2, 2019 19:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Unkn0wnCat/148b66f43d677f41873b855f98a87800 to your computer and use it in GitHub Desktop.
Save Unkn0wnCat/148b66f43d677f41873b855f98a87800 to your computer and use it in GitHub Desktop.
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