@ -4284,9 +4284,53 @@
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
< div class = "highlight" > < span class = "filename" > merge_sort.rb< / span > < pre > < span > < / span > < code > < a id = "__codelineno-12-1" name = "__codelineno-12-1" href = "#__codelineno-12-1" > < / a > < span class = "o" > [< / span > < span class = "n" > class< / span > < span class = "o" > ]< / span > < span class = "p" > {}< / span > < span class = "o" > -[< / span > < span class = "n" > func< / span > < span class = "o" > ]< / span > < span class = "p" > {< / span > < span class = "n" > merge< / span > < span class = "p" > }< / span >
< a id = "__codelineno-12-2" name = "__codelineno-12-2" href = "#__codelineno-12-2" > < / a >
< a id = "__codelineno-12-3" name = "__codelineno-12-3" href = "#__codelineno-12-3" > < / a > < span class = "o" > [< / span > < span class = "n" > class< / span > < span class = "o" > ]< / span > < span class = "p" > {}< / span > < span class = "o" > -[< / span > < span class = "n" > func< / span > < span class = "o" > ]< / span > < span class = "p" > {< / span > < span class = "n" > merge_sort< / span > < span class = "p" > }< / span >
< div class = "highlight" > < span class = "filename" > merge_sort.rb< / span > < pre > < span > < / span > < code > < a id = "__codelineno-12-1" name = "__codelineno-12-1" href = "#__codelineno-12-1" > < / a > < span class = "c1" > ### 合并左子数组和右子数组 ###< / span >
< a id = "__codelineno-12-2" name = "__codelineno-12-2" href = "#__codelineno-12-2" > < / a > < span class = "k" > def< / span > < span class = "w" > < / span > < span class = "nf" > merge< / span > < span class = "p" > (< / span > < span class = "n" > nums< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > left< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > mid< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > right< / span > < span class = "p" > )< / span >
< a id = "__codelineno-12-3" name = "__codelineno-12-3" href = "#__codelineno-12-3" > < / a > < span class = "w" > < / span > < span class = "c1" > # 左子数组区间为 [left, mid], 右子数组区间为 [mid+1, right]< / span >
< a id = "__codelineno-12-4" name = "__codelineno-12-4" href = "#__codelineno-12-4" > < / a > < span class = "w" > < / span > < span class = "c1" > # 创建一个临时数组 tmp, 用于存放合并后的结果< / span >
< a id = "__codelineno-12-5" name = "__codelineno-12-5" href = "#__codelineno-12-5" > < / a > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nb" > Array< / span > < span class = "o" > .< / span > < span class = "n" > new< / span > < span class = "p" > (< / span > < span class = "n" > right< / span > < span class = "w" > < / span > < span class = "o" > -< / span > < span class = "w" > < / span > < span class = "n" > left< / span > < span class = "w" > < / span > < span class = "o" > +< / span > < span class = "w" > < / span > < span class = "mi" > 1< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "mi" > 0< / span > < span class = "p" > )< / span >
< a id = "__codelineno-12-6" name = "__codelineno-12-6" href = "#__codelineno-12-6" > < / a > < span class = "w" > < / span > < span class = "c1" > # 初始化左子数组和右子数组的起始索引< / span >
< a id = "__codelineno-12-7" name = "__codelineno-12-7" href = "#__codelineno-12-7" > < / a > < span class = "w" > < / span > < span class = "n" > i< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > j< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > k< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > left< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > mid< / span > < span class = "w" > < / span > < span class = "o" > +< / span > < span class = "w" > < / span > < span class = "mi" > 1< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "mi" > 0< / span >
< a id = "__codelineno-12-8" name = "__codelineno-12-8" href = "#__codelineno-12-8" > < / a > < span class = "w" > < / span > < span class = "c1" > # 当左右子数组都还有元素时,进行比较并将较小的元素复制到临时数组中< / span >
< a id = "__codelineno-12-9" name = "__codelineno-12-9" href = "#__codelineno-12-9" > < / a > < span class = "w" > < / span > < span class = "k" > while< / span > < span class = "w" > < / span > < span class = "n" > i< / span > < span class = "w" > < / span > < span class = "o" > < =< / span > < span class = "w" > < / span > < span class = "n" > mid< / span > < span class = "w" > < / span > < span class = "o" > & & < / span > < span class = "w" > < / span > < span class = "n" > j< / span > < span class = "w" > < / span > < span class = "o" > < =< / span > < span class = "w" > < / span > < span class = "n" > right< / span >
< a id = "__codelineno-12-10" name = "__codelineno-12-10" href = "#__codelineno-12-10" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "n" > nums< / span > < span class = "o" > [< / span > < span class = "n" > i< / span > < span class = "o" > ]< / span > < span class = "w" > < / span > < span class = "o" > < =< / span > < span class = "w" > < / span > < span class = "n" > nums< / span > < span class = "o" > [< / span > < span class = "n" > j< / span > < span class = "o" > ]< / span >
< a id = "__codelineno-12-11" name = "__codelineno-12-11" href = "#__codelineno-12-11" > < / a > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "o" > [< / span > < span class = "n" > k< / span > < span class = "o" > ]< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > nums< / span > < span class = "o" > [< / span > < span class = "n" > i< / span > < span class = "o" > ]< / span >
< a id = "__codelineno-12-12" name = "__codelineno-12-12" href = "#__codelineno-12-12" > < / a > < span class = "w" > < / span > < span class = "n" > i< / span > < span class = "w" > < / span > < span class = "o" > +=< / span > < span class = "w" > < / span > < span class = "mi" > 1< / span >
< a id = "__codelineno-12-13" name = "__codelineno-12-13" href = "#__codelineno-12-13" > < / a > < span class = "w" > < / span > < span class = "k" > else< / span >
< a id = "__codelineno-12-14" name = "__codelineno-12-14" href = "#__codelineno-12-14" > < / a > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "o" > [< / span > < span class = "n" > k< / span > < span class = "o" > ]< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > nums< / span > < span class = "o" > [< / span > < span class = "n" > j< / span > < span class = "o" > ]< / span >
< a id = "__codelineno-12-15" name = "__codelineno-12-15" href = "#__codelineno-12-15" > < / a > < span class = "w" > < / span > < span class = "n" > j< / span > < span class = "w" > < / span > < span class = "o" > +=< / span > < span class = "w" > < / span > < span class = "mi" > 1< / span >
< a id = "__codelineno-12-16" name = "__codelineno-12-16" href = "#__codelineno-12-16" > < / a > < span class = "w" > < / span > < span class = "k" > end< / span >
< a id = "__codelineno-12-17" name = "__codelineno-12-17" href = "#__codelineno-12-17" > < / a > < span class = "w" > < / span > < span class = "n" > k< / span > < span class = "w" > < / span > < span class = "o" > +=< / span > < span class = "w" > < / span > < span class = "mi" > 1< / span >
< a id = "__codelineno-12-18" name = "__codelineno-12-18" href = "#__codelineno-12-18" > < / a > < span class = "w" > < / span > < span class = "k" > end< / span >
< a id = "__codelineno-12-19" name = "__codelineno-12-19" href = "#__codelineno-12-19" > < / a > < span class = "w" > < / span > < span class = "c1" > # 将左子数组和右子数组的剩余元素复制到临时数组中< / span >
< a id = "__codelineno-12-20" name = "__codelineno-12-20" href = "#__codelineno-12-20" > < / a > < span class = "w" > < / span > < span class = "k" > while< / span > < span class = "w" > < / span > < span class = "n" > i< / span > < span class = "w" > < / span > < span class = "o" > < =< / span > < span class = "w" > < / span > < span class = "n" > mid< / span >
< a id = "__codelineno-12-21" name = "__codelineno-12-21" href = "#__codelineno-12-21" > < / a > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "o" > [< / span > < span class = "n" > k< / span > < span class = "o" > ]< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > nums< / span > < span class = "o" > [< / span > < span class = "n" > i< / span > < span class = "o" > ]< / span >
< a id = "__codelineno-12-22" name = "__codelineno-12-22" href = "#__codelineno-12-22" > < / a > < span class = "w" > < / span > < span class = "n" > i< / span > < span class = "w" > < / span > < span class = "o" > +=< / span > < span class = "w" > < / span > < span class = "mi" > 1< / span >
< a id = "__codelineno-12-23" name = "__codelineno-12-23" href = "#__codelineno-12-23" > < / a > < span class = "w" > < / span > < span class = "n" > k< / span > < span class = "w" > < / span > < span class = "o" > +=< / span > < span class = "w" > < / span > < span class = "mi" > 1< / span >
< a id = "__codelineno-12-24" name = "__codelineno-12-24" href = "#__codelineno-12-24" > < / a > < span class = "w" > < / span > < span class = "k" > end< / span >
< a id = "__codelineno-12-25" name = "__codelineno-12-25" href = "#__codelineno-12-25" > < / a > < span class = "w" > < / span > < span class = "k" > while< / span > < span class = "w" > < / span > < span class = "n" > j< / span > < span class = "w" > < / span > < span class = "o" > < =< / span > < span class = "w" > < / span > < span class = "n" > right< / span >
< a id = "__codelineno-12-26" name = "__codelineno-12-26" href = "#__codelineno-12-26" > < / a > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "o" > [< / span > < span class = "n" > k< / span > < span class = "o" > ]< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > nums< / span > < span class = "o" > [< / span > < span class = "n" > j< / span > < span class = "o" > ]< / span >
< a id = "__codelineno-12-27" name = "__codelineno-12-27" href = "#__codelineno-12-27" > < / a > < span class = "w" > < / span > < span class = "n" > j< / span > < span class = "w" > < / span > < span class = "o" > +=< / span > < span class = "w" > < / span > < span class = "mi" > 1< / span >
< a id = "__codelineno-12-28" name = "__codelineno-12-28" href = "#__codelineno-12-28" > < / a > < span class = "w" > < / span > < span class = "n" > k< / span > < span class = "w" > < / span > < span class = "o" > +=< / span > < span class = "w" > < / span > < span class = "mi" > 1< / span >
< a id = "__codelineno-12-29" name = "__codelineno-12-29" href = "#__codelineno-12-29" > < / a > < span class = "w" > < / span > < span class = "k" > end< / span >
< a id = "__codelineno-12-30" name = "__codelineno-12-30" href = "#__codelineno-12-30" > < / a > < span class = "w" > < / span > < span class = "c1" > # 将临时数组 tmp 中的元素复制回原数组 nums 的对应区间< / span >
< a id = "__codelineno-12-31" name = "__codelineno-12-31" href = "#__codelineno-12-31" > < / a > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "mi" > 0< / span > < span class = "o" > ...< / span > < span class = "n" > tmp< / span > < span class = "o" > .< / span > < span class = "n" > length< / span > < span class = "p" > )< / span > < span class = "o" > .< / span > < span class = "n" > each< / span > < span class = "w" > < / span > < span class = "k" > do< / span > < span class = "w" > < / span > < span class = "o" > |< / span > < span class = "n" > k< / span > < span class = "o" > |< / span >
< a id = "__codelineno-12-32" name = "__codelineno-12-32" href = "#__codelineno-12-32" > < / a > < span class = "w" > < / span > < span class = "n" > nums< / span > < span class = "o" > [< / span > < span class = "n" > left< / span > < span class = "w" > < / span > < span class = "o" > +< / span > < span class = "w" > < / span > < span class = "n" > k< / span > < span class = "o" > ]< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "o" > [< / span > < span class = "n" > k< / span > < span class = "o" > ]< / span >
< a id = "__codelineno-12-33" name = "__codelineno-12-33" href = "#__codelineno-12-33" > < / a > < span class = "w" > < / span > < span class = "k" > end< / span >
< a id = "__codelineno-12-34" name = "__codelineno-12-34" href = "#__codelineno-12-34" > < / a > < span class = "k" > end< / span >
< a id = "__codelineno-12-35" name = "__codelineno-12-35" href = "#__codelineno-12-35" > < / a >
< a id = "__codelineno-12-36" name = "__codelineno-12-36" href = "#__codelineno-12-36" > < / a > < span class = "c1" > ### 归并排序 ###< / span >
< a id = "__codelineno-12-37" name = "__codelineno-12-37" href = "#__codelineno-12-37" > < / a > < span class = "k" > def< / span > < span class = "w" > < / span > < span class = "nf" > merge_sort< / span > < span class = "p" > (< / span > < span class = "n" > nums< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > left< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > right< / span > < span class = "p" > )< / span >
< a id = "__codelineno-12-38" name = "__codelineno-12-38" href = "#__codelineno-12-38" > < / a > < span class = "w" > < / span > < span class = "c1" > # 终止条件< / span >
< a id = "__codelineno-12-39" name = "__codelineno-12-39" href = "#__codelineno-12-39" > < / a > < span class = "w" > < / span > < span class = "c1" > # 当子数组长度为 1 时终止递归< / span >
< a id = "__codelineno-12-40" name = "__codelineno-12-40" href = "#__codelineno-12-40" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "n" > left< / span > < span class = "w" > < / span > < span class = "o" > > =< / span > < span class = "w" > < / span > < span class = "n" > right< / span >
< a id = "__codelineno-12-41" name = "__codelineno-12-41" href = "#__codelineno-12-41" > < / a > < span class = "w" > < / span > < span class = "c1" > # 划分阶段< / span >
< a id = "__codelineno-12-42" name = "__codelineno-12-42" href = "#__codelineno-12-42" > < / a > < span class = "w" > < / span > < span class = "n" > mid< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > left< / span > < span class = "w" > < / span > < span class = "o" > +< / span > < span class = "w" > < / span > < span class = "n" > right< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "o" > /< / span > < span class = "w" > < / span > < span class = "mi" > 2< / span > < span class = "w" > < / span > < span class = "c1" > # 计算中点< / span >
< a id = "__codelineno-12-43" name = "__codelineno-12-43" href = "#__codelineno-12-43" > < / a > < span class = "w" > < / span > < span class = "n" > merge_sort< / span > < span class = "p" > (< / span > < span class = "n" > nums< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > left< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > mid< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "c1" > # 递归左子数组< / span >
< a id = "__codelineno-12-44" name = "__codelineno-12-44" href = "#__codelineno-12-44" > < / a > < span class = "w" > < / span > < span class = "n" > merge_sort< / span > < span class = "p" > (< / span > < span class = "n" > nums< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > mid< / span > < span class = "w" > < / span > < span class = "o" > +< / span > < span class = "w" > < / span > < span class = "mi" > 1< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > right< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "c1" > # 递归右子数组< / span >
< a id = "__codelineno-12-45" name = "__codelineno-12-45" href = "#__codelineno-12-45" > < / a > < span class = "w" > < / span > < span class = "c1" > # 合并阶段< / span >
< a id = "__codelineno-12-46" name = "__codelineno-12-46" href = "#__codelineno-12-46" > < / a > < span class = "w" > < / span > < span class = "n" > merge< / span > < span class = "p" > (< / span > < span class = "n" > nums< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > left< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > mid< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > right< / span > < span class = "p" > )< / span >
< a id = "__codelineno-12-47" name = "__codelineno-12-47" href = "#__codelineno-12-47" > < / a > < span class = "k" > end< / span >
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >