flat_stable_sort.qbk 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. [/===========================================================================
  2. Copyright (c) 2017 Steven Ross, Francisco Tapia, Orson Peters
  3. Distributed under the Boost Software License, Version 1.0
  4. See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt
  6. =============================================================================/]
  7. [section:flat_stable_sort 2.4.- flat_stable_sort]
  8. [section:flat_stable_sort_description Description]
  9. [:
  10. [*Flat_stable_sort] is a new stable sort algorithm, designed and implemented by Francisco Jose Tapia for the Boost Sort Library
  11. The goal of the algorithm is to stably sort with little additional memory (about 1% of the memory used by the data).
  12. The default stable sort algorithms provided by most compilers and libraries use substantial additional memory, usually half of the data to sort.
  13. This new algorithm provides around 80%-90% of the speed of the spinsort and the stable sort algorithms provided by compilers and libraries.
  14. This algorithm is fast when the data is almost sorted. Many times the new elements are inserted at the end of the sorted elements,
  15. or some elements are modified, breaking the order of the elements. In these cases, the flat_stable_sort algorithm is very fast.
  16. [*[teletype]
  17. ``
  18. | | | |
  19. Algorithm | Stable | Additional Memory | Best, average, and worst case |
  20. -----------------+---------+-----------------------------+--------------------------------+
  21. flat_stable_sort | Yes | size of the data / 256 + 8K | N, NlogN , NlogN |
  22. | | | |
  23. ``
  24. ]
  25. ]
  26. [:
  27. [h4[_Memory Used]]
  28. Memory used by the stable sort algorithms measured on Linux x64
  29. [*[teletype]
  30. ``
  31. Algorithm | Memory used |
  32. -------------------+--------------+
  33. std::stable_sort | 1177 MB |
  34. spinsort | 1175 MB |
  35. flat_stable_sort | 788 MB |
  36. -------------------+--------------+
  37. ``
  38. ]
  39. ]
  40. [endsect]
  41. [section:flat_stable_sort_benchmark Benchmark]
  42. [:
  43. The benchmark with 100000000 64 bits integers,comparing with std::stable_sort of GCC 6.3 compiler and spinsort, running on a Intel i7-5820K CPU @ 3.30GH shows the mentioned characteristics.
  44. [*[teletype]
  45. ``
  46. Data |std::stable_sort| spin_sort |flat_stable_sort |
  47. -----------------------------+----------------+------------+-----------------+
  48. random | 8.62 | 9.73 | 10.80 |
  49. | | | |
  50. sorted | 4.88 | 0.06 | 0.07 |
  51. sorted + 0.1% end | 4.92 | 0.41 | 0.36 |
  52. sorted + 1% end | 4.97 | 0.55 | 0.49 |
  53. sorted + 10% end | 5.73 | 1.32 | 1.40 |
  54. | | | |
  55. sorted + 0.1% middle | 6.58 | 1.89 | 2.61 |
  56. sorted + 1% middle | 7.06 | 2.12 | 3.07 |
  57. sorted + 10% middle | 9.56 | 4.02 | 5.49 |
  58. | | | |
  59. reverse sorted | 0.13 | 0.14 | 1.87 |
  60. reverse sorted + 0.1% end | 5.22 | 0.52 | 0.42 |
  61. reverse sorted + 1% end | 5.29 | 0.66 | 0.55 |
  62. reverse sorted + 10% end | 6.03 | 1.45 | 1.44 |
  63. | | | |
  64. reverse sorted + 0.1% middle | 6.52 | 1.89 | 2.54 |
  65. reverse sorted + 1% middle | 7.09 | 2.12 | 3.09 |
  66. reverse sorted + 10% middle | 9.46 | 4.02 | 5.53 |
  67. | | | |
  68. -----------------------------+----------------+------------+-----------------+
  69. ``
  70. ]
  71. If you want detailed information about this algorithm you can find it in the [@../papers/flat_stable_sort_eng.pdf flat_stable_sort_en.pdf document]
  72. ]
  73. [endsect]
  74. [section:flat_stable_sort_programming Programming]
  75. [:
  76. You only need to include the file boost/sort/sort.hpp.
  77. The flat_stable_sort function is in the namespace boost::sort.
  78. [c++]
  79. ``
  80. #include <boost/sort/sort.hpp>
  81. template <class iter_t, typename compare>
  82. void flat_stable_sort (iter_t first, iter_t last, compare comp = compare());
  83. ``
  84. This algorithm uses a [*comparison object], in the same way as the standard library sort
  85. algorithms. If you don't define it, the comparison object defaults to std::less, which uses
  86. the < operator internally for comparisons.
  87. This algorithm is [*exception safe], meaning that, the exceptions generated by the algorithm
  88. guarantee the integrity of the objects to sort, but not their relative order. If the exception
  89. is generated inside the objects (in the move or copy constructors) the results are undefined.
  90. ]
  91. [endsect]
  92. [endsect]