2018-03-22

2018-03-22  本文已影响0人  学打坐的船长
import java.util.Arrays;

/**
 * @author captain
 *
 */
public class InsertSort {

public static void insertSort(int[] array, int nextIndex) {
    if (nextIndex > array.length - 1) {
        return;
    }

    for (int i = 0; i < nextIndex; i++) {
        if (array[i] > array[nextIndex]) {
            swap(array, i, nextIndex);
        }
    }
    nextIndex++;
    insertSort(array, nextIndex);
}

public static void swap(int[] array, int i, int j) {
    int tmp = array[i];
    array[i] = array[j];
    array[j] = tmp;
}

/**
 * @param args
 */
public static void main(String[] args) {
    int[] testArray = {57, 68, 59, 52, 4, 4, 6, 7, 3, 22, 33, 44, 66};
    insertSort(testArray, 1);
    System.out.println(Arrays.toString(testArray));
}

}
上一篇下一篇

猜你喜欢

热点阅读