4

第 25 题:如何理解归并排序?

 2 years ago
source link: https://segmentfault.com/a/1190000040532601
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

什么是归并排序?

把长度为 n 的输入序列分成两个长度为 n/2 的子序列;

对这两个子序列分别采用归并排序;

将两个排序好的子序列合并成一个最终的排序序列。

<img src="https://noxussj.top:3000/25/1.png"></img>

总的来说就是先拆分,后合并,合并的同时进行排序

归并排序就像一场比武大赛

举个例子,有 A、B、C、D、E、F、G、H 一共 8 个武术家参考参加比武大会。

第一轮,两两一组,有 4 名选手胜出(四分之一决赛)

第二轮,两两一组,有两名选手胜出(半决赛)

第三轮,仅剩的两人一组,冠军胜出(总决赛)

<img src="https://noxussj.top:3000/25/2.png"></img>

<img src="https://noxussj.top:3000/25/3.gif"></img>

参考资料
值得收藏的十大经典排序算法
什么是归并排序?
归并排序详解

文章的内容/灵感都从下方内容中借鉴


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK