码迷,mamicode.com
首页 > 编程语言 > 详细

插入排序

时间:2017-09-04 18:53:03      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:插入排序   []   ring   string   res   insert   print   while   import   

package alg;

import java.util.Arrays;

/**
 * 插入排序
 * Created by dinghaiyun on 2017/9/1.
 */
public class InsertionSort {
    public static void main(String[] args) {
        int[] arr = {5, 1, 3, 6, 7, 8, 2};
        sort(arr);
        System.out.println("insert sort result: " + Arrays.toString(arr));
    }

    private static void sort(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            int current = arr[i];
            int j = i - 1;

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

 

插入排序

标签:插入排序   []   ring   string   res   insert   print   while   import   

原文地址:http://www.cnblogs.com/tracer-dhy/p/7474895.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!