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

内排序-插入排序

时间:2018-08-20 00:29:48      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:...   static   import   --   算法思想   code   hang   插入   span   

算法思想:每步将一个待排序的记录,插入前面已经排序的序列适当位置上,并使之也有序,重复该过程,直到全部数据插入完为止。

package Sort;

import java.util.Arrays;

public class DirectSort
{
    public static void main(String[] args)
    {

        int[] a = new int[] { 20, 30, 90, 40, 70, 110, 60, 10, 100, 50, 80, 1, 2 };
        DirectSort directSort = new DirectSort();
        directSort.Sort(a);
        ;
        for (int t : a)
            System.out.println(t);
    }

    void ExchangeData(int[] datas, int index1, int index2)
    {
        datas[index1] = datas[index2] + (datas[index2] = datas[index1]) * 0;
    }

    public void Sort(int[] array)
    {
        for (int i = 1; i < array.length; i++)//交换次数,1,2,3,4,。。。。。n-1次 注意冒泡是 n-1,n-2,n-3.....1次
        {
            for (int j = i; j > 0 && array[j - 1] > array[j]; j--)
            {
                ExchangeData(array, j - 1, j);
            }
        }

    }

}

 

内排序-插入排序

标签:...   static   import   --   算法思想   code   hang   插入   span   

原文地址:https://www.cnblogs.com/mytrip/p/9478278.html

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