thought

Insertion sort, you can think of it like we’re playing poker. When we get the first card, our hand is in order. When we get the second card, we need to compare the left or right side of the first card that we put the second card in. When you get the third card, you need to insert it in the right place in order. . After taking the cards, our hands are in order.

The illustration

code

`

public static void insertionSort(int[] arr){ if (arr == null || arr.length<2){ return; } for (int I =1; i<arr.length; i++){ for (int j=i-1; j>=0 &&arr[j]>arr[j+1]; j--){ swap(arr,j,j+1); } } } public static void swap(int[] arr,int i,int j){ arr[i] = arr[i]^arr[j]; arr[j] = arr[i]^arr[j]; arr[i] = arr[i]^arr[j]; }Copy the code

`