归并排序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 ...
随机推荐
- 邓_ Jqery·笔记本【照片】
-------------------------------------------------------------------------------------------- [PHP] - ...
- MyBatis之基于XML的动态SQL
先说下我的梦想,大学的时候一直想着是能开店卖胡辣汤,到目前依然还是我的梦想,上周一家出版社联系我问我有没有时间可以合作出书,这也是我的梦想之一,想了想还是放弃了,至少觉得目前不行,毕竟工作还不到五年, ...
- jquery 循环获取checkBox的值,以及对复选框选中,取消,操作按钮
法一:jquery 循环获取选中checkBox框的值 function checkeds() { $("input:checkbox").each(function(index) ...
- 理解rem实现响应式布局原理及js动态计算rem
前言 移动端布局中,童鞋们会使用到rem作为css单位进行不同手机屏幕大小上的适配.那么来讲讲rem在其中起的作用和如何动态设置rem的值. 1.什么是rem rem是相对于根元素(html标签)的字 ...
- 或许你不知道的10条SQL技巧
一.一些常见的SQL实践 (1)负向条件查询不能使用索引 select * from order where status!=0 and stauts!=1 not in/not exists都不是好 ...
- Linux修改主机名脚本-不重启-支持RedHat、SUSE
需要用脚本修改主机名,涉及RedHat.SUSE系统,并且要求修改立即生效且不重启,下面就是我的脚本. 使用脚本的方法如下: 1 首先创建一个脚本文件,假如命名为ModifyHostname.sh: ...
- 从LINQ开始之LINQ to Objects(上)
LINQ概述 LINQ,语言集成查询(Language Integrated Query),它允许使用C#或VB代码以查询数据库相同的方式来操作不同的数据源. LINQ体系结构 从上图可以看出,LIN ...
- Java数据结构和算法(十四)——堆
在Java数据结构和算法(五)——队列中我们介绍了优先级队列,优先级队列是一种抽象数据类型(ADT),它提供了删除最大(或最小)关键字值的数据项的方法,插入数据项的方法,优先级队列可以用有序数组来实现 ...
- 无法远程连接SQLSERVER2000的解决方法
有时候客户端和服务器不在同一个局域网里面,这时候很可能无法直接使用服务器名称来标识该服务器,这时候我们可以使用HOSTS文件来进行名字解析,具体的方法是: 1.使用记事本打开HOSTS文件(一般情况下 ...
- Linux命令:useradd
Linux下:useradd 等价于 adduser Aix下:useradd 来自为知笔记(Wiz)