insert_sort.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //----------------------------------------------------------------------------
  2. /// @file insert_sort.hpp
  3. /// @brief Insertion Sort algorithm
  4. ///
  5. /// @author Copyright (c) 2016 Francisco Jose Tapia (fjtapia@gmail.com )\n
  6. /// Distributed under the Boost Software License, Version 1.0.\n
  7. /// ( See accompanying file LICENSE_1_0.txt or copy at
  8. /// http://www.boost.org/LICENSE_1_0.txt )
  9. /// @version 0.1
  10. ///
  11. /// @remarks
  12. //-----------------------------------------------------------------------------
  13. #ifndef __BOOST_SORT_INTROSORT_DETAIL_INSERT_SORT_HPP
  14. #define __BOOST_SORT_INTROSORT_DETAIL_INSERT_SORT_HPP
  15. #include <functional>
  16. #include <iterator>
  17. #include <algorithm>
  18. #include <utility> // std::swap
  19. #include <boost/sort/common/util/traits.hpp>
  20. #include <boost/sort/common/util/insert.hpp>
  21. namespace boost
  22. {
  23. namespace sort
  24. {
  25. using common::util::compare_iter;
  26. using common::util::value_iter;
  27. //
  28. //-----------------------------------------------------------------------------
  29. // function : insert_sort
  30. /// @brief : Insertion sort algorithm
  31. /// @param first: iterator to the first element of the range
  32. /// @param last : iterator to the next element of the last in the range
  33. /// @param comp : object for to do the comparison between the elements
  34. /// @remarks This algorithm is O(N^2)
  35. //-----------------------------------------------------------------------------
  36. template < class Iter_t, typename Compare = compare_iter < Iter_t > >
  37. static void insert_sort (Iter_t first, Iter_t last,
  38. Compare comp = Compare())
  39. {
  40. //--------------------------------------------------------------------
  41. // DEFINITIONS
  42. //--------------------------------------------------------------------
  43. typedef value_iter< Iter_t > value_t;
  44. if ((last - first) < 2) return;
  45. for (Iter_t it_examine = first + 1; it_examine != last; ++it_examine)
  46. {
  47. value_t Aux = std::move (*it_examine);
  48. Iter_t it_insertion = it_examine;
  49. while (it_insertion != first and comp (Aux, *(it_insertion - 1)))
  50. {
  51. *it_insertion = std::move (*(it_insertion - 1));
  52. --it_insertion;
  53. };
  54. *it_insertion = std::move (Aux);
  55. };
  56. };
  57. /*
  58. //
  59. //-----------------------------------------------------------------------------
  60. // function : insert_partial_sort
  61. /// @brief : Insertion sort of elements sorted
  62. /// @param first: iterator to the first element of the range
  63. /// @param mid : last pointer of the sorted data, and first pointer to the
  64. /// elements to insert
  65. /// @param last : iterator to the next element of the last in the range
  66. /// @param comp : object for to do the comparison between the elements
  67. /// @remarks This algorithm is O(N^2)
  68. //-----------------------------------------------------------------------------
  69. template < class Iter_t, typename Compare = compare_iter < Iter_t > >
  70. void insert_partial_sort (Iter_t first, Iter_t mid, Iter_t last,
  71. Compare comp = Compare())
  72. {
  73. //--------------------------------------------------------------------
  74. // DEFINITIONS
  75. //--------------------------------------------------------------------
  76. typedef value_iter< Iter_t > value_t;
  77. if ( mid == last ) return ;
  78. insert_sort ( mid, last, comp);
  79. if (first == mid) return ;
  80. // creation of the vector of elements to insert and their position in the
  81. // sorted part
  82. std::vector<Iter_t> viter ;
  83. std::vector<value_t> vdata ;
  84. for ( Iter_t alpha = mid ; alpha != last ; ++alpha)
  85. vdata.push_back ( std::move ( *alpha));
  86. Iter_t linf = first , lsup = mid ;
  87. for ( uint32_t i= 0 ; i < vdata.size() ; ++i)
  88. { Iter_t it1 = std::upper_bound ( linf, lsup , vdata[i], comp);
  89. viter.push_back ( it1 );
  90. linf = it1 ;
  91. };
  92. // moving the elements
  93. viter.push_back ( mid) ;
  94. for ( uint32_t i = viter.size() -1 ; i!= 0 ; --i)
  95. { Iter_t src = viter[i], limit = viter[i-1];
  96. Iter_t dest = src + ( i);
  97. while ( src != limit) * (--dest) = std::move ( *(--src));
  98. *(viter[i-1] + (i -1)) = std::move (vdata[i-1]);
  99. };
  100. }
  101. */
  102. //
  103. //****************************************************************************
  104. }; // End namespace sort
  105. }; // End namespace boost
  106. //****************************************************************************
  107. //
  108. #endif