1. 程式人生 > >六 簡單排序之插入排序

六 簡單排序之插入排序

++ .com pub clas amp 排序 bubuko 變量 image

原理

技術分享圖片

源代碼:

public class insertSort {
public static void sort(int[] array) //插入排序
{
int temp =0; //中間變量
for(int i=1;i<array.length;i++)
{
temp =array[i];
int j =i-1;
while(j>0 && array[j] >temp ) //往前挪動
{
array[j] = array[j-1];

j--;
}
array[j] =temp;

}
}
}

六 簡單排序之插入排序