Insertion Sort Case

1. Introduction to Insertion Sort:

Put the number on the right side of the vertical line to the right position on the left side of the vertical line every time {10|6,4,2}//For the first time, put the number 6 on the right side of the vertical line at the appropriate position on the left side of the vertical line 10 The front of the vertical line becomes {6,10|4,2} {6,10|4,2}//For the second time, the number 4 on the right side of the vertical line is placed (inserted) in front of the appropriate position 6 on the left side of the vertical line , it becomes {4,6,10|2} {4,6,10|2}//For the third time, the number 2 on the right side of the vertical line is placed (inserted) in front of the appropriate position 4 on the left side of the vertical line, and it becomes into {2,4,6,10}

Summary: Insert four numbers three times

Second, according to the prompt derivation:

package com;

import java.util.Arrays;


//插入排序介绍:
//每次把竖线右边的那个数字放到竖线左边合适位置
//{10|6,4,2}//第一次把竖线右边的那个数字6放到(插入到)竖线左边合适位置10的前边,变成{6,10|4,2}
//{6,10|4,2}//第二次把竖线右边的那个数字4放到(插入到)竖线左边合适位置6的前边,变成{4,6,10|2}
//{4,6,10|2}//第三次把竖线右边的那个数字2放到(插入到)竖线左边合适位置4的前边,变成{2,4,6,10}
//总结:四个数插入三次
public class Test {
    public static void main(String[] args) {

        //一、第一次代码实现--这段代码功能:把6插入到 竖线"|"  前边合适位置(把它放到比它大的数前边<放到10的前边>)
        int[] ary={10,6,4,2};//{10|6,4,2}
        //         0 1 2 3
        int index=1;//6所处的位置///
        int t=ary[index];//用做比较的数6(待插入的数)/
        int left_Index=index-1;//10所处的位置
        while(left_Index>=0 && t<ary[left_Index]){//如果ary[left_Index]10大于t中的6,  就把10往后复制(把第0位赋值给第1位,这两位都存的是10)
            ary[left_Index+1]=ary[left_Index];//就把10往后复制(把第0位赋值给第1位,这两位都存的是10)(6在t中存储了,没有丢)
            left_Index--;//把left_Index从0变成-1,然后第二次判断是否执行while,发现第二次不能执行(只执行一次while,只把6放到10前边了)
        }
        ary[left_Index+1]=t;//把t中存对得6放入到 -1+1位(放到第0位)
        System.out.println(Arrays.toString(ary));


        //二、第二次代码实现--这段代码功能:把4插入到 竖线"|"  前边合适位置(把它放到比它大的数前边<放到6的前边>)
        //            0  1 2 3
        ary=new int[]{6,10,4,2};//{6,10,|4, 2}
        index=2;//6所处的位置///
        t=ary[index];//用做比较的数4(待插入的数)/
        left_Index=index-1;//10所处的位置
        while(left_Index>=0 && t<ary[left_Index]){//如果ary[left_Index]10大于t中的4,  就把10往后复制(把第0位赋值给第1位,这两位都存的是10)
            ary[left_Index+1]=ary[left_Index];//就把10往后复制(把第0位赋值给第1位,这两位都存的是10)(4在t中存储了,没有丢)
            left_Index--;
 //把left_Index从1变成0,然后第二次判断是否执行while,发现第二次可以执行while(下一次又会用while的这三行代码将6往后复制<最后将t中的4插入到原本6的位置>)
        }
        ary[left_Index+1]=t;//把t中存对得4放入到 -1+1位(放到第0位<放到6的前边>)
        System.out.println(Arrays.toString(ary));



        //三、第三次代码实现--自己把上边第二段代码在这里写一遍实现把2插入到竖线"|"  前边合适位置(把它放到比它大的数前边<放到6的前边>)
        ary=new int[]{6,10,4,2};//{6,10,,4,|2}


        //四、把第二三段代码注释掉,然后给第一段代码的外层加一个for循环来实现同样效果(用一个循环控制这段相似代码执行三次)

    }
}



Third, the complete code of insertion sort:

do it yourself

//        int[] array={10,6,4,2};
//        for(int index = 1; index<array.length; index++){//外层向右的index,即作为比较对象的数据的index
//            int temp = array[index];//用作比较的数据
//            int leftindex = index-1;
//            while(leftindex>=0 && array[leftindex]>temp){//当比到最左边或者遇到比temp小的数据时,结束循环
//                array[leftindex+1] = array[leftindex];
//                leftindex--;
//            }
//            array[leftindex+1] = temp;//把temp放到空位上
//        }
//        System.out.println(Arrays.toString(array));

Guess you like

Origin blog.csdn.net/gulanga5/article/details/124038855