public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[] a = new int[n]; //生成n个随机数
for(int i = 0; i < n; i++)
a[i] = (int)(Math.random() * 100); /*long start = System.currentTimeMillis();
InsertionSort(a);
long end = System.currentTimeMillis();*/
//100000个测试数据InsertionSort的排序时间约为2800ms /*System.out.println(Arrays.toString(a));
System.out.println(start);
System.out.println(end);
System.out.printf("The time is: %d", end - start);*/ //获取当前时间(ms)
long start = System.currentTimeMillis();
divide(a, 0, a.length - 1);
long end = System.currentTimeMillis();
//100000000个测试数据MergeSort的排序时间约为12000ms 10000000个测试数据用时约为1200ms
//相对于平均时间复杂度O(n^2)的插入排序 随着测试数据数量级的增长 性能提升极大 //System.out.println(Arrays.toString(a));
System.out.println(start);
System.out.println(end);
// end - start 即为排序算法运行的时间
System.out.println("The time is: " + (end - start));
} /*public static void InsertionSort(int[] a)
{
for(int i = 1; i < a.length; i++)
{
int key = a[i];
int j;
for(j = i - 1; j >= 0 && key < a[j]; j--)
a[j + 1] = a[j];
a[j + 1] = key;
}
}*/ public static void divide(int[] a, int front, int rear)
{
if(rear <= front)
return; int mid = front + (rear - front) / 2;
divide(a, mid + 1, rear);
divide(a, front, mid);
merge(a, front, mid, rear);
} public static void merge(int[] a, int front, int mid, int rear)
{
int[] b = new int[rear - front + 1];
int i = front, j = mid + 1, k = 0; while(i <= mid && j <= rear)
if(a[i] < a[j])
b[k++] = a[i++];
else
b[k++] = a[j++]; if(i > mid)
while(j <= rear)
b[k++] = a[j++];
else
while(i <= mid)
b[k++] = a[i++]; for(int t = 0, q = front; t < b.length; t++, ++q)
a[q] = b[t];
}

Insertion Sort 与 Merge Sort的性能比较(Java)的更多相关文章

  1. Insertion Sort and Merge Sort

    Insertion Sort(插入排序) 思路:for 循环遍历数组中的每一个数 用while将每次遍历到的数于左侧的数进行对比,将小的排到左边 void InsertionSort(int*A, i ...

  2. 【Sort】Merge Sort归并排序

    归并排序运行时间O(N log N),但是由于需要线性附加内存,所以很少用于主存排序. 算法核心在于以下三条语句,分治递归,分别对左半边和右半边的数组进行排序,然后把左右半边的数组一一进行比较放入数组 ...

  3. [Algorithms] Sorting Algorithms (Insertion Sort, Bubble Sort, Merge Sort and Quicksort)

    Recently I systematicall review some sorting algorithms, including insertion sort, bubble sort, merg ...

  4. 归并排序(merge sort)

    M erge sort is based on the divide-and-conquer paradigm. Its worst-case running time has a lower ord ...

  5. 基础排序算法之并归排序(Merge Sort)

    并归排序是学习分治法 (Merge Sort) 的好例子.而且它相对于选择,插入,冒泡排序来说,算法性能有一定提升.我首先会描述要解决的问题,并给出一个并归排序的例子.之后是算法的思路以及给出伪代码. ...

  6. 【高级排序算法】2、归并排序法的实现-Merge Sort

    简单记录 - bobo老师的玩转算法系列–玩转算法 -高级排序算法 Merge Sort 归并排序 Java实现归并排序 SortTestHelper 排序测试辅助类 package algo; im ...

  7. 【高级排序算法】1、归并排序法 - Merge Sort

    归并排序法 - Merge Sort 文章目录 归并排序法 - Merge Sort nlogn 比 n^2 快多少? 归并排序设计思想 时间.空间复杂度 归并排序图解 归并排序描述 归并排序小结 参 ...

  8. 【算法】归并排序(Merge Sort)(五)

    归并排序(Merge Sort) 归并排序是建立在归并操作上的一种有效的排序算法.该算法是采用分治法(Divide and Conquer)的一个非常典型的应用.将已有序的子序列合并,得到完全有序的序 ...

  9. [算法]——归并排序(Merge Sort)

    归并排序(Merge Sort)与快速排序思想类似:将待排序数据分成两部分,继续将两个子部分进行递归的归并排序:然后将已经有序的两个子部分进行合并,最终完成排序.其时间复杂度与快速排序均为O(nlog ...

随机推荐

  1. Request.Params

    在开发中有时会用到Request.Params["id"]来获取参数,那么到底是从什么地方接收参数呢? 一般情况下,有三种方式进行参数传递1.GET 方式,通过url传递,如?id ...

  2. C# ListBox实现显示插入最新的数据的方法

    在我们使用ListBox控件时,如果我们在里面不断的添加一条条数据,但是在我们添加的数据过多超过了ListBox显示的窗口时(此时会产生滑动条), 发现我们无法看到最新添加的数据.实现倒序显示此处有两 ...

  3. Android项目刮刮奖详解(一)

    前言 最近正在学鸿洋大大的刮刮奖,感觉学有所得,便是来写篇详解(尽管网上有很多了,不过毕竟是自己写的,自己以后方便复习),正文开始 目标 实现画板功能 思路 我们需要自定义View来实现画板功能,之后 ...

  4. 通俗易懂的来讲讲js的函数执行上下文

    0.开场白 在平时编写JavaScript代码时,我们并不会和执行上下文直接接触,但是想要彻底搞懂JavaScript函数的话,执行上下文是我们绕不过去的一个知识点. 1.执行上下文栈 JavaScr ...

  5. angular打包后路由和文件路径不对

    base href换成如下script标签 <!-- <base href="/"> --> <script> document.write(' ...

  6. 商汤科技汤晓鸥:其实不存在AI行业,唯一存在的是“AI+“行业

    https://mp.weixin.qq.com/s/bU-TFh8lBAF5L0JrWEGgUQ 9 月 17 日,2018 世界人工智能大会在上海召开,在上午主论坛大会上,商汤科技联合创始人汤晓鸥 ...

  7. iOS----------常用三方库

    1.笔者常用三方库 名称 作用 说明 <small>AFNetworking <small>基于HTTP/HTTPS 联网请求 <small> <small& ...

  8. SpringBoot集成mybatis配置

    一个有趣的现象:传统企业大都喜欢使用hibernate,互联网行业通常使用mybatis:之所以出现这个问题感觉与对应的业务有关,比方说,互联网的业务更加的复杂,更加需要进行灵活性的处理,所以myba ...

  9. Android application使用总结

    简介: Application和Activity.Service一样,都是Android框架的一个系统组件,每一个应用都有一个Application,Application的生命周期也就是整个app的 ...

  10. 尝试Java,从入门到Kotlin(下)

    上篇已提(tu)到(cao)Java中的各种坑.习惯了C#的各种特性和语法糖后,再转到Java感觉比较别扭.最后本着反正Java也不是很熟悉,干脆再折腾折腾其他语言的破罐子破摔的心态,逛了一圈JVM语 ...