`
tiandizhiguai
  • 浏览: 44687 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

插入排序

阅读更多
public class Sort
{

    public static void main(String[] args)
    {
        int[] arr = new int[]{8,6,15,45,12,36,25,5};
        insertSort(arr);
        
        for(int a : arr)
        {
            System.out.print(a + " ");
        }
    }

    /**
     * 插入排序,适用于少量数据的排序,时间复杂度O(n2),是稳定的排序算法,原地排序
     * 
     * @param a
     */
    public static void insertSort(int[] a)
    {
        int length = a.length;

        for (int i = 1; i < length; i++)
        {
            int temp = a[i];
            int j = i;
            for (; j > 0 && a[j - 1] > temp; j--)
            {
                a[j] = a[j - 1];
            }
            a[j] = temp;
        }
    }
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics