合并两个有序序列

转自: http://blog.youkuaiyun.com/whinah/article/details/6610622


经典写法

合并两个有序序列,太简单了吧?还有专门讨论的必要吗?

这是一个最简单的 Merge 版本:

  1. template<class InputIter1, class InputIter2, class OutputIter>  
  2. void merge(InputIter1 first1, InputIter1 last1,  
  3.            InputIter2 first2, InputIter2 last2, OutputIter output) {  
  4.     for (; first1 != last1 && first2 != last2; ++output) {  
  5.         if (*first2 < *first1)  
  6.             *output = *first2, ++first2;  
  7.         else  
  8.             *output = *first1, ++first1;  
  9.     }  
  10.     for (; first1 != last1; ++first1, ++output)  
  11.         *output = *first1;  
  12.     for (; first2 != last2; ++first2, ++output)  
  13.         *output = *first2;  
  14. }  

看似简单?为什么循环里面判断是:
  1. if (*first2 < *first1)  
  2.     *output = *first2, ++first2;  
  3. else  
  4.     *output = *first1, ++first1;  

而不是:

  1. if (*first1 < *first2)  
  2.     *output = *first1, ++first1;  
  3. else  
  4.     *output = *first2, ++first2;  

答案:排序的稳定性,有序序列可能存在相等的元素,对于相等的元素,在 output 中,前者可以保证 seq1 的实例在 seq2 中的之前,而后者反了过来,虽说仍然是确定的,但不符合人的直觉。

为什么要两个不同类的 InputIter ? 只是付出了很小的努力,我们就达到了 merge 不同 iterator 的效用(注意,是效用,不是目标!),从理论上讲,InputIter1, InputIter2, OutputIter 完全可以是 不同的 iterator,并且,他们所指向的元素类型也可以不同,只要在相应的操作符下兼容即可。

应用

如此,最正常的应用莫过于于 merge_sort  了:

  1. template<class RandInputIter, class OutputIter>  
  2. OutputIter  
  3. merge_sort_loop(RandInputIter first, RandInputIter last, OutputIter output) {  
  4.     ptrdiff_t len = last - first;  
  5.     if (len <= 1) {  
  6.         if (len == 1)  
  7.             *output = *first, ++output;  
  8.         return output;  
  9.     }  
  10.     else {  
  11.         ptrdiff_t mid = len / 2;  
  12.         OutputIter lasto1 = merge_sort_loop(first      , first + mid, output);  
  13.         OutputIter lasto2 = merge_sort_loop(first + mid, last       , lasto1);  
  14.         merge(output, lasto1, lasto1, lasto2, first);  
  15.         std::copy(first, last, output);  
  16.         return lasto2;  
  17.     }  
  18. }  
  19.   
  20. template<class RandIter>  
  21. void merge_sort(RandIter first, RandIter last) {  
  22.     typedef typename std::iterator_traits<RandIter>::value_type val_t;  
  23.     boost::scoped_array<val_t> tmp(new val_t[last - first]); // exception safe  
  24.     merge_sort_loop(first, last, tmp.get());  
  25. }  

在 merge_sort_loop 中,output  不必是 RandomAccessIterator !(stl 里面有 stable_sort/inplace_merge,效率和内存使用比这里的 merge_sort 都要低,生产系统中还是应该用 stl 。)

这里说的只是经典的 merge 写法,有没有其它写法呢?只要仔细想,肯定有!

另一种写法


  1. template<class InputIter1, class InputIter2, class OutputIter>  
  2. OutputIter  
  3. merge2(InputIter1 first1, InputIter1 last1,  
  4.        InputIter2 first2, InputIter2 last2, OutputIter output) {  
  5.     for (; first1 != last1 && first2 != last2; ++output) {  
  6.         if (*first2 < *first1) {  
  7.             do { // why use do .. while ?  
  8.                 *output = *first2;  
  9.                  ++output; ++first2;  
  10.             } while (first2 != last2 && *first2 < *first1);  
  11.             *output = *first1; ++first1;  
  12.         } else {  
  13.             do { // why use do .. while ?  
  14.                 *output = *first1;  
  15.                  ++output; ++first1;  
  16.             } while (first1 != last1 && !(*first2 < *first1)); // Important  
  17.             *output = *first2, ++first2;  
  18.         }  
  19.     }  
  20.     for (; first1 != last1; ++first1, ++output)  
  21.         *output = *first1;  
  22.     for (; first2 != last2; ++first2, ++output)  
  23.         *output = *first2;  
  24.     return output;  
  25. }  

注意,为了实现稳定性,Important 行的条件是:
  1. while (first1 != last1 && !(*first2 < *first1))  
而非
  1. while (first1 != last1 && *first1 < *first2)  


这种写法有什么优势呢?搞那么复杂?

这里有一道题目:原地合并两个单向链表,返回新的表头。

简单生硬地套用经典写法:

  1. Node* mergelist(Node* a, Node* b) {  
  2.     if (NULL == a) return b;  
  3.     if (NULL == b) return a;  
  4.     Node* c = b->key < a->key ? b : a; // note:stablize  
  5.     do { // why use do .. while ?  
  6.         if (b->key < a->key) { // note:stablize  
  7.             Node* tmp = b->next;  
  8.             b->next = a;  
  9.             b = tmp;  
  10.         } else {  
  11.             Node* tmp = a->next;  
  12.             a->next = b;  
  13.             a = tmp;  
  14.         }  
  15.     } while (a && b);  
  16.     return c;  
  17. }  
有问题吗?具体啥问题,慢慢找,不难找!

套用新写法:

  1. Node* mergelist2(Node* a, Node* b) {  
  2.     if (NULL == a) return b;  
  3.     if (NULL == b) return a;  
  4.     Node* c = b->key < a->key ? b : a; // note:stablize  
  5.     do { // why use do .. while ?  
  6.         Node* prev;  
  7.         if (b->key < a->key) { // note:stablize  
  8.             do {          
  9.                 prev = b;         
  10.                 b = b->next;      
  11.             } while (b && b->key < a->key); // stablize   
  12.             prev->next = a;  
  13.         } else {  
  14.             do {          
  15.                 prev = a;         
  16.                 a = a->next;      
  17.             } while (a && !(b->key < a->key)); // stablize   
  18.             prev->next = b;  
  19.         }         
  20.     } while (a && b);   
  21.     return c;  
  22. }  


其它(可能的困惑)


!(y<x)

可以看到,程序中很多地方使用了 ! (y < x) 的写法,而非 x <= y,为什么?

一方面,stl 对 Comparable 的最小定义是 operator<,一个类型,如果定义了 operator<,那么它可以在直接使用所有stl的排序/堆/查找算法,还有 set/map, multiset/multimap 容器。但是 std::find 等在非排序序列上操作的算法不算。

虽然,直观看,!(y < x) 和 x <= y 等价,然而,在数学定义上,两者并不等价,我们可以举出一些反例,但这些反例多多少看上去并不太“自然”。我正在努力寻找“很自然”的反例。

boost 提供了一系列模板类来从 operator< 推导其它操作符: ==, !=, <=, >, >=,原理上都很简单。

do {...} while

一般情况下, while (expr) {... } 和 do {...} while (expr) 等价,如果循环之前 expr 为 true 的话,在这种情况下,我们应该用 do {... } while,主要原因有两点:

  1. 少了一次对 expr 的求值
  2. 减少了一次非条件(绝对)跳转,和至少一次条件跳转(参考编译原理关于代码生成的章节,或者直接看编译器生成的汇编码)
当然,如果循环执行的次数很多的话,这个效率的损失微乎其微,但是,如果循环的执行次数很少,就像这本文例子中的,内循环的平均执行次数少于2次,这个多余的开销就相当可观了!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值