归并的核心思想是归并。归并的速度直接影响到算法的快慢。

1. 简单插入归并

  public static class MergeSorter1 implements Sorter {
public void sort(int[] values) {
int step = 1;
while (step <= values.length) {
int index = 0;
while (index < values.length) {
int low, high, end = 0;
if (index + step < values.length) {
low = index;
high = index + step;
if (index + step + step < values.length) {
end = index + step + step;
} else {
end = values.length;
}
merge(values, low, high, end);
index = end;
} else {
break;
}
}
step = 2 * step;
}
}
private void merge(int[] values, int low, int high, int end) {
while (high < end) {
while (low < high) {
if (values[low] <= values[high]) {
low++;
} else {
break;
}
}
if (low != high) {
int temp = values[high];
for (int i = low; i <= high; ++i) {
int _temp = values[i];
values[i] = temp;
temp = _temp;
}
}
high++;
}
}
}

简单插入归并的性能打印:

.... take time: 654

2. 额外建立一个临时数组归并:

    public static class MergeSorter2 implements Sorter {
public void sort(int[] values) {
int step = 1;
while (step <= values.length) {
int index = 0;
while (index < values.length) {
merge(values, index, index + step, index + step + step);
index = index + step + step;
}
step = 2 * step;
}
}
private void merge(int[] values, int low, int high, int end) {
if (low >= values.length) {
return;
}
if (high > values.length) {
high = values.length;
}
if (end > values.length) {
end = values.length;
}
int originLow = low;
int originHigh = high;
int originEnd = end;
int[] tempValues = new int[end - low];
int p = 0;
while (p < tempValues.length) {
if (low < originHigh) {
if (high == originEnd || values[low] <= values[high]) {
tempValues[p] = values[low];
low++;
p++;
}
}
if (high < originEnd) {
if (low == originHigh || values[low] >= values[high]) {
tempValues[p] = values[high];
high++;
p++;
}
}
}
for (int i = 0; i < tempValues.length; ++i) {
values[originLow + i] = tempValues[i];
}
}
}

性能打印:

.... take time: 1520

3. 使用公用临时数组归并

   public static class MergeSorter3 implements Sorter {
public int[] tempValues;
public void sort(int[] values) {
boolean isInTemp = false;
int[] originValues = values;
tempValues = new int[values.length];
int step = 1;
while (step <= values.length) {
int index = 0;
while (index < values.length) {
merge(values, index, index + step, index + step + step);
index = index + step + step;
}
// printValues(tempValues);
int[] _tempValues = tempValues;
tempValues = values;
values = _tempValues;
isInTemp = !isInTemp;
step = 2 * step;
}
if (isInTemp) {
for (int i = 0; i < values.length; ++i) {
originValues[i] = values[i];
}
}
}
private void merge(int[] values, int low, int high, int end) {
if (low >= values.length) {
return;
}
if (high > values.length) {
high = values.length;
}
if (end > values.length) {
end = values.length;
}
int pLow = low;
int pHigh = high;
int pMerged = low;
while (pMerged < end) {
if (pLow < high) {
if (pHigh >= end) {
tempValues[pMerged] = values[pLow];
pLow++;
pMerged++;
} else if (values[pLow] < values[pHigh]) {
tempValues[pMerged] = values[pLow];
pLow++;
pMerged++;
} else if (values[pLow] == values[pHigh]) {
tempValues[pMerged] = values[pLow];
pLow++;
pMerged++;
tempValues[pMerged] = values[pHigh];
pHigh++;
pMerged++;
}
}
if (pHigh < end) {
if (pLow >= high) {
tempValues[pMerged] = values[pHigh];
pHigh++;
pMerged++;
} else if (values[pLow] > values[pHigh]) {
tempValues[pMerged] = values[pHigh];
pHigh++;
pMerged++;
} else if (values[pLow] == values[pHigh]) {
tempValues[pMerged] = values[pLow];
pLow++;
pMerged++;
tempValues[pMerged] = values[pHigh];
pHigh++;
pMerged++;
}
}
}
}
}

时间打印:

.... take time: 784

4. 在算法3基础上进行优化

public static class MergeSorter4 implements Sorter {
public int[] tempValues;
public void sort(int[] values) {
boolean isInTemp = false;
int[] originValues = values;
tempValues = new int[values.length];
int step = 1;
while (step <= values.length) {
int index = 0;
while (index < values.length) {
int  end = index + step + step;
merge(values, index, index + step, end);
index = end;
}
// printValues(tempValues);
int[] _tempValues = tempValues;
tempValues = values;
values = _tempValues;
isInTemp = !isInTemp;
step = 2 * step;
}
if (isInTemp) {
for (int i = 0; i < values.length; ++i) {
originValues[i] = values[i];
}
}
}
private void merge(int[] values, int low, int high, int end) {
if (low >= values.length) {
return;
}
if (high > values.length) {
high = values.length;
}
if (end > values.length) {
end = values.length;
}
int pLow = low;
int pHigh = high;
int pMerged = low;
while (pLow < high && pHigh < end) {
if (values[pLow] <= values[pHigh]) {
tempValues[pMerged++] = values[pLow++];
} else {
tempValues[pMerged++] = values[pHigh++];
}
}
while (pLow < high) {
tempValues[pMerged++] = values[pLow++];
}
while(pHigh< end) {
tempValues[pMerged++] = values[pHigh++];
}
}
}

