Algorithm 101----merge sort
2014-05-13 20:18
316 查看
Although I met this topic quite a long ago, I never did such a solid thing---- write a piece of merge-sort code with my own hands! Really thanks the MOOC course "Algorithm part1" offered by Standford, I was motivated again to have a hands-on experience in
this topic !
First of all, let me remember how merge-sort works. Naturally people would implement this by using the divide-conquer strategy.
First of all, let me say what a merge step is:
Given two input array a[] and b[],
then combine the two arrays into a larger one c[], here is the psuedocode for merge step.
for i=1:1:length(c)
if a[j]>b[k]
c[i]=b[k];
k++
else
c[i]=a[j];
j++
end
During the implementation, we divide the whole problem into the smallest problems, each with a size 1, then merge the smallest problems into a bigger sorted one, then merger the bigger ones into further bigger ones, repeat this process again and again until
we arrive the original problem.
here is the full-version psuedocode for merge sort:
function mergesort ( intput array)
if intput array's size==1
then return input array
else
temp array1=mergesort( the first half of input array);
temp array2= mergesort(the second half of input array);
return merge(temp array 1, temp array 2);
remember each merge process's cost is 6*input size. the cost of merge sort is O(nlogn). it beats most of sorting algorithm off the shelves !
this topic !
First of all, let me remember how merge-sort works. Naturally people would implement this by using the divide-conquer strategy.
First of all, let me say what a merge step is:
Given two input array a[] and b[],
then combine the two arrays into a larger one c[], here is the psuedocode for merge step.
for i=1:1:length(c)
if a[j]>b[k]
c[i]=b[k];
k++
else
c[i]=a[j];
j++
end
During the implementation, we divide the whole problem into the smallest problems, each with a size 1, then merge the smallest problems into a bigger sorted one, then merger the bigger ones into further bigger ones, repeat this process again and again until
we arrive the original problem.
here is the full-version psuedocode for merge sort:
function mergesort ( intput array)
if intput array's size==1
then return input array
else
temp array1=mergesort( the first half of input array);
temp array2= mergesort(the second half of input array);
return merge(temp array 1, temp array 2);
remember each merge process's cost is 6*input size. the cost of merge sort is O(nlogn). it beats most of sorting algorithm off the shelves !
相关文章推荐
- Introduction to Algorithm - Summary of Chapter 2(2) - Merge Sort
- Algorithm Intro - Merge Sort
- Algorithm Part I:Merge Sort
- Algorithm 101----QUick Sort
- ssh client 报 algorithm negotiation failed的解决方法
- 欧几里德与扩展欧几里德算法 Extended Euclidean algorithm
- The 101 Most Useful Websites
- UVA - 101 The Blocks Problem
- 输出101~200内的质数
- SGU101 欧拉回路 弱智水题
- LaTeX技巧630:用LaTeX寫algorithm
- 80老翁谈人生(101):无可奉告pk必须奉告
- 加载web view的时候 Domain=WebKitErrorDomain Code=101 “The operation couldn’t be completed. (WebKitErrorDo
- 101、使用ContentProvider在应用间传递数据
- SQL 101 关键字 语句 注释
- C++ sgi STL学习笔记之non-mutating algorithm
- C++ STL学习——algorithm
- freeCodeCamp-Basic Algorithm Scripting
- 连接 insance 到 vlan101 - 每天5分钟玩转 OpenStack(97)
- 判断101到200之间有多少个素数并将其打印出来