public class InsertSort
{
public static void main(String [] args){
long [] arr = {23 , 45 , 6 , 56 , 8,10,-980,-10,33,44};
sort(arr);
show(arr);
}
public static void show(long [] arr){
for(long val : arr){
System.out.print(val+"\t");
}
System.out.print("\n");
}
public static void sort(long [] arr){
long temp = 0;
for(int i = 1; i< arr.length;i++)
{
temp = arr[i];//设置哨兵
int j = i -1;
<span style="white-space:pre"> </span>//循环比较
for(; (j>=0 && arr[j] > temp) ; j--)// 注意:j>=0 条件必须写在 arr[j] > temp 之前,否则会出现下标越界问题
{
arr[j+1] = arr[j]; //大的数向后移动
}
arr[j+1] = temp;//找到位置插入
}
}
}