//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. Web API (四) 特性路由(Attribute Route)

    特性路由 是Web API 2 中提出的一种新的类型的路由,正如其名称那样,它是通过特性(Attribute) 来定义路由的,相比之前的基于模式(Convertion Based)的路由,特性路由 能 ...

  2. Redis的事务功能详解

    Redis的事务功能详解 MULTI.EXEC.DISCARD和WATCH命令是Redis事务功能的基础.Redis事务允许在一次单独的步骤中执行一组命令,并且可以保证如下两个重要事项: >Re ...

  3. struts异常:No result defined for action

    问题描述: No result defined for action com.freedom.funitureCityPSIMS.controller.login.CheckAction and re ...

  4. Linuxc - 通过管道,让小程序更有活力

    通过管道,让小程序更有活力 root@jiqing:~/cspace/les6# ls avg.c avg.out input.c input.out 一个负责输入,一个负责统计平均值 avg.c # ...

  5. javascript函数之arguments

    function foo(x,y,z){ console.info (arguments.length); //2 实际的参数个数 console.info(arguments[0]); //传入的第 ...

  6. Steeze框架之入门使用

    一.介绍 steeze是一个优雅.简洁而又高效的PHP开源框架,在整合了知名框架ThinkPHP和Laravel优点的同时,重写了底层架构,增强了功能实现.支持swoole模型运行,支持容器.模型.依 ...

  7. scrapy_数据收集

    什么是数据收集器? 数据以key/value形式存在,收集一些状态,简化数据收集的状态 计算到底发送了多少request等等统计信息 如何对404页面进行设置? 通过response.status等于 ...

  8. 记录一次tomcat下项目没有加载成功

    哥们儿实在太low了,web.xml文件中加载的spring mybatis配置文件和配置的文件不是同一个文件名导致的!

  9. 绕过js验证

    我在火狐和谷歌下想删除对应js,由于是外部js引入的,没删掉.只好借用了工具. 这个工具也并不是多么的高大上,也许大家都用过,httprequester 步骤:打开火狐附加组件管理器——扩展——输入h ...

  10. AQS 框架之 LockSupport 线程阻塞工具类

    ■ 前言 并发包一直是 JDK 里面比较难理解的,同时也是很精美的语言,膜拜下 Doug Li 大神.作者不敢长篇大论,只求循序渐进地把并发包通过理论和实战 (代码) 的方式介绍给大家. 其实做每一件 ...