package test;
public class test {
public static void main(String[] args){
int[] x = { 6, 2, 4, 1, 5, 9 };
insertion_sort(x);
System.out.println("最终排序结果: ");
print(x);
}
/// 插入排序
static void insertion_sort(int[] unsorted)
{
for (int i = 1; i < unsorted.length; i++)
{
int temp = unsorted[i];
int j = i-1;
while (j >=0 && temp<unsorted[j])
{
unsorted[j+1] = unsorted[j];
j--;
}//while循环里面的目的是:将大于temp值的数整体向后移动一个单位
unsorted[j+1] = temp;
print(unsorted);
System.out.println
直接插入排序法——java语言实现
最新推荐文章于 2024-10-06 18:21:54 发布
