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

Cocktail Sort #198

Closed
TechnoPhasePRO opened this issue Mar 23, 2020 · 7 comments · Fixed by #312
Closed

Cocktail Sort #198

TechnoPhasePRO opened this issue Mar 23, 2020 · 7 comments · Fixed by #312

Comments

@TechnoPhasePRO
Copy link

Description of the problem

The code added sorts the array of numbers with minimum complexity as compared to bubble sort.

Example of the problem

References/Other comments

https://en.wikipedia.org/wiki/Cocktail_shaker_sort

@TechnoPhasePRO
Copy link
Author

@czgdp1807 please assign me this issue.

@TechnoPhasePRO
Copy link
Author

following is the API design for the cocktail sort :->

def cocktailsort(): The cocktail sort function is similar to bubble sort . It traverse thorough both
directions while sorting the array. The complexity in comparison is less.
list: It contains the list of elements to be sorted.
first: first indicates the first index of the array
last: last indicates the last index element of the array
k: variable that counts the length of the array
t: variable is used to denote if array is sorted after the sorting process. It is initialized 1 to
check if array is sorted after every iteration.
temp: It is used as third variable to exchange the values.
while t != 0: After each iteration, the value of t is reset which is evaluated in while condition. If value of t is 1, the control skips the iteration process.
for i in range(first, last, 1): this for loop will do the forward sort. the greatest element after each
pass will be at the last index. This for loop will do the forward sort
the greatest element after each pass will be at the last index.
if t == 0: t is initialized to 1 to check if the loop was sorted or not in the forward sort.
if value of t was not changed the control will jump off the loop. Else the control will
move to the backward sort.
for i in range(last, first - 1, -1): the next loop will do the backward sort in the array. Value of first
variable is increased every time as smallest element is found after
backward sort.
return (list): It returns the sorted array.

@TechnoPhasePRO
Copy link
Author

@czgdp1807 Please review my API design for cocktail sort.

@czgdp1807
Copy link
Member

@TechnoPhasePRO
It looks like you have provided the complete implementation. See the suggestions I have made below.
Cocktail sort is good as a sequential algorithm. However, if thought carefully we can parallelise it by using two threads, one thread moving from left to right and another moving from right to left simultaneously. The thing to be taken care of is to avoid a scenario where the equation, i = (n - 1)/2 has integer solutions for i. As in that case both the threads will clash and only one thread should be allowed process i and then the other one. Concept of locks can be used for this particular i to avoid inconsistency.

I would suggest the following APIs,

  1. cocktail_shaker_sort(array, **kwargs) - kwargs can be used to pass, start and end.
  2. cocktail_shaker_sort_parallel(array, num_threads, **kwargs) - Similar case for kwargs.

bandaabhigna added a commit to bandaabhigna/pydatastructs that referenced this issue Mar 23, 2020
@TechnoPhasePRO
Copy link
Author

@czgdp1807 i want to add the PR for this , that's why i proposed API first but @bandaabhigna already added PR without any API , i think this is wrong ????

@aditimaurya
Copy link

Hello, can you please assign this issue to me?

@czgdp1807
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants