Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added cocktail shaker sort #312

Merged
merged 36 commits into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
5b34a79
Update queue.py
Arvind-raj06 Jan 7, 2021
52f01c3
Update linked_lists.py
Arvind-raj06 Jan 8, 2021
6610a7f
Update linked_lists.py
Arvind-raj06 Jan 8, 2021
4c5c855
Update linked_lists.py
Arvind-raj06 Jan 8, 2021
f93008a
Update queue.py
Arvind-raj06 Jan 8, 2021
6867add
Update linked_lists.py
Arvind-raj06 Jan 8, 2021
628045c
Update linked_lists.py
Arvind-raj06 Jan 8, 2021
09e5e42
Completed updating the insert_after
Arvind-raj06 Jan 8, 2021
74009e6
Update linked_lists.py
Arvind-raj06 Jan 8, 2021
0055baa
Update queue.py
Arvind-raj06 Jan 8, 2021
9dec38c
Update linked_lists.py
Arvind-raj06 Jan 10, 2021
708f6bc
Cocktail
Arvind-raj06 Jan 10, 2021
27a5f1a
Update algorithms.py
Arvind-raj06 Jan 10, 2021
6ae5a0c
Implementing the cocktail sort
Arvind-raj06 Jan 10, 2021
aaf529a
Update __init__.py
Arvind-raj06 Jan 10, 2021
9f937d4
Correcting error
Arvind-raj06 Jan 10, 2021
38ebcbe
Completion
Arvind-raj06 Jan 10, 2021
9a77768
Converting to ODA
Arvind-raj06 Jan 10, 2021
40350b4
Update algorithms.py
Arvind-raj06 Jan 10, 2021
a049c11
Update algorithms.py
Arvind-raj06 Jan 10, 2021
a990d46
Really!
Arvind-raj06 Jan 10, 2021
e49d268
Including cocktail sort
Arvind-raj06 Jan 11, 2021
c6c5fdd
Correcting for doda
Arvind-raj06 Jan 11, 2021
db70e68
Error Correction
Arvind-raj06 Jan 11, 2021
9acbebf
Yep done!
Arvind-raj06 Jan 11, 2021
57d7fbf
Hope this works fine
Arvind-raj06 Jan 11, 2021
3569652
Update algorithms.py
Arvind-raj06 Jan 11, 2021
c9d4f9c
Commit
Arvind-raj06 Jan 11, 2021
e1d817f
Cocktail update
Arvind-raj06 Jan 11, 2021
864e95c
Update algorithms.py
Arvind-raj06 Jan 11, 2021
570a287
Update test_algorithms.py
Arvind-raj06 Jan 12, 2021
cf91f8a
Let's check
Arvind-raj06 Jan 12, 2021
3c34257
Update test_algorithms.py
Arvind-raj06 Jan 12, 2021
1ae0e3d
Update test_algorithms.py
Arvind-raj06 Jan 12, 2021
89b903a
Fixed cocktail sort
czgdp1807 Jan 13, 2021
7a4581c
cocktail_sort -> cocktail_shaker_sort
czgdp1807 Jan 13, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pydatastructs/linear_data_structures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
heapsort,
matrix_multiply_parallel,
counting_sort,
bucket_sort
bucket_sort,
cocktail_sort
)
__all__.extend(algorithms.__all__)
61 changes: 61 additions & 0 deletions pydatastructs/linear_data_structures/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
'matrix_multiply_parallel',
'counting_sort',
'bucket_sort',
'cocktail_sort'
]

def _merge(array, sl, el, sr, er, end, comp):
Expand Down Expand Up @@ -546,3 +547,63 @@ def bucket_sort(array: Array, **kwargs) -> Array:
if _check_type(array, DynamicArray):
array._modify(force=True)
return array

def cocktail_sort(array: Array, **kwargs) -> Array:
"""
Performs cocktail sort on the given array.

Parameters
==========

array: Array
The array which is to be sorted.

Returns
=======

output: Array
The sorted array.

Examples
========

>>> from pydatastructs import OneDimensionalArray as ODA, cocktail_sort
>>> arr = ODA(int, [5, 78, 1, 0])
>>> out = cocktail_sort(arr)
>>> str(out)
'[0, 1, 5, 78]'
>>> arr = ODA(int, [21, 37, 5])
>>> out = cocktail_sort(arr)
>>> str(out)
'[5, 21, 37]'

References
==========

.. [1] https://en.wikipedia.org/wiki/Cocktail_shaker_sort

Note
====

Since, cocktail sort is a comparison sorting algorithm,
custom comparators aren't allowed.
The ouput array doesn't contain any `None` value.
"""
def swap(i, j):
array[i], array[j] = array[j], array[i]
lower = kwargs.get('start', 0)
upper = kwargs.get('end', len(array) - 1)
swapping = False
while (not swapping and upper - lower >= 1):
swapping = True
for j in range(lower, upper):
if array[j + 1] < array[j]:
swap(j + 1, j)
swapping = False
upper = upper - 1
for j in range(upper, lower, -1):
if array[j - 1] > array[j]:
swap(j - 1, j)
swapping = False
lower = lower + 1
return array
16 changes: 15 additions & 1 deletion pydatastructs/linear_data_structures/tests/test_algorithms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pydatastructs import (
merge_sort_parallel, DynamicOneDimensionalArray,
OneDimensionalArray, brick_sort, brick_sort_parallel,
heapsort, matrix_multiply_parallel, counting_sort, bucket_sort)
heapsort, matrix_multiply_parallel, counting_sort, bucket_sort, cocktail_sort)
from pydatastructs.utils.raises_util import raises
import random

Expand Down Expand Up @@ -70,6 +70,20 @@ def test_counting_sort():
480, 548, 686, 688, 696, 779]
assert counting_sort(arr)._data == expected_arr

def test_cocktail_sort():
random.seed(1000)

n = random.randint(10, 20)
arr = DynamicOneDimensionalArray(int, 0)
for _ in range(n):
arr.append(random.randint(1, 1000))
for _ in range(n//3):
arr.delete(random.randint(0, n//2))

expected_arr = [102, 134, 228, 247, 362, 373, 448,
480, 548, 686, 688, 696, 779]
assert counting_sort(arr)._data == expected_arr

def test_matrix_multiply_parallel():
ODA = OneDimensionalArray

Expand Down