00001
00029 #ifndef ABA_SORTER_INC
00030 #define ABA_SORTER_INC
00031
00032 template <class ItemType, class KeyType>
00033 ABA_SORTER<ItemType, KeyType>::ABA_SORTER(ABA_GLOBAL *glob)
00034 :
00035 glob_(glob)
00036 { }
00037
00038 template <class ItemType, class KeyType>
00039 void ABA_SORTER<ItemType, KeyType>::quickSort(int n,
00040 ABA_ARRAY<ItemType> &items,
00041 ABA_ARRAY<KeyType> &keys)
00042 {
00043 quickSort(items, keys, 0, n - 1);
00044 }
00045
00046 template <class ItemType, class KeyType>
00047 void ABA_SORTER<ItemType, KeyType>::quickSort(ABA_ARRAY<ItemType> &items,
00048 ABA_ARRAY<KeyType> &keys,
00049 int left,
00050 int right)
00051 {
00052 if (left < right) {
00053 int q = partition(items, keys, left, right);
00054
00055 quickSort(items, keys, left, q);
00056 quickSort(items, keys, q+1, right);
00057 }
00058 }
00059
00060 template <class ItemType, class KeyType>
00061 int ABA_SORTER<ItemType, KeyType>::
00062 partition(ABA_ARRAY<ItemType> &items, ABA_ARRAY<KeyType> &keys,
00063 int left,
00064 int right)
00065 {
00066
00067 KeyType k = keys[left];
00068
00069 int l = left - 1;
00070 int r = right + 1;
00071
00072 while (1) {
00073
00074 do r--; while(k < keys[r]);
00075 do l++; while(keys[l] < k);
00076
00077 if(l < r) {
00079 itemSwap_ = items[l];
00080 items[l] = items[r];
00081 items[r] = itemSwap_;
00082
00083 keySwap_ = keys[l];
00084 keys[l] = keys[r];
00085 keys[r] = keySwap_;
00086
00087 }
00088 else
00089 return r;
00090 }
00091 }
00092
00093 template <class ItemType, class KeyType>
00094 void ABA_SORTER<ItemType, KeyType>::heapSort(int n,
00095 ABA_ARRAY<ItemType> &items,
00096 ABA_ARRAY<KeyType> &keys)
00097 {
00098 buildHeap(n, items, keys);
00099
00100 for (int i = n - 1; i > 0; i--) {
00102 itemSwap_ = items[0];
00103 items[0] = items[i];
00104 items[i] = itemSwap_;
00105
00106 keySwap_ = keys[0];
00107 keys[0] = keys[i];
00108 keys[i] = keySwap_;
00109
00110 heapify(i, items, keys, 0);
00111 }
00112 }
00113
00114 template <class ItemType, class KeyType>
00115 void ABA_SORTER<ItemType, KeyType>::
00116 buildHeap(int n, ABA_ARRAY<ItemType> &items, ABA_ARRAY<KeyType> &keys)
00117 {
00118 for (int i = n/2 - 1; i >= 0; i--)
00119 heapify(n, items, keys, i);
00120 }
00121
00122 template <class ItemType, class KeyType>
00123 void ABA_SORTER<ItemType, KeyType>::
00124 heapify(int n, ABA_ARRAY<ItemType> &items, ABA_ARRAY<KeyType> &keys, int root)
00125 {
00126 int l, r, largest;
00127
00128 int lRoot = root;
00129
00130 while (1) {
00131 l = 2*lRoot + 1;
00132 r = l + 1;
00133
00135 if (l < n && keys[l] > keys[lRoot])
00136 largest = l;
00137 else
00138 largest = lRoot;
00139
00140 if (r < n && keys[r] > keys[largest])
00141 largest = r;
00142
00143
00144 if (largest != lRoot) {
00146 itemSwap_ = items[largest];
00147 items[largest] = items[lRoot];
00148 items[lRoot] = itemSwap_;
00149
00150 keySwap_ = keys[largest];
00151 keys[largest] = keys[lRoot];
00152 keys[lRoot] = keySwap_;
00153
00154 lRoot = largest;
00155 }
00156 else break;
00157 }
00158 }
00159
00160 template <class ItemType, class KeyType>
00161 void ABA_SORTER<ItemType, KeyType>::
00162 check(int n, ABA_ARRAY<ItemType> &items, ABA_ARRAY<KeyType> &keys)
00163 {
00164 for (int i = 0; i < n - 1; i++)
00165 if (keys[i] > keys[i + 1]) {
00166 glob_->err() << "ABA_SORTER::check(): error" << endl;
00167 glob_->err() << "keys[" << i << "] = " << keys[i];
00168 glob_->err() << " > keys[" << i + 1 << "] = " << keys[i+1] << "." << endl;
00169 exit(Fatal);
00170 }
00171 }
00172
00173
00174 #endif // ABA_SORTER_INC