内排序-插入排序

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

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);
            }
        }

    }

}

猜你喜欢

转载自www.cnblogs.com/mytrip/p/9478278.html
今日推荐