插入排序

/**
 * @author WenQiang Wu
 * @version Dec 28, 2009
 */
public class InsertSort {

    /**
     * insert sort
     * 
     * @param value
     */
    public static void insertSort(int[] value) {
        for (int i = 1; i < value.length; i++) {
            int key = value[i];
            int j = i - 1;

            while (j >= 0 && value[j] > key) {
                value[j + 1] = value[j];
                j--;
            }
            value[j + 1] = key;
        }

    }

    /**
     * display result
     * 
     * @param value
     */
    public static void display(int[] value) {
        for (int i = 0; i < value.length; i++) {

            System.out.print(value[i] + "  ");
        }
    }

    /**
     * 
     * @param args
     */
    public static void main(String[] args) {
        int[] value = { 12, 8, 3, 59, 132, 1236, 3400, 8, 352, 37 };
        insertSort(value);
        display(value);
    }
}

你可能感兴趣的:(J#)