public class InsertionSortAlgorithmTest {
public static String arrayToString(int[] array){
StringBuilder sb = new StringBuilder();
for(int a:array){
sb.append(a).append(" ");
}
return sb.toString();
}
public static int[] insertionSort(int[] array){
int key;
int j;
for (int i = 1; i < array.length; i++) {
key = array[i];
j = i - 1;
while (j >= 0 && array[j] > key) {
array[j + 1] = array[j];
j = j - 1;
}
array[j + 1] = key;
System.out.println(arrayToString(array));
}
return array;
}
public static void main(String[] args) {
int[] array = {2, 3, 1, 7, 5, 9, 4, 6, 8};
insertionSort(array);
}
}
posted on 2014-02-18 23:23
Jimi 阅读(1523)
评论(0) 编辑 收藏 所属分类:
算法