算法导论第一章

来源:互联网 发布:单片机小车制作流程 编辑:程序博客网 时间:2024/05/16 18:01

插入排序

package com.gds.algorithms;/** * Created by yangzhanku on 2017/9/7. */public class InsertionSortTest {    public static void main(String[] args) {        int[] arrayTest = {100, 9, 8, 78, 90, 10};        InsertionSort(arrayTest);    }    private static void InsertionSort(int[] arrayTest) {        for (int j = 1; j < arrayTest.length; j++) {            int key = arrayTest[j];            int i = j - 1;            while (i >= 0 && arrayTest[i] > key) {                arrayTest[i + 1] = arrayTest[i];                i--;            }            arrayTest[i + 1] = key;        }        for (int k :                arrayTest) {            System.out.print(k + " ");        }    }}
原创粉丝点击