java中的插入排序算法

public static void insertsort(int[] array)
    {
        int temp;
        for(int i =1;i<array.length;i++)
        {
            for(int j=i;j>0;j--)
            {
                if(array[j]<array[j-1])
                {
                    temp = array[j-1];
                    array[j-1] = array[j];
                    array[j] = temp;
                }
            }
        }
    }
    public static void main(String[] args) {
        int[] array = {100, 45, 17, 36, 21, 17, 13, 7};
        System.out.println("数组的长度:" + array.length);
        System.out.println("排序前的数组:"+Arrays.toString(array));
        insertsort(array);
        System.out.println("排序后的数组:"+Arrays.toString(array));
        for(int i : array)
        {
            System.out.print(i + " " );
        }
    }

猜你喜欢

转载自www.cnblogs.com/suyun0702/p/12673426.html