insertsort.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright 2002 Adrian Thurston <thurston@cs.queensu.ca>
  3. */
  4. /* This file is part of Aapl.
  5. *
  6. * Aapl is free software; you can redistribute it and/or modify it under the
  7. * terms of the GNU Lesser General Public License as published by the Free
  8. * Software Foundation; either version 2.1 of the License, or (at your option)
  9. * any later version.
  10. *
  11. * Aapl is distributed in the hope that it will be useful, but WITHOUT ANY
  12. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with Aapl; if not, write to the Free Software Foundation, Inc., 59
  18. * Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #ifndef _AAPL_INSERTSORT_H
  21. #define _AAPL_INSERTSORT_H
  22. #ifdef AAPL_NAMESPACE
  23. namespace Aapl {
  24. #endif
  25. /**
  26. * \addtogroup sort
  27. * @{
  28. */
  29. /**
  30. * \class InsertSort
  31. * \brief Insertion sort an array of data.
  32. *
  33. * InsertSort can be used to sort any array of objects of type T provided a
  34. * compare class is given. InsertSort is in-place. It does not require any
  35. * temporary storage.
  36. *
  37. * Objects are not made aware that they are being moved around in memory.
  38. * Assignment operators, constructors and destructors are never invoked by the
  39. * sort.
  40. *
  41. * InsertSort runs in O(n^2) time. It is most useful when sorting small arrays.
  42. * where it can outperform the O(n*log(n)) sorters due to its simplicity.
  43. * InsertSort is a not a stable sort. Elements with the same key will not have
  44. * their relative ordering preserved.
  45. */
  46. /*@}*/
  47. /* InsertSort. */
  48. template <class T, class Compare> class InsertSort
  49. : public Compare
  50. {
  51. public:
  52. /* Sorting interface routine. */
  53. void sort(T *data, long len);
  54. };
  55. /**
  56. * \brief Insertion sort an array of data.
  57. */
  58. template <class T, class Compare>
  59. void InsertSort<T,Compare>::sort(T *data, long len)
  60. {
  61. /* For each next largest spot in the sorted array... */
  62. for ( T *dest = data; dest < data+len-1; dest++ ) {
  63. /* Find the next smallest element in the unsorted array. */
  64. T *smallest = dest;
  65. for ( T *src = dest+1; src < data+len; src++ ) {
  66. /* If src is smaller than the current src, then use it. */
  67. if ( compare( *src, *smallest ) < 0 )
  68. smallest = src;
  69. }
  70. if ( smallest != dest ) {
  71. /* Swap dest, smallest. */
  72. char tmp[sizeof(T)];
  73. memcpy( tmp, dest, sizeof(T) );
  74. memcpy( dest, smallest, sizeof(T) );
  75. memcpy( smallest, tmp, sizeof(T) );
  76. }
  77. }
  78. }
  79. #ifdef AAPL_NAMESPACE
  80. }
  81. #endif
  82. #endif /* _AAPL_INSERTSORT_H */