插入排序 - Java

/** * 插入排序 */
public class InsertSort {
    public static void main(String[] args) {    
        int[] arr = {2, 0, 4, 9, 1, 7, 3, 0, 6, 8, 3, 1};
        sort(arr);
        for (int num : arr) {
            System.out.println(num);
        }
    }

    public static void sort (int[] arr) {
        for (int i = 1; i < arr.length; i ++) {
            int temp = arr[i];
            int j = i;
            while (j > 0 && arr[j - 1] > temp) {
                arr[j] = arr[j - 1];
                j --;
            }
            arr[j] = temp;
        }
    }
}
点赞