1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
| namespace N { class sort { public:
template<typename T> void swap(T& a, T& b) { T temp = a; a = b; b = temp; };
template<typename T> void Bubblesort(vector<T>& t) { int len = t.size();
for (int i = 0; i < len; ++i) { for (int j = len - 1; j > i; --j) { if (t[j - 1] > t[j]) { swap(t[j - 1], t[j]); } } } };
template<typename T> void Selectionsort(vector<T>& t) { int len = t.size(); int min; T val;
for (int i = 0; i < len - 1; ++i) { min = i; val = t[i];
for (int j = i + 1; j < len; ++j) { if (val > t[j]) { min = j; val = t[j]; } } swap(t[i], t[min]); } }
template<typename T> void Insertsort(vector<T>& t) { int len = t.size();
for (int i = 1; i < len; ++i) { for (int j = i; j > 0; --j) { if (t[j - 1] > t[j]) { swap(t[j - 1], t[j]); } else { break; } } }
}
template<typename T> void HeapSort(vector<T>& v, int i) { buildheap(v, 0); int len = v.size(); int lastnode = len - 1;
for (i = lastnode; i > 0; --i) { swap(v[0], v[i]); Heapify(v, i, 0); } }
template<typename T> void Groupsort(vector<T>& t, int L, int R) {
if (L == R) return; int mid = (L + R) / 2; Groupsort(t, L, mid); Groupsort(t, mid + 1, R);
_GroupSort(t, L, mid+1, R); }
template<typename T> void QuickSort(vector<T>& v, int L, int R) { T pivot = v[L]; int L_pre = L; int R_pre = R;
while (L < R) { if (v[R] < pivot) { v[L] = v[R]; --R; }
if (v[L] > pivot) { v[R] = v[L]; ++L; } }
v[L] = pivot;
QuickSort(v, L_pre, L); QuickSort(v, R, R_pre); }
private: template<typename T> void Heapify(vector<T>& v, int n, int i) { if (i > n) return;
int l_node = 2 * i + 1; int r_node = 2 * i + 2; int max = i;
if (l_node < n && v[i] < v[l_node]) { max = l_node; }
if (r_node < n && v[max] < v[r_node]) { max = r_node; }
if (max != i) { swap(v[i], v[max]); Heapify(v, n, max); } }
template<typename T> void buildheap(vector<T>& v, int i) { int len = v.size(); int lastnode = len - 1; int parent_lastnode = (lastnode - 1)/2;
for (i = parent_lastnode; i >= 0; --i) { Heapify(v, len, i); } }
template<typename T> void _GroupSort(vector<T>& v, int L, int M, int R) { int left_size = M - L; int right_size = R - M + 1; vector<T> LeftV(left_size); vector<T> RightV(right_size);
for (int i = L; i < M; ++i) { LeftV[i-L] = v[i]; }
for (int i = M; i <= R; ++i) { RightV[i - M] = v[i]; }
int i = 0, j = 0, k = L;
while (i < left_size && j < right_size) { if (LeftV[i] < RightV[j]) { v[k++] = LeftV[i++]; } else { v[k++] = RightV[j++]; } }
while (i < left_size) v[k++] = LeftV[i++]; while (j < right_size) v[k++] = RightV[j++]; } }; };
|