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. Docker日志管理--docker部署安装ELK (十一)--技术流ken

    Docker logs 对于一个运行的容器,Docker 会将日志发送到 容器的 标准输出设备(STDOUT)和标准错误设备(STDERR),STDOUT 和 STDERR 实际上就是容器的控制台终端 ...

  2. 消息队列中间件(二)使用 ActiveMQ

    ActiveMQ 介绍 Active MQ 是由 Apache 出品的一款流行的功能强大的开源消息中间件,它速度快,支持跨语言的客户端,具有易于使用的企业集成模式和许多的高级功能,同时完全支持 JSM ...

  3. .net mvc 导出excel表格

    使用using System.IO; /// /// 导出Excel文件,并自定义文件名 /// public static void DataTableExcel(System.Data.DataT ...

  4. 使用Linq查找重复

    namespace RemoveTheSame { class Program { static void Main(string[] args) { List<User> list = ...

  5. Nginx 初識

    今天簡單了解了一下Nginx,并在本機安裝,并簡單配置了一下,道理什麼的還不懂,就是看能不能跑起來. 1.安裝從官網下載就好,把文件隨便解壓在一個英文目錄裡面. 然後修改配置文件,修改的內容如下: 2 ...

  6. 学JAVA第七天,循环深入了解

    因为星期五放假,所以今天补回. 上次已经解释过循环了,现在我们来进一步了解. 例如for循环:for( int i=0 : i<10 : i++ ){需要循环的内容},这样就会循环10次了 如果 ...

  7. Flask 系列之 Migration

    说明 操作系统:Windows 10 Python 版本:3.7x 虚拟环境管理器:virtualenv 代码编辑器:VS Code 实验目标 通过使用 flask-migrate 实现数据库的迁移操 ...

  8. MySQL 常用指令小结

    l  创建数据库:CREATE DATABASE table_name; l  删除数据库:DROP DATABASE table_name; l  展示数据库:SHOW DATABASE; l  选 ...

  9. Easyui datagrid 设置内容超过单元格宽度时自动换行显示

    datagrid 设置内容超过单元格宽度时自动换行显示 by:授客 QQ:1033553122 测试环境 jquery-easyui-1.5.3 问题描述 单元格内容超过单元格宽度不会自动化换行.如下 ...

  10. Go-Ethereum 1.7.2 结合 Mist 0.9.2 实现代币智能合约的实例

    目录 目录 1.什么是 Mist 2.Mist 在哪里下载? 3.Mist 有哪些依赖? 4.如何安装 Mist? 4.1.安装 Mist 依赖工具包 4.2.安装 Mist 4.3.启动 Mist, ...