算法

插入排序

public class InsertionSort {

    public static void main(String[] args) {
        int[] arr = { 23, 5, 14, 8, 49 };
        for (int p = 1; p < arr.length; p++) {
            int temp = arr[p];
            int position = p;
            for (int j = p - 1; j >= 0; j--) {
                if (arr[j] > temp) {
                    arr[j + 1] = arr[j];
                    position -= 1;
                } else {
                    break;
                }
            }
            arr[position] = temp;
        }
        System.out.println(Arrays.toString(arr));
    }
}

稳定排序

你可能感兴趣的:(算法)