|
12 | 12 | 'matrix_multiply_parallel',
|
13 | 13 | 'counting_sort',
|
14 | 14 | 'bucket_sort',
|
| 15 | + 'cocktail_shaker_sort' |
15 | 16 | ]
|
16 | 17 |
|
17 | 18 | def _merge(array, sl, el, sr, er, end, comp):
|
@@ -498,7 +499,6 @@ def bucket_sort(array: Array, **kwargs) -> Array:
|
498 | 499 |
|
499 | 500 | This function does not support custom comparators as is the case with
|
500 | 501 | other sorting functions in this file.
|
501 |
| - The ouput array doesn't contain any `None` value. |
502 | 502 | """
|
503 | 503 | start = kwargs.get('start', 0)
|
504 | 504 | end = kwargs.get('end', len(array) - 1)
|
@@ -546,3 +546,78 @@ def bucket_sort(array: Array, **kwargs) -> Array:
|
546 | 546 | if _check_type(array, DynamicArray):
|
547 | 547 | array._modify(force=True)
|
548 | 548 | return array
|
| 549 | + |
| 550 | +def cocktail_shaker_sort(array: Array, **kwargs) -> Array: |
| 551 | + """ |
| 552 | + Performs cocktail sort on the given array. |
| 553 | +
|
| 554 | + Parameters |
| 555 | + ========== |
| 556 | +
|
| 557 | + array: Array |
| 558 | + The array which is to be sorted. |
| 559 | + start: int |
| 560 | + The starting index of the portion |
| 561 | + which is to be sorted. |
| 562 | + Optional, by default 0 |
| 563 | + end: int |
| 564 | + The ending index of the portion which |
| 565 | + is to be sorted. |
| 566 | + Optional, by default the index |
| 567 | + of the last position filled. |
| 568 | + comp: lambda/function |
| 569 | + The comparator which is to be used |
| 570 | + for sorting. If the function returns |
| 571 | + False then only swapping is performed. |
| 572 | + Optional, by default, less than or |
| 573 | + equal to is used for comparing two |
| 574 | + values. |
| 575 | +
|
| 576 | + Returns |
| 577 | + ======= |
| 578 | +
|
| 579 | + output: Array |
| 580 | + The sorted array. |
| 581 | +
|
| 582 | + Examples |
| 583 | + ======== |
| 584 | +
|
| 585 | + >>> from pydatastructs import OneDimensionalArray as ODA, cocktail_shaker_sort |
| 586 | + >>> arr = ODA(int, [5, 78, 1, 0]) |
| 587 | + >>> out = cocktail_shaker_sort(arr) |
| 588 | + >>> str(out) |
| 589 | + '[0, 1, 5, 78]' |
| 590 | + >>> arr = ODA(int, [21, 37, 5]) |
| 591 | + >>> out = cocktail_shaker_sort(arr) |
| 592 | + >>> str(out) |
| 593 | + '[5, 21, 37]' |
| 594 | +
|
| 595 | + References |
| 596 | + ========== |
| 597 | +
|
| 598 | + .. [1] https://en.wikipedia.org/wiki/Cocktail_shaker_sort |
| 599 | + """ |
| 600 | + def swap(i, j): |
| 601 | + array[i], array[j] = array[j], array[i] |
| 602 | + |
| 603 | + lower = kwargs.get('start', 0) |
| 604 | + upper = kwargs.get('end', len(array) - 1) |
| 605 | + comp = kwargs.get("comp", lambda u, v: u <= v) |
| 606 | + |
| 607 | + swapping = False |
| 608 | + while (not swapping and upper - lower >= 1): |
| 609 | + |
| 610 | + swapping = True |
| 611 | + for j in range(lower, upper): |
| 612 | + if _comp(array[j], array[j+1], comp) is False: |
| 613 | + swap(j + 1, j) |
| 614 | + swapping = False |
| 615 | + |
| 616 | + upper = upper - 1 |
| 617 | + for j in range(upper, lower, -1): |
| 618 | + if _comp(array[j-1], array[j], comp) is False: |
| 619 | + swap(j, j - 1) |
| 620 | + swapping = False |
| 621 | + lower = lower + 1 |
| 622 | + |
| 623 | + return array |
0 commit comments