bubblesort.h 2.5 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_BUBBLESORT_H
  21. #define _AAPL_BUBBLESORT_H
  22. #ifdef AAPL_NAMESPACE
  23. namespace Aapl {
  24. #endif
  25. /**
  26. * \addtogroup sort
  27. * @{
  28. */
  29. /**
  30. * \class BubbleSort
  31. * \brief Bubble sort an array of data.
  32. *
  33. * BubbleSort can be used to sort any array of objects of type T provided a
  34. * compare class is given. BubbleSort 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. * BubbleSort runs in O(n^2) time. It is most useful when sorting arrays that
  42. * are nearly sorted. It is best when neighbouring pairs are out of place.
  43. * BubbleSort is a stable sort, meaning that objects with the same key have
  44. * their relative ordering preserved.
  45. */
  46. /*@}*/
  47. /* BubbleSort. */
  48. template <class T, class Compare> class BubbleSort
  49. : public Compare
  50. {
  51. public:
  52. /* Sorting interface routine. */
  53. void sort(T *data, long len);
  54. };
  55. /**
  56. * \brief Bubble sort an array of data.
  57. */
  58. template <class T, class Compare> void BubbleSort<T,Compare>::
  59. sort(T *data, long len)
  60. {
  61. bool changed = true;
  62. for ( long pass = 1; changed && pass < len; pass ++ ) {
  63. changed = false;
  64. for ( long i = 0; i < len-pass; i++ ) {
  65. /* Do we swap pos with the next one? */
  66. if ( this->compare( data[i], data[i+1] ) > 0 ) {
  67. char tmp[sizeof(T)];
  68. /* Swap the two items. */
  69. memcpy( tmp, data+i, sizeof(T) );
  70. memcpy( data+i, data+i+1, sizeof(T) );
  71. memcpy( data+i+1, tmp, sizeof(T) );
  72. /* Note that we made a change. */
  73. changed = true;
  74. }
  75. }
  76. }
  77. }
  78. #ifdef AAPL_NAMESPACE
  79. }
  80. #endif
  81. #endif /* _AAPL_BUBBLESORT_H */