Last active
December 21, 2015 21:09
-
-
Save ehgoodenough/6366756 to your computer and use it in GitHub Desktop.
An implementation of an iterative insertion sort.
This file contains hidden or 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 class Insortion | |
{ | |
public static void main(String[] args) | |
{ | |
char[] array = {'a', 'c', 'b', 'e', 'd'}; | |
insertionSort(array); | |
for(int i = 0; i < array.length; i++) {System.out.print(array[i]);} | |
} | |
public static void insertionSort(char[] array) | |
{ | |
for(int i = 1; i < array.length; i++) | |
{ | |
if(array[i] < array[i-1]) | |
{ | |
char value = array[i]; | |
for(int j = i-1; j >= 0; j--) | |
{ | |
if(value > array[j]) | |
{ | |
break; | |
} | |
array[j+1] = array[j]; | |
array[j] = value; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment