Insertion Sort is analogous to sorting cards
public class InsertionSort {
public static void main(String[] args) {
short numberArray[] = {7,99,-4,13,1,675,-37};
for(byte i = 1; i < numberArray.length;i++)
{
short key = numberArray[i];
byte j = (byte) (i - 1);
while(j >= 0 && numberArray[j] > key)
{
numberArray[j + 1] = numberArray[j];
j = (byte) (j - 1);
}
numberArray[j + 1] = key;
}
for(byte k = 0;k < numberArray.length;k++)
{
System.out.print(numberArray[k] + " ");
}
}
}
No comments:
Post a Comment