打印时间:

.... take time: 573

5. Java SDK归并

   public static class SDKSorter implements Sorter {
public void sort(int[] values) {
Arrays.parallelSort(values);
}
}

时间打印:

.... take time: 338

可见android SDK的归并效率很高,值得使用。

[置顶] Android面试题目之四: 归并排序相关推荐

  1. Android面试题目之四: 归并排序

    归并的核心思想是归并.归并的速度直接影响到算法的快慢. 1. 简单插入归并 public static class MergeSorter1 implements Sorter {public voi ...

  2. [置顶] Android面试题目之三: 字符串转整形

    1. 首先写了一个字符串转整形的算法: public static class String2Int1 implements String2Int { @Override public int str ...

  3. [置顶] Android面试题目之二:整形转换为字符串

    整形转换为字符串,写了五种算法,并进行了性能分析: 1. 算法: 直接先求得长度,然后从左向右求得各位的大小 private static class Int2String1 implements I ...

  4. 应该是史上最全最新Java和Android面试题目(自己总结和收集的)

    Android面试题目 Java 基础 int占用几个字节 讲一下常见编码方式? UTF-8编码下中文占几个字节 int和Interger的区别 int.char.long各占多少字节数 string ...

  5. 一个BAT大厂面试者整理的Android面试题目!

    身边好多朋友都裸辞了,出去旅游了一圈之后,回来才发现,工作并没有想象中那么好找.朋友小A一心只想进大厂面试Android,于是面试了阿里巴巴.美团.滴滴等,最后在某个大厂经历了5轮面试后拿到了offe ...

  6. android listview标题置顶,Android仿QQ左滑删除置顶ListView操作

    最近闲来无事,于是研究了一下qq的左滑删除效果,尝试着实现了一下,先上效果图: 大致思路原理: - 通过设置margin实现菜单的显示与隐藏 - 监听onTouchEvent,处理滑动事件 上代码 i ...

  7. 某通信公司的Android面试题目

    某通信公司的Android面试题目 今天的面试感觉做的不是很好,有些知识点明显没有掌握好,现在抽空把面试题目抄下来,同时努力掌握好对应的知识点. stack和heap有什么区别? heap是堆,sta ...

  8. [置顶] Android自定义控件 芝麻信用分雷达图

    [置顶] Android自定义控件 芝麻信用分雷达图 标签: android自定义雷达芝麻信用 2016-10-23 20:11  3548人阅读  评论(24)  收藏  举报   分类: 自定义控 ...

  9. Android面试题目(1-2-3--7)

    网上收集的Android 题目一 1.Activity生命周期说下,出现异常主要在那个阶段处理? 2.数据存储有哪几种方式?说过你用过哪些,做了哪些相关的项目?Sqlite用过说下? 3.Adapte ...

最新文章

  1. 书店POS机--细化迭代1--测试
  2. Golang的基本类型、引用类型、复合类型
  3. 轻舟智航发布Driven-by-QCraft第三代自动驾驶硬件方案
  4. 腾讯成联合国全球合作伙伴,TDSQL如何支撑史上最大规模全球会议
  5. leetcode 907. Sum of Subarray Minimums | 907. 子数组的最小值之和(单调栈)
  6. 使用countup.js使数字动态叠加
  7. 程序员水平自测题:程序员们,想知道你的技术达到了什么水平吗?
  8. 织梦系统MySQL安装_如何更改已安装的织梦dedecms系统数据库表前缀?
  9. 更换介质:请把标有 “Debian GNU/Linux 10.2.0 _Buster_ - Official amd64 DVD Binary-1 20191116-09:57” 的盘片插入驱动器“
  10. 多个客户端共用同一条拉起链接 ,如何配置 Universal Link iOS App
  11. 温故知新:数据科学札记
  12. java面向对象之实现房屋出租系统
  13. GIS空间分析(一)——空间分析与GIS
  14. 5V转24V差分信号转TTL电平脉冲信号隔离变送器
  15. 本人有51SAP培训机构全套SAP培训课程教材和视频, 欲转手
  16. 天刀手游不显示服务器列表,天涯明月刀手游服务器bug解决方法
  17. 高速窄脉冲峰值保持设计
  18. 红孩儿编辑器的模块设计16
  19. vulnhub靶机ME AND MY GIRLFRIEND: 1
  20. 《让时间陪你慢慢变富》有感

热门文章

  1. php 获取对象中的元素个数组长度,php数组长度怎么获取
  2. python写完程序保存_Python学习笔记——文件处理
  3. cin输入字符串怎么结束_翻遍全网,只为让你记住这些输入输出函数
  4. 池州市计算机专科,2021年3月安徽省池州市计算机等级考试时间
  5. android 悬浮窗权限,Android 悬浮窗权限校验
  6. 用java编写运行的小游戏_第一次用Java编写小游戏!
  7. mysql 当前时间减指定时间_MySQL实现当前数据表的所有时间都增加或减少指定的时间间隔(推荐)...
  8. go/golang init()方法的调用
  9. 在android手机上运行PHP
  10. Exchange日常管理之二十二:配置保留策略