排序-插入-java
1 public class Test { 2 public static void main(String[] args) { 3 int[] arr = {5, 2, 6, 8, 9, 1}; 4 if (arr == null || arr.length <= 2) { 5 return; 6 } 7 for (int i = 0; i < arr.length - 1; i++) { 8 //插入排序:类似打扑克,抓一个数,判断数据在哪个位置,插入进去即可 9 for (int j = i; j >= 0 && arr[j] > arr[j + 1]; j--) { 10 swap(arr, j, j + 1); 11 } 12 } 13 for (int i = 0; i < arr.length; i++) { 14 System.out.println(arr[i]); 15 } 16 } 17 18 private static void swap(int[] arr, int i, int j) { 19 int temp = arr[i]; 20 arr[i] = arr[j]; 21 arr[j] = temp; 22 } 23 }