Sorting-JAVA Implementation [3] Insertion Sort

package org.lion.euler.study.sort;

/**
 * Insertion sort
 * <pre>
 * Principle: Starting from the second value, compare all the values ​​before the value in turn, until after the value smaller than the value, and then insert.
 * </pre>
 * @author lion
 *
 */
public class InsertSort extends AbstractSort {

	@Override
	public void sort(Integer[] array) {
		for (int i = 0, j = i; i < array.length - 1; j = ++i) {
            int ai = array[i + 1];
            while (ai < array[j]) {
            	array[j + 1] = array[j];
                if (j-- == 0) {
                    break;
                }
            }
            array[j + 1] = ai;
        }
	}

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325981328&siteId=291194637