Java implements insertion sort algorithm

Ready to work

Insert picture description here

  1. Understand the use of arrays
  2. Understand the use of loops and the nested use of loops
  3. Analyze the problem: As can be seen from the above animation, the main idea of ​​insertion sort is to insert an element in an ordered arrangement. By default, the first one is considered to be ordered, and the latter elements are compared with the previous ones. The elements of are compared one by one and inserted into the appropriate position; therefore, a loop (outer loop) is needed to control the number of elements; another loop (inner loop) is needed to control the current number of elements behind the elements one by one The ordered elements are compared to find the appropriate position for insertion.
    If the text is not clear, the code demonstration will be clear

Code

public class Test {
    
    
    public static void main(String[] args) {
    
    
        int[] ints = insertionSort(new int[]{
    
    45,22,55,33,44,66,11,23,54,88,16});
        System.out.println(Arrays.toString(ints));
    }


    public static int[] insertionSort(int[] array){
    
    
       //由于默认认为第一个是有序的,则从第二个元素开始比较,因此循环从1开始
        for (int i = 1; i <array.length ; i++) {
    
    
            //从当前元素依次循环遍历与前面所有有序的元素挨个比较,找到合适的插入位置交换位置
            for (int j = i-1; j >=0 ; j--) {
    
    
                //如果后一个数比前面任何一个数小则交换,一直交换到合适的位置(这里是升序排序)
                //如果想要降序排序,则修改为>即可
                if (array[j+1]<array[j]){
    
    
                    int temp=array[j+1];
                    array[j+1]=array[j];
                    array[j]=temp;
                }
            }
        }
        //返回已经排序完毕的数组
        return array;
    }
}

Guess you like

Origin blog.csdn.net/weixin_45608165/article/details/112093956