排序算法_C++11的实现

本文介绍了一种结合了插入排序、堆排序和快速排序等算法优点的混合排序算法,并提供了详细的C++11代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

近来在学习<<STL源码剖析>>和C++11,按自己的理解自己动手实现排序算法的实现,代码中多用C++11的新特性。

 

这算是自娱自乐吧大笑


这里只考虑排序的对象的有move构造函数,move赋值函数。


[cpp]  view plain copy
  1. #ifndef YYRSort_H_  
  2. #define YYRSort_H_  
  3.   
  4. #include <algorithm>  
  5.   
  6. using namespace std;  
  7.   
  8. const size_t Threshold = 16;  
  9.   
  10. namespace YYRSort  
  11. {     
  12.     //插入排序  
  13.     template<typename RandomIterator>  
  14.     inline void InsertionSort(RandomIterator first, RandomIterator last)  
  15.     {  
  16.         if((last-first) < 2) //It don't need to sort when the container only has one or zero element.  
  17.         {  
  18.             return;  
  19.         };  
  20.   
  21.         auto Index = first + 1;  
  22.         decltype(Index) NextPos;  
  23.         decltype(Index) PrePos;  
  24.         auto TempValue = *Index;  
  25.   
  26.         for(;Index != last; ++Index)  
  27.         {  
  28.             NextPos = Index - 1;  
  29.             PrePos  = Index;  
  30.             TempValue = std::move(*Index);  
  31.   
  32.             if(TempValue < *first)  
  33.             {  
  34.                 std::move_backward(first, Index, Index+1);  
  35.                 *first = std::move(TempValue);  
  36.             }  
  37.             else  
  38.             {  
  39.                 while(true)  
  40.                 {  
  41.                     if(TempValue < *NextPos)  
  42.                     {  
  43.                         *PrePos = std::move(*NextPos);  
  44.                         PrePos = NextPos;  
  45.                         --NextPos;  
  46.                     }  
  47.                     else  
  48.                     {  
  49.                         *PrePos = std::move(TempValue);  
  50.                         break;  
  51.                     }  
  52.                 }  
  53.             }  
  54.         }  
  55.     }  
  56.   
  57.     //生成最大堆   
  58.     template<typename RandomIterator>  
  59.     inline void Make_Heap(RandomIterator first, RandomIterator last)  
  60.     {  
  61.         if((last-first) < 2)  
  62.         {  
  63.             return;  
  64.         }  
  65.   
  66.         auto TempValue = *first;  
  67.         for(auto Pos = first + 1; Pos != last; ++Pos)  
  68.         {  
  69.             auto Index = ((Pos - first) - 1) / 2;  
  70.             auto Parent = Pos;  
  71.             decltype(Pos) Child;  
  72.             while(true)  
  73.             {  
  74.                 Child = Parent;   
  75.                 Parent = first + Index;  
  76.                 if(*Parent < *Child)  
  77.                 {  
  78.                     TempValue = std::move(*Parent);  
  79.                     *Parent   = std::move(*Child);  
  80.                     *Child    = std::move(TempValue);  
  81.                     if(Index == 0)  
  82.                     {  
  83.                         break;  
  84.                     }  
  85.                     Index = (Index - 1) / 2;                      
  86.                 }  
  87.                 else  
  88.                 {  
  89.                     break;  
  90.                 }  
  91.             }  
  92.         }  
  93.     }  
  94.   
  95.     //堆排序  
  96.     template<typename RandomIterator>  
  97.     inline void Sort_Heap(RandomIterator first, RandomIterator last)  
  98.     {  
  99.         if((last-first) < 2)  
  100.         {  
  101.             return;  
  102.         }  
  103.           
  104.         Make_Heap(first, last);  
  105.   
  106.         --last;  
  107.   
  108.         auto TempValue = *first;  
  109.   
  110.         for(; first < last; --last)  
  111.         {  
  112.             TempValue = std::move(*first);  
  113.             *first    = std::move(*last);  
  114.             *last     = std::move(TempValue);  
  115.   
  116.             size_t Index = 0;  
  117.             auto Parent = first;  
  118.               
  119.             while(true)  
  120.             {  
  121.                 Index = (Index * 2) + 1;  
  122.                 auto LeftChild = first + Index;  
  123.                 if(!(LeftChild < last))  
  124.                 {  
  125.                     break;  
  126.                 }  
  127.   
  128.                 auto RightChild = LeftChild + 1;  
  129.                 if(!(RightChild < last))  
  130.                 {  
  131.                     if(*Parent < *LeftChild)  
  132.                     {  
  133.                         TempValue =  std::move(*Parent);  
  134.                         *Parent   =  std::move(*LeftChild);  
  135.                         *LeftChild = std::move(TempValue);  
  136.                         Parent = LeftChild;  
  137.                     }  
  138.                     else  
  139.                     {  
  140.                         break;  
  141.                     }  
  142.                 }  
  143.                 else  
  144.                 {  
  145.                     if(*Parent < *LeftChild)  
  146.                     {  
  147.                         if(*LeftChild < *RightChild)  
  148.                         {  
  149.                             TempValue =  std::move(*Parent);  
  150.                             *Parent   =  std::move(*RightChild);  
  151.                             *RightChild = std::move(TempValue);  
  152.                             Parent = RightChild;  
  153.                             ++Index;  
  154.                         }  
  155.                         else  
  156.                         {  
  157.                             TempValue =  std::move(*Parent);  
  158.                             *Parent   =  std::move(*LeftChild);  
  159.                             *LeftChild = std::move(TempValue);  
  160.                             Parent = LeftChild;  
  161.                         }  
  162.                     }  
  163.                     else  
  164.                     {  
  165.                         if(*Parent < *RightChild)  
  166.                         {  
  167.                             TempValue =  std::move(*Parent);  
  168.                             *Parent   =  std::move(*RightChild);  
  169.                             *RightChild = std::move(TempValue);  
  170.                             Parent = RightChild;  
  171.                             ++Index;  
  172.                         }  
  173.                         else  
  174.                         {  
  175.                             break;  
  176.                         }  
  177.                     }  
  178.                 }  
  179.             }  
  180.         }  
  181.     }  
  182.   
  183.     //取三个值的中值  
  184.     template<typename T>  
  185.     inline T& MedianThree(T& A, T& B, T& C)  
  186.     {  
  187.         if(A < B)  
  188.         {  
  189.             if(C < A)  
  190.             {  
  191.                 return A;  
  192.             }  
  193.             else if(B < C)  
  194.             {  
  195.                 return B;  
  196.             }  
  197.             else  
  198.             {  
  199.                 return C;  
  200.             }  
  201.         }  
  202.         else  
  203.         {  
  204.             if(C < B)  
  205.             {  
  206.                 return B;  
  207.             }  
  208.             else if(A < C)  
  209.             {  
  210.                 return A;  
  211.             }  
  212.             else  
  213.             {  
  214.                 return C;  
  215.             }  
  216.         }  
  217.     }  
  218.   
  219.     //分割区间为2个子区间,左边区间所有元素都小于右边区间的任意一个原始,返回左边区间的开始位置  
  220.     template<typename RandomIterator, typename T>  
  221.     inline RandomIterator Partition(RandomIterator first, RandomIterator last, T MidValue)  
  222.     {  
  223.         T TempValue = *first;  
  224.   
  225.         while(true)  
  226.         {  
  227.             while(*first < MidValue)  
  228.             {  
  229.                 ++first;  
  230.             }  
  231.             --last;  
  232.             while(MidValue < *last)  
  233.             {  
  234.                 --last;  
  235.             }  
  236.   
  237.             if(!(first < last))  
  238.             {  
  239.                 return first;  
  240.             }  
  241.   
  242.             TempValue = std::move(*first);  
  243.             *first = std::move(*last);  
  244.             *last = std::move(TempValue);  
  245.             ++first;  
  246.         }  
  247.     }  
  248.   
  249.     //快速排序  
  250.     template<typename RandomIterator>  
  251.     void QuickSort(RandomIterator first, RandomIterator last)  
  252.     {  
  253.         if(last - first < 2)  
  254.         {  
  255.             return;  
  256.         };  
  257.   
  258.         auto DivPos   = Partition(first, last,  
  259.                                   MedianThree(*first, *(last-1), *(first + (last-first)/2)));  
  260.         QuickSort(first, DivPos);  
  261.         QuickSort(DivPos, last);  
  262.     }  
  263.   
  264.     inline size_t _lg(size_t S)  
  265.     {  
  266.         size_t Ret = 0;  
  267.         while(S > 1)  
  268.         {  
  269.             ++Ret;  
  270.             S /= 2;  
  271.         }  
  272.         return Ret;  
  273.     }  
  274.   
  275.     template<typename RandomIterator>  
  276.     void _IntroSort(RandomIterator first, RandomIterator last, size_t LayerLimit)  
  277.     {  
  278.         if(last - first > Threshold)  
  279.         {  
  280.             if(LayerLimit > 0)  
  281.             {  
  282.                 auto DivPos   = Partition(first, last, MedianThree(*first, *(last-1), *(first + (last-first)/2)));  
  283.                 --LayerLimit;  
  284.                 _IntroSort(first, DivPos, LayerLimit);  
  285.                 _IntroSort(DivPos, last, LayerLimit);  
  286.             }  
  287.             else  
  288.             {  
  289.                 Sort_Heap(first, last);  
  290.             }  
  291.         }  
  292.     }  
  293.   
  294.     template<typename RandomIterator>  
  295.     inline void _InsertionSort_Unguard(RandomIterator first, RandomIterator last)  
  296.     {  
  297.         auto TempValue = *first;  
  298.         decltype(first) Pre;  
  299.         decltype(first) Next;  
  300.   
  301.         for(auto Pos = first + 1; Pos != last; ++Pos)  
  302.         {  
  303.             Pre  = Pos;  
  304.             Next = Pre - 1;  
  305.             TempValue = std::move(*Pos);  
  306.             while (true)  
  307.             {  
  308.                 if(TempValue < *Next)  
  309.                 {  
  310.                     *Pre = std::move(*Next);  
  311.                     Pre  = Next;  
  312.                     --Next;  
  313.                 }  
  314.                 else  
  315.                 {  
  316.                     *Pre = std::move(TempValue);  
  317.                     break;  
  318.                 }  
  319.             }  
  320.         }  
  321.     }  
  322.       
  323.     template<typename RandomIterator>  
  324.     inline void _InsertionSort(RandomIterator first, RandomIterator last)  
  325.     {  
  326.         if((last - first) > Threshold)  
  327.         {  
  328.             InsertionSort(first, first + Threshold);  
  329.             _InsertionSort_Unguard(first, last);              
  330.         }  
  331.         else  
  332.         {  
  333.             InsertionSort(first, last);  
  334.         }  
  335.     }  
  336.   
  337.     //混合排序  
  338.     template<typename RandomIterator>  
  339.     void Sort(RandomIterator first, RandomIterator last)  
  340.     {  
  341.         if((last - first) < 2)  
  342.         {  
  343.             return;  
  344.         }  
  345.   
  346.         size_t LayerLimit = _lg(last - first) * 2;  
  347.         _IntroSort(first, last, LayerLimit);  
  348.   
  349.         _InsertionSort(first, last);  
  350.     }  
  351. }  
  352.   
  353. #endif  

原文地址:http://blog.youkuaiyun.com/yyrrbadboy2012/article/details/8495072


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值