//C语言实现

 void mergeSort(int array[],int first, int last)
{
if (first < last)//拆分数列中元素只剩下两个的时候,不再拆分
{
int mid = (first + last) / ;
//递归拆分数组
mergeSort(array, first, mid);
mergeSort(array, mid + , last);
//归并两个数组
merge(array, first, mid, last);
}
} void merge(int array[], int first,int mid,int last)
{
int i = first, j = mid + , k = first;
int temp[last + ]; //从两个数列的第一个开始判断
while (i <= mid && j <= last)
if (array[i] <= array[j])
temp[k ++] = array[i ++];
else
temp[k ++] = array[j ++]; //如果有剩余,补充进入数组
while (i <= mid)
temp[k ++] = array[i ++];
while (j <= last)
temp[k ++] = array[j ++]; //复制数组
while (first <= last)
{
array[first] = temp[first];
first ++;
}
}
 //Objective-C实现
//通过NSMutableArray的Category实现 //.h文件
@interface NSMutableArray (ArraySort) - (void)mergeSort; @end //.m文件 #import "NSMutableArray+ArraySort.h" @implementation NSMutableArray (ArraySort) - (void)mergeSort
{
[self sortByStartIndex: endIndex:self.count - ];
} - (void)sortByStartIndex:(int)start endIndex:(int)end
{
if (start < end)
{
int mid = (start + end) / ;
[self sortByStartIndex:start endIndex:mid];
[self sortByStartIndex:mid + endIndex:end];
[self mergeByStartIndex:start midIndex:mid endIndex:end];
}
} - (void)mergeByStartIndex:(int)start midIndex:(int)mid endIndex:(int)end
{
int i = start,j = mid + ;
NSMutableArray *tempArray = [[NSMutableArray alloc] initWithCapacity:end + ]; while (i <= mid && j <= end)
if ([[self objectAtIndex:i] integerValue] <= [[self objectAtIndex:j] integerValue])
[tempArray addObject:[self objectAtIndex:i ++]];
else
[tempArray addObject:[self objectAtIndex:j ++]]; while (i <= mid)
[tempArray addObject:[self objectAtIndex:i ++]];
while (j <= end)
[tempArray addObject:[self objectAtIndex:j ++]]; for (id object in tempArray)
[self replaceObjectAtIndex:start++ withObject:object];
} @end

归并排序Merge Sort的更多相关文章

  1. 经典排序算法 - 归并排序Merge sort

    经典排序算法 - 归并排序Merge sort 原理,把原始数组分成若干子数组,对每个子数组进行排序, 继续把子数组与子数组合并,合并后仍然有序,直到所有合并完,形成有序的数组 举例 无序数组[6 2 ...

  2. 连续线性空间排序 起泡排序(bubble sort),归并排序(merge sort)

    连续线性空间排序 起泡排序(bubble sort),归并排序(merge sort) 1,起泡排序(bubble sort),大致有三种算法 基本版,全扫描. 提前终止版,如果发现前区里没有发生交换 ...

  3. 排序算法二:归并排序(Merge sort)

    归并排序(Merge sort)用到了分治思想,即分-治-合三步,算法平均时间复杂度是O(nlgn). (一)算法实现 private void merge_sort(int[] array, int ...

  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

    基本思想:参考 归并排序是建立在归并操作上的一种有效的排序算法.该算法是采用分治法的一个非常典型的应用.首先考虑下如何将2个有序数列合并.这个非常简单,只要从比较2个数列的第一个数,谁小就先取谁,取了 ...

  6. 归并排序Merge sort(转)

    原理,把原始数组分成若干子数组,对每一个子数组进行排序, 继续把子数组与子数组合并,合并后仍然有序,直到全部合并完,形成有序的数组 举例 无序数组[6 2 4 1 5 9] 先看一下每个步骤下的状态, ...

  7. 数据结构 - 归并排序(merging sort)

    归并排序(merging sort): 包含2-路归并排序, 把数组拆分成两段, 使用递归, 将两个有序表合成一个新的有序表. 归并排序(merge sort)的时间复杂度是O(nlogn), 实际效 ...

  8. 数据结构 - 归并排序(merging sort) 具体解释 及 代码

    归并排序(merging sort) 具体解释 及 代码 本文地址: http://blog.csdn.net/caroline_wendy 归并排序(merging sort): 包括2-路归并排序 ...

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

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

随机推荐

  1. navicat将多个表导出为一个sql文件

    1.shift选中多个表 2右键选择--转储sql文件---结构和数据

  2. 最常用Python开源框架有哪些?

    Python开源框架有很多,像Django.Flask.webpy等等,但哪些是最常用到的呢?我们收集了一些Python使用者的宝贵意见,把他们认为最常用的Python开源框架简单的介绍给大家. 一. ...

  3. 虚拟主机,VPS,云主机之间的区别?

    虚拟主机即共享主机,是利用虚拟技术把一台完整的服务器分成若干个主机,拥有多个网站,共享这台服务器的硬件和带宽的资源.可以托管简单的静态和动态的网站,满足客户最基本的网络托管需求. VPS是将一台物理服 ...

  4. destoon 默认广告位代码

    <img src="http://www.testinstrument.cn/skin/default/jiurong/img/banner.png" alt="& ...

  5. apache:侧重于http server tomcat:侧重于servlet引擎

    apache:侧重于http server tomcat:侧重于servlet引擎

  6. mysql查询语句处理

    两表做链接查询,   查理处理顺序各个阶段: 1) From: 对From子句中的坐标<left_table>和右表<right_table>执行笛卡尔积,产生虚拟表T1: 2 ...

  7. MVVM探索:从ViewModel关闭Window的最佳实践

    在WPF里使用MVVM开发的时候,似乎总是不可避免的会遇到这样一个问题:ViewModel在处理完业务之后需要关闭这个Window,这时候要怎么处理? 网上有很多解决方案:有的在ViewModel抛出 ...

  8. confirm显示数组中的内容时,总是带一个逗号分隔的解决方法

    问题的关键 就是在给confirm显示之前,将数组转换成字符串,并以每个数组的元素为一个字符串,加上一个换行回车符即可: 代码中的背景色 为关键的点 <script type="tex ...

  9. WebService初入

    WebService 1. WebService基本认识 WebService最早是微软提出了一种以XML为载体网络信息传输的规范,现在几乎所有的语言与平台都支持,带有状态机制,不依赖于容器,可以发送 ...

  10. MySQL中时间函数NOW()和SYSDATE()的区别

    mysql中日期函数还是比较常用的.主要有NOW()和SYSDATE()两种,虽然都表示当前时间,但使用上有一点点区别. NOW()取的是语句开始执行的时间,SYSDATE()取的是动态的实时时间. ...