Skip to content

Instantly share code, notes, and snippets.

@ehgoodenough
Last active December 21, 2015 21:09
Show Gist options
  • Save ehgoodenough/6366756 to your computer and use it in GitHub Desktop.
Save ehgoodenough/6366756 to your computer and use it in GitHub Desktop.
An implementation of an iterative insertion sort.
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