归并排序Merge Sort
//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的更多相关文章
- 经典排序算法 - 归并排序Merge sort
经典排序算法 - 归并排序Merge sort 原理,把原始数组分成若干子数组,对每个子数组进行排序, 继续把子数组与子数组合并,合并后仍然有序,直到所有合并完,形成有序的数组 举例 无序数组[6 2 ...
- 连续线性空间排序 起泡排序(bubble sort),归并排序(merge sort)
连续线性空间排序 起泡排序(bubble sort),归并排序(merge sort) 1,起泡排序(bubble sort),大致有三种算法 基本版,全扫描. 提前终止版,如果发现前区里没有发生交换 ...
- 排序算法二:归并排序(Merge sort)
归并排序(Merge sort)用到了分治思想,即分-治-合三步,算法平均时间复杂度是O(nlgn). (一)算法实现 private void merge_sort(int[] array, int ...
- 归并排序(merge sort)
M erge sort is based on the divide-and-conquer paradigm. Its worst-case running time has a lower ord ...
- 归并排序——Merge Sort
基本思想:参考 归并排序是建立在归并操作上的一种有效的排序算法.该算法是采用分治法的一个非常典型的应用.首先考虑下如何将2个有序数列合并.这个非常简单,只要从比较2个数列的第一个数,谁小就先取谁,取了 ...
- 归并排序Merge sort(转)
原理,把原始数组分成若干子数组,对每一个子数组进行排序, 继续把子数组与子数组合并,合并后仍然有序,直到全部合并完,形成有序的数组 举例 无序数组[6 2 4 1 5 9] 先看一下每个步骤下的状态, ...
- 数据结构 - 归并排序(merging sort)
归并排序(merging sort): 包含2-路归并排序, 把数组拆分成两段, 使用递归, 将两个有序表合成一个新的有序表. 归并排序(merge sort)的时间复杂度是O(nlogn), 实际效 ...
- 数据结构 - 归并排序(merging sort) 具体解释 及 代码
归并排序(merging sort) 具体解释 及 代码 本文地址: http://blog.csdn.net/caroline_wendy 归并排序(merging sort): 包括2-路归并排序 ...
- [算法]——归并排序(Merge Sort)
归并排序(Merge Sort)与快速排序思想类似:将待排序数据分成两部分,继续将两个子部分进行递归的归并排序:然后将已经有序的两个子部分进行合并,最终完成排序.其时间复杂度与快速排序均为O(nlog ...
随机推荐
- TP传输的两种模式
主动模式(active): 我们知道,FTP是由TCP封包的模式连接,TCP 这种封包由于需要经过 Server 端与 Client 端两边的『三次握手』之后,才能确定联机,也就是需要执行AC ...
- 人人都是CEO
在这个互联网崛起的时代有些流行说法,比如:人人都是产品经理,人人都是程序员以突显行业繁荣的特点,但从更基本的出发点,难道人人不都是 CEO 么?个人的 CEO. 从这个名字套路出发,我沿着想了下去,作 ...
- CCF系列之模板生成系统( 201509-3 )
试题名称: 模板生成系统 试题编号: 201509-3 时间限制: 1.0s 内存限制: 256.0MB 问题描述 成成最近在搭建一个网站,其中一些页面的部分内容来自数据库中不同的数据记录,但是页面的 ...
- mybatis_helloworld(2)_源码
摘录自:http://blog.csdn.net/y172158950/article/details/16982123 在helloworld(1)中,分析了insert一条数据的流程,现在分析下源 ...
- linux ssh登录的小知识
查看服务器的各个端口: # netstat -tulnp 或者 #netstat -tnip 筛选在后面添加 |grep *** 准许root登录 #vi /etc/ssh/sshd_config 找 ...
- Springboot中使用AOP统一处理Web请求日志
title: Springboot中使用AOP统一处理Web请求日志 date: 2017-04-26 16:30:48 tags: ['Spring Boot','AOP'] categories: ...
- springMVC注解方式+easyUI+MYSQL配置实例
刚接触springMVC,使用的注解方式,也在学习阶段,所以把自己学习到的记下来.本文利用springMVC从数据库读取用户信息为例,分享一下. 1.准备相关架包及资源.因为使用springMVC+e ...
- jsp页面取值
一般就用el表达式 ${recordList[4].baseRate8.split("/")[0] } <s:date name="recordList[#id]. ...
- Apache Shiro java安全框架
什么是Apache Shiro? Apache Shiro(发音为“shee-roh”,日语“堡垒(Castle)”的意思)是一个强大易用的Java安全框架,提供了认证.授权.加密和会话管理功能,可为 ...
- CURL处理POST、GET请求
Curl是一个库,它允许你通过各种协议和各种不同的服务器进行连接和通讯 a.php <?php function curlRequest($url,$data=''){ $ch=curl_ini ...