今天上班没有什么事情需要处理,昨天刚好看在本书上看到归并排序就试着将其代码写了出来。尝试着进行一下分析!感觉从算法的描述到具体的代码,中间有不少细节需要注意。不如到底是小于还是小于等于,是该加一还是减一等等。怪不得有些公司面试时要求进行机试,其实这也是为了考察一个人的动手能了和对细节的处理能力!其实我也不喜欢机试,更别说是有些变态的让直接在纸上写代码的了。。。。
归并排序算法的概念:
归并排序其实是算法里边用的比较多的算法,是“分而治之思想
”的典型应用。其基本思想是,将一些已排序的子文件进行合并得到一个完整的有序文件。归并是只要比较各子文件记录的关键码,其最小者就是全局最小者(相对于参与比较的字文件);取出它后继续比较各子文件的第一个记录....如此下去就能完成排序任务。
实现代码(经过测试):
class MergeSorter
public #public methods
#Appling Top-To-Down method to sort array:
def merger_sort_for_array(source_array,start_index,end_index)
middle=0
if start_index< end_index
middle=(start_index+end_index)/2
merger_sort_for_array(source_array,start_index,middle)
merger_sort_for_array(source_array,middle+1,end_index)
merge_sort(source_array,start_index,middle,end_index)
end
end
#Apping Down-To-Top method to sort array:
def merger_sort_for_array2(source_array,end_index)
length=1
while length<end_index do
merge_array(source_array,length,end_index)
length *=2
end
end
private #Priavate methods
def merge_sort(source_array, start_index, middle_index,end_index)
first_begin=start_index
second_begin=middle_index+1
target_begin=0
target_array=Array.new(end_index-start_index+1)
#Based on the value copy the elements to temprary in sorted order.
while first_begin<=middle_index and second_begin<=end_index
if source_array[first_begin] > source_array[second_begin]
target_array[target_begin]=source_array[second_begin]
target_begin =target_begin + 1
second_begin =second_begin + 1
else
target_array[target_begin]=source_array[first_begin]
target_begin =target_begin + 1
first_begin =first_begin + 1
end
end
#Copy the left part to temprary array if there are element left.
while first_begin<=middle_index do
target_array[target_begin]=source_array[first_begin]
first_begin =first_begin + 1
target_begin +=1
end
#Copy the right part elemnnts to temprary if there are element left.
while second_begin<= end_index do
target_array[target_begin]=source_array[second_begin]
second_begin =second_begin + 1
target_begin +=1
end
#Copy the result back to the source array
i=start_index
p=0
while i<=end_index do
source_array[i]=target_array[p]
p +=1
i +=1
end
end
#Do a single trip sorting
def merge_array(source_array,length,end_index)
start_index=0
while( start_index+2*length-1<=end_index) do
merge_sort(source_array,start_index,start_index+length-1,start_index+2*length-1)
start_index +=2*length
end
#There are tow data parts left ,and the last parts' length is less than length.
if (start_index+length-1)<end_index
merge_sort(source_array,start_index,start_index+length-1,end_index)
end
end
end
#Test code:
sorter=MergeSorter.new()
source_array=[1,3,2,5,7,6,2,8,4,-6,12]
source_array2=[1,3,2,5,7,6,2,8,4,-6,12]
sorter.merger_sort_for_array2(source_array,10)
puts source_array
sorter.merger_sort_for_array(source_array2 , 0 , 10)
puts source_array2
使用预定义数组的实现:
该实现的时间开销为n*log(n) ,空间开销为O(n)既辅助数组的大小。
#Merge Sort
class Sorter
def do_merge_sort(source_array,start_index,middle,end_index,target_array)
first_index=start_index
second_index=middle+1
position=start_index
while first_index<=middle and second_index<=end_index do
if source_array[first_index] <source_array[second_index]
target_array[position]=source_array[first_index]
first_index =first_index+1
else
target_array[position]=source_array[second_index]
second_index =second_index+1
end
position =position+1
end
while first_index<=middle do
target_array[position]=source_array[first_index]
position +=1
first_index +=1
end
while second_index<=end_index do
target_array[position] =source_array[second_index]
position +=1
second_index +=1
end
end
def merge_sort(source_array,start_index,end_index,target_array)
if start_index==end_index
target_array[start_index]=source_array[end_index]
else
middle=(start_index+end_index)/2
merge_sort(source_array,start_index,middle,target_array)
merge_sort(source_array,middle+1,end_index,target_array)
#First sort the target_array
do_merge_sort(source_array,start_index,middle,end_index,target_array)
#Then copy the sorted target_array to source_array
do_merge_sort(target_array,start_index,middle,end_index,source_array)
end
end
end
source=[1,3,2,4,7,2,0,-2]
target=Array.new(8,0)
sorter=Sorter.new()
sorter.merge_sort(source,0,7,target)
print source
puts ""
print target
算法分析和讨论:
本实现使用了同一种思想的两种不同的实现,一个是自顶而下,一个是自底而上的。两种算法的时间复杂度都是n*Log(n).空间复杂度也是n*Log(n),因为每一趟的排序过程中要建立临时的数组,其空间大小基本上和待排序数组一致。
还有另外其他的一些实现方式,比如可以预先创建一个辅助数组,大小和待排序的数组一样大。通过该辅助数组可以降低算法的空间复杂度,既O(n).但是这会使的算法的可理解性下降。所以在实际应用时,根据具体的需要选择不同的算法实现。一般情况下对算法的可理解行要求不该,可以使用预创建的辅助数组降低空间开销,但是如果程序对空间开销要求不高,建议使用可理解行高的算法实现。
总结:
一种算法思想可以有不同的实现,实现之间的空间开销和时间开销个不相同。因此我们要根据需要仔细选择算法实现。
分享到:
相关推荐
归并排序算法,有程序和复杂性分析,还有解释,挺清楚的,很有用
分治法实现归并排序算法算法设计与分析实验报告(word文档良心出品).docx分治法实现归并排序算法算法设计与分析实验报告(word文档良心出品).docx分治法实现归并排序算法算法设计与分析实验报告(word文档良心出品)....
快速排序、归并排序、基数排序等排序算法比较,比较时间性能,采用C++语言实现。。。
根号n段归并排序算法时间复杂度分析过程: 1.合并 根号n向下取整 段子数组使用的是自底向上两两归并的策略 2.根号n段归并排序算法时间复杂度的数学推导
MATLAB实现《算法设计与分析》中的插入排序、二分归并排序、归并排序实验,其中包括.m文件和实验报告,安徽大学本科课程。
算法设计实验报告,包括:快速排序和归并排序两种算法各自的基本思想、时间复杂度分析,C++实现代码,两种算法运行时间的比较,运行截图,实验心得。
一个算法设计与分析的实验报告,比较归并排序与快速排序的时间差异,这里采用在一个java程序中对随机生成的任意个数分别进行两种方法的排序并记录各自的时间,最后得出结论。 本实验报告附代码以及详细解释
此为一个利用Java语言编写的排序分析程序,程序中统计了各种排序算法(冒泡排序、选择排序、插入排序、希尔排序、快速排序、堆排序、归并排序、基数排序)的分析,ppt中包含各种排序算法的分析,附上动画演示(来自...
试通过随机数据比较归并排序、基数排序各算法的关键字比较次数和关键字移动次数。 (1)待排序表的表长不小于100;其中的数据要用伪随机数产生程序产生;至少要用5组不同的输入数据作比较;比较的指标为有。关键字...
实现以下常用的内部排序算法并进行性能比较:"直接插入排序"," 折半插入排序"," 2—路插入排序"," 表插入排序"," 希尔排序"," 起泡排序"," 快速排序"," 简单选择排序"," 树形选择排序"," 堆排序"," 归并排序"," 链式...
C/C++语言实现归并排序算法,递归调用,算法设计与分析。
[7.6.1]--506归并排序算法及分析.srt
[7.6.1]--506归并排序算法及分析.mp4
排序(Sorting) 是计算机程序设计中的一种重要操作,它的功能是将一个数据元素(或记录)的...本文主要介绍快速排序算法和归并排序算法的基本概念、原理以及具体的实现方法,并对这两种排序算法的时间复杂度进行分析。
计算机算法设计与分析之归并算法 C++完整代码实现归并排序
。
讨论超立方体结构上的并行归并排序算法, 着重分析算 法的通信复杂性, 在此基拙上推导算法的加速比
利用随机函数产生30000个随机整数,利用插入排序、起泡排序、选择排序、快速排序、堆排序、归并排序等排序方法进行排序,并且 (1) 统计每一种排序上机所花费的时间。 (2) 统计在完全正序,完全逆序情况下记录的比较...
用java对常用排序算法进行分析与实现.包含: 插入排序 直接插入排序、希尔排序 • 选择排序 简单选择排序、堆排序 • 交换排序 冒泡排序、快速排序 • 归并排序 • 基数排序
针对传统排序算法计算耗时、实时性差的缺点,提出一种可并行的多层次归并排序算法并在FPGA中实现了其并行计算,同时分析了其周期精确的计算时间。结果表明该归并排序算法可以[O(N)]的时间复杂度完成特征点的排序,...