Natural Merge Sort(自然归并排序)
This is a Natural Merge Sort program from my textbook. It works, but I don't think it's good.
// Natural merge sort program in the textbook
public class NaturalMergeSortProgram {
public static void main(String[] args)
{
int a[] = new int[10000000];
int b[] = new int[a.length];
for (int i = 0; i < a.length; i++)
a[i] = (int)(1+Math.random()*(1000-1+1));
long starTime=System.currentTimeMillis();
NaturalMergeSort(a, b);
long endTime=System.currentTimeMillis();
long Time = endTime - starTime;
System.out.println("executing time: "+Time+"(ms)");
/*for (int i = 0; i < b.length; i++)
{ if (i % 20 == 0)
System.out.println();
System.out.print(a[i]+" ");
}*/
}
public static void NaturalMergeSort(int a[], int b[])
{ // merge array a into b and then b into a until sorted
while (!MergeRuns(a, b) & !MergeRuns(b, a));
}
public static boolean MergeRuns(int a[], int b[])
{
int i = 0, k = 0;
int n = a.length;
boolean asc = true;
int x;
while (i < n)
{
k = i;
do
x = a[i++];
while (i < n && x <= a[i]); // elements are increasing
while (i < n && x >= a[i]) // elements are decreasing
x = a[i++];
merge(a, b, k , i-1, asc);
asc = !asc;
}
return k == 0;
}
public static void merge(int a[], int b[], int low, int high, boolean asc)
{ // merge a[low:high] into b[low:high]
int k = asc ? low : high;
int c = asc ? 1 : -1;
int i = low, j = high;
while (i <= j)
{
if (a[i] <= a[j])
b[k] = a[i++];
else
b[k] = a[j--];
k += c;
}
}
}
Or maybe I don't get it? Because it's rather obscure and lack of comments( these comments are all added by me).
So I decide to write my own Natural Merge Sort program:
// My own natural merge sort program
public class MyNaturalMergeSort { public static void main(String args[]) {
int a[] = new int[10000000];
int b[] = new int[a.length];
for (int i = 0; i < a.length; i++)
a[i] = (int)(1+Math.random()*(1000-1+1)); long starTime=System.currentTimeMillis();
while (!NaturalMergeSort(a, b) && !NaturalMergeSort(b, a));
long endTime=System.currentTimeMillis();
long Time = endTime - starTime;
System.out.println("executing time: "+Time+"(ms)");
for (int i = 0; i < 100; i++)
{ if (i % 20 == 0)
System.out.println();
System.out.print(a[9999*i]+" ");
}
System.out.println(a[a.length-1]);
} public static boolean NaturalMergeSort(int x[], int y[]) {
// find the two adjacent natural increasing arrays x[l:m] and x[m+1:r],
// then merge them into y[l:r] using function merge()
int i, l = 0, m = 0, r;
for (i = 0; i < x.length; i++)
{ l = i;
while ((i < x.length-1) && (x[i] <= x[i+1])) // get x[l:m]
i++;
m = i++;
while ((i < x.length-1) && (x[i] <= x[i+1])) // get x[m+1:r]
i++;
r = (i == x.length) ? i-1 : i; // if it's true, that means array x is
// already sorted, we only need to copy
// array x to array y
merge(x, y, l, m, r);
}
return (l == 0) && (m == x.length - 1); // it's true only when the whole
// array is already sorted
} public static void merge(int x[], int y[], int l, int m, int r) {
// merge x[l:m] and x[m+1:r] into y[l:r]
int i = l,
j = m+1,
k = l;
while ((i <= m) && (j <= r))
if (x[i] <= x[j])
y[k++] = x[i++];
else
y[k++] = x[j++];
while (k <= r)
if (i > m) // elements in x[l:m] are all merged into array y[]
y[k++] = x[j++];
else
y[k++] = x[i++]; }
}
After running each program for 3 times, I got the executing time as below:
program in textbook -- 1457ms 1389ms 1359ms
my program -- 1281ms 1172ms 1185ms
In average, my program saves roughly 0.2 second. Though it's not that better, it still makes me exciting!
And through this practice, I came to know there's a lot of fun hacking the algorithm. I'm looking forword to write more beautiful and efficient code!
Natural Merge Sort(自然归并排序)的更多相关文章
- 【算法】归并排序(Merge Sort)(五)
归并排序(Merge Sort) 归并排序是建立在归并操作上的一种有效的排序算法.该算法是采用分治法(Divide and Conquer)的一个非常典型的应用.将已有序的子序列合并,得到完全有序的序 ...
- 连续线性空间排序 起泡排序(bubble sort),归并排序(merge sort)
连续线性空间排序 起泡排序(bubble sort),归并排序(merge sort) 1,起泡排序(bubble sort),大致有三种算法 基本版,全扫描. 提前终止版,如果发现前区里没有发生交换 ...
- [算法]——归并排序(Merge Sort)
归并排序(Merge Sort)与快速排序思想类似:将待排序数据分成两部分,继续将两个子部分进行递归的归并排序:然后将已经有序的两个子部分进行合并,最终完成排序.其时间复杂度与快速排序均为O(nlog ...
- 归并排序(Merge Sort)
归并排序是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用.将已有序的子序列合并,得到完全有序的序列:即先使每个子序列有序,再使子序 ...
- 经典排序算法 - 归并排序Merge sort
经典排序算法 - 归并排序Merge sort 原理,把原始数组分成若干子数组,对每个子数组进行排序, 继续把子数组与子数组合并,合并后仍然有序,直到所有合并完,形成有序的数组 举例 无序数组[6 2 ...
- 排序算法二:归并排序(Merge sort)
归并排序(Merge sort)用到了分治思想,即分-治-合三步,算法平均时间复杂度是O(nlgn). (一)算法实现 private void merge_sort(int[] array, int ...
- 归并排序 ALDS1_5_B:Merge Sort
Merge Sort Write a program of a Merge Sort algorithm implemented by the following pseudocode. You sh ...
- 【高级排序算法】2、归并排序法的实现-Merge Sort
简单记录 - bobo老师的玩转算法系列–玩转算法 -高级排序算法 Merge Sort 归并排序 Java实现归并排序 SortTestHelper 排序测试辅助类 package algo; im ...
- 【高级排序算法】1、归并排序法 - Merge Sort
归并排序法 - Merge Sort 文章目录 归并排序法 - Merge Sort nlogn 比 n^2 快多少? 归并排序设计思想 时间.空间复杂度 归并排序图解 归并排序描述 归并排序小结 参 ...
随机推荐
- Java IO编程全解(一)——Java的I/O演进之路
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7419117.html JDK1.4之前的早期版本,Java对I/O的支持并不完善,开发人员在开发高性能I/O ...
- CSS盒子模型之详解
前言: 盒子模型是css中最核心的基础知识,理解了这个重要的概念才能更好的排版,进行页面布局.一.css盒子模型概念 CSS盒子模型 又称框模型 (Box Model) ,包含了元 ...
- d3力导向图聚焦
效果描述 双击节点,节点以及节点一度关联的节点保持高亮状态,其余节点变灰,半径变小,文字消失,并且向内收缩. 效果展示 正常状态 聚焦效果 关键代码 节点变化 激活节点保持高亮的样式,其余节点应用no ...
- shell编程/字库裁剪(1)
我写这个帖子的意图,在于三个: 1.用代码生成代码的思维. 2.shell编程的思路. 3.裁剪字库的具体程序. 我打算分为三节来说: 第一节讲裁剪裁剪词库的意义以及使用场合: 第二节讲如何用shel ...
- JavaWeb之Maven配置
Maven和C#的nuget类似,可以通过设置就能引入框架等第三方,方便又省事.Java中使用Maven来管理第三方.今天尝试着配置了一下. 一.JDK的安装 关于JDK的安装可以查看百度经验,设置P ...
- linux 守护进程编程
概述: Daemon运行在后台也称作"后台服务进程". 它是没有控制终端与之相连的进程.它独立于控制终端.通常周期的执行某种任务. 守护进程脱离终端是为了避免进程在执行过程中的信息 ...
- VS2017生成解决方案报错,提示对路径的访问被拒绝
目前我用的vs2017的版本是15.3.5.生成解决方案有时会提示如下: 开始以为是权限的问题,找到相应的目录设置everyone权限,再次生成还是不行.重启VS试了下,还是不行. 最后无奈重启下电脑 ...
- 如何透彻分析Java开发人员
第一部分:对于参加工作一年以内的同学.恭喜你,这个时候,你已经拥有了一份Java的工作. 这个阶段是你成长极快的阶段,而且你可能会经常加班.但是加班不代表你就可以松懈了,永远记得我说的那句话,从你入行 ...
- 在Owin Self-Hosing下实现每个请求中共享上下文(数据)
问题 这几天在做公司的外部WebApi网关,由于使用了OAuth2.0,所以不得不使用Owin来部署网关. 而涉及到请求上下文的问题,为了使业务层能获取到请求头的信息,又不与网关耦合,决定把请求信息写 ...
- Java求555 555的约数中最大的三位数。
package org.llh.test; /** * 求555 555的约数中最大的三位数 * @author llh * */ public class Car { //整数j除以整数i(i≠0) ...