00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef __SEAL_SORT_H__
00030 #define __SEAL_SORT_H__
00031
00032 #include "randaccess.h"
00033 #include "utils.h"
00034
00035 namespace seal
00036 {
00037
00040 enum SortOrder
00041 {
00044 ASCENDING = 0,
00045
00048 DESCENDING = 1
00049 };
00050
00056 template <typename T>
00057 class SortingAlgorithm
00058 {
00059 public:
00060
00063 SortingAlgorithm(void) {}
00064
00067 virtual ~SortingAlgorithm() {}
00068
00078 virtual void operator()(RandomAccess<T> &array, int from, int to, SortOrder order = ASCENDING) {}
00079
00089 virtual void sort(RandomAccess<T> &array, int from, int to, SortOrder order = ASCENDING) = 0;
00090
00091 };
00092
00093
00094
00095
00096
00101 template <typename T>
00102 class BubbleSort : public SortingAlgorithm<T>
00103 {
00104 public:
00107 BubbleSort(void) : SortingAlgorithm<T>() {}
00108
00111 virtual ~BubbleSort() {}
00112
00122 virtual void operator()(RandomAccess<T> &array, int from, int to, SortOrder order = ASCENDING);
00123
00133 virtual void sort(RandomAccess<T> &array, int from, int to, SortOrder order = ASCENDING)
00134 {
00135 operator()(array, from, to, order);
00136 }
00137
00138 };
00139
00140 template <typename T>
00141 void BubbleSort<T>::operator()(RandomAccess<T> &a, int from, int to, SortOrder order)
00142 {
00143 if (to < from)
00144 {
00145 order = (order == ASCENDING ? DESCENDING : ASCENDING);
00146 swap(from, to);
00147 }
00148
00149 if (order == ASCENDING)
00150 {
00151 for (int n = to; n > from; n--)
00152 {
00153 for (int i = from; i < n; i++)
00154 {
00155 if (a[i] > a[i+1])
00156 {
00157 T temp = a[i];
00158 a[i] = a[i+1];
00159 a[i+1] = temp;
00160 }
00161 }
00162 }
00163 }
00164
00165 if (order == DESCENDING)
00166 {
00167 for (int n = to; n > from; n--)
00168 {
00169 for (int i = from; i < n; i++)
00170 {
00171 if (a[i] < a[i+1])
00172 {
00173 T temp = a[i];
00174 a[i] = a[i+1];
00175 a[i+1] = temp;
00176 }
00177 }
00178 }
00179 }
00180
00181 }
00182
00183
00184
00185
00186
00191 template <typename T>
00192 class MergeSort : public SortingAlgorithm<T>
00193 {
00194 public:
00195
00198 MergeSort(void) : SortingAlgorithm<T>() {}
00199
00202 virtual ~MergeSort() {}
00203
00213 virtual void operator()(RandomAccess<T> &array, int from, int to, SortOrder order = ASCENDING);
00214
00224 virtual void sort(RandomAccess<T> &array, int from, int to, SortOrder order = ASCENDING)
00225 {
00226 operator()(array, from, to, order);
00227 }
00228
00229 };
00230
00231 template <typename T>
00232 void MergeSort<T>::operator()(RandomAccess<T> &a, int from, int to, SortOrder order)
00233 {
00234 if (to < from)
00235 {
00236 order = (order == ASCENDING ? DESCENDING : ASCENDING);
00237 swap(from, to);
00238 }
00239
00240 if (to == from)
00241 return;
00242 else
00243 {
00244 int mid = (to+from)/2;
00245 operator()(a, from, mid, order);
00246 operator()(a, mid+1, to, order);
00247
00248
00249 int insertPos = from;
00250 if (order == ASCENDING)
00251 {
00252 for (int i = mid+1; i <= to; i++)
00253 {
00254 for (int j = insertPos; j < i; j++)
00255 {
00256 if (a[j] > a[i])
00257 {
00258 T temp = a[i];
00259 for (int k = i; k > j; k--)
00260 a[k] = a[k-1];
00261
00262 a[j] = temp;
00263 insertPos = j;
00264 break;
00265 }
00266 }
00267 }
00268 }
00269
00270 if (order == DESCENDING)
00271 {
00272 for (int i = mid+1; i <= to; i++)
00273 {
00274 for (int j = insertPos; j < i; j++)
00275 {
00276 if (a[j] < a[i])
00277 {
00278 T temp = a[i];
00279 for (int k = i; k > j; k--)
00280 a[k] = a[k-1];
00281
00282 a[j] = temp;
00283 insertPos = j;
00284 break;
00285 }
00286 }
00287 }
00288 }
00289 }
00290 }
00291
00292
00293
00294
00295
00296 }
00297
00298 #endif // __SEAL_SORT_H__
00299