|
6 | 6 |
|
7 | 7 | __all__ = [
|
8 | 8 | 'merge_sort_parallel',
|
9 |
| - 'brick_sort' |
| 9 | + 'brick_sort', |
| 10 | + 'brick_sort_parallel' |
10 | 11 | ]
|
11 | 12 |
|
12 | 13 | def _merge(array, sl, el, sr, er, end, comp):
|
@@ -163,3 +164,72 @@ def brick_sort(array, **kwargs):
|
163 | 164 |
|
164 | 165 | if _check_type(array, DynamicArray):
|
165 | 166 | array._modify(force=True)
|
| 167 | + |
| 168 | +def _brick_sort_swap(array, i, j, comp, is_sorted): |
| 169 | + if _comp(array[j], array[i], comp): |
| 170 | + array[i], array[j] = array[j], array[i] |
| 171 | + is_sorted[0] = False |
| 172 | + |
| 173 | +def brick_sort_parallel(array, num_threads, **kwargs): |
| 174 | + """ |
| 175 | + Implements Concurrent Brick Sort / Odd Even sorting algorithm |
| 176 | +
|
| 177 | + Parameters |
| 178 | + ========== |
| 179 | +
|
| 180 | + array: Array/list |
| 181 | + The array which is to be sorted. |
| 182 | + num_threads: int |
| 183 | + The maximum number of threads |
| 184 | + to be used for sorting. |
| 185 | + start: int |
| 186 | + The starting index of the portion |
| 187 | + which is to be sorted. |
| 188 | + Optional, by default 0 |
| 189 | + end: int |
| 190 | + The ending index of the portion which |
| 191 | + is to be sorted. |
| 192 | + Optional, by default the index |
| 193 | + of the last position filled. |
| 194 | + comp: lambda/function |
| 195 | + The comparator which is to be used |
| 196 | + for sorting. If the function returns |
| 197 | + False then only swapping is performed. |
| 198 | + Optional, by default, less than or |
| 199 | + equal to is used for comparing two |
| 200 | + values. |
| 201 | +
|
| 202 | + Examples |
| 203 | + ======== |
| 204 | +
|
| 205 | + >>> from pydatastructs import OneDimensionalArray, brick_sort_parallel |
| 206 | + >>> arr = OneDimensionalArray(int,[3, 2, 1]) |
| 207 | + >>> brick_sort_parallel(arr, num_threads=5) |
| 208 | + >>> [arr[0], arr[1], arr[2]] |
| 209 | + [1, 2, 3] |
| 210 | + >>> brick_sort_parallel(arr, num_threads=5, comp=lambda u, v: u > v) |
| 211 | + >>> [arr[0], arr[1], arr[2]] |
| 212 | + [3, 2, 1] |
| 213 | +
|
| 214 | + References |
| 215 | + ========== |
| 216 | +
|
| 217 | + .. [1] https://en.wikipedia.org/wiki/Odd%E2%80%93even_sort |
| 218 | + """ |
| 219 | + |
| 220 | + start = kwargs.get('start', 0) |
| 221 | + end = kwargs.get('end', len(array) - 1) |
| 222 | + comp = kwargs.get("comp", lambda u, v: u <= v) |
| 223 | + |
| 224 | + is_sorted = [False] |
| 225 | + with ThreadPoolExecutor(max_workers=num_threads) as Executor: |
| 226 | + while is_sorted[0] is False: |
| 227 | + is_sorted[0] = True |
| 228 | + for i in range(start + 1, end, 2): |
| 229 | + Executor.submit(_brick_sort_swap, array, i, i + 1, comp, is_sorted).result() |
| 230 | + |
| 231 | + for i in range(start, end, 2): |
| 232 | + Executor.submit(_brick_sort_swap, array, i, i + 1, comp, is_sorted).result() |
| 233 | + |
| 234 | + if _check_type(array, DynamicArray): |
| 235 | + array._modify(force=True) |
0 commit comments