package sort;
/**
* 插入排序算法思想:插入排序是从第二个位置开始,判断其前面一位与之大小关系,如果小于前一位的数据
* 则进行交换,依次往前比较。当比较完成之后,进行第二轮选择下一位的数据开始上一轮的比较。
* @author 2萌
*
*/
public class test_insertSort {
public static void insertSort(int[] array) {
for (int i = 1; i < array.length; i++) {
int temp = array[i];
int j = i-1;
while (j>=0 && array[j]>temp) {
swap(array, j, j+1);
j--;
}
}
}
public static void swap(int[] array, int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array = {6,9,3,5,7,1,0,8,10};
insertSort(array);
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
}
07-23
07-23
07-23