NeoAlgo icon indicating copy to clipboard operation
NeoAlgo copied to clipboard

Sorting Algorithm:Cocktail Sort

Open sahanabalappa opened this issue 6 years ago • 8 comments

Bubble sort algorithm can be further optimized with the help of Cocktail Sort. Here instead of moving in one direction from left to right we move in the opposite direction right to left. Hence, once we move the largest element towards the right, while returning back to the initial position, we take the smallest element to the left. Cocktail sort will be faster then bubble sort, but it will not change the complexity. The complexity will be still O(n^2). EX: Initial Un-Sorted array: {2, 7, 6, 4, 1, 8, 5, 3} Pass 1: Left to right: {2, 6, 7, 4, 1, 8, 5, 3} 2 > 6? False {2, 6, 4, 7, 1, 8, 5, 3} 6 > 4? True, swap {2, 4, 6, 1, 7, 8, 5, 3} 6 > 1? True, swap {2, 4, 1, 6, 7, 8, 5, 3} 6 > 7? False {2, 4, 1, 6, 7, 5, 8, 3} 7 > 5? True, swap {2, 4, 1, 6, 5, 7, 8, 3} 7 > 8? False {2, 4, 1, 6, 5, 7, 8, 3} 8 > 3? True, swap {2, 4, 1, 6, 5, 7, 3, 8}

Now from right to left: {2, 4, 1, 6, 5, 7, 3, 8} 3 < 7? True, swap {2, 4, 1, 6, 5, 3, 7, 8} 3 < 5? True, swap {2, 4, 1, 6, 3, 5, 7, 8} 3 < 6? True, swap {2, 4, 1, 3, 6, 5, 7, 8} 3 < 1? False {2, 1, 4, 3, 6, 5, 7, 8} 1 < 2? True, swap {1, 2, 4, 3, 6, 5, 7, 8}

Hence in the first pass, lowest element is at the left and highest element is at the right.

Pass 2:

Left to Right {1, 2, 4, 3, 6, 5, 7, 8} 1 > 2? False {1, 2, 4, 3, 6, 5, 7, 8} 2 > 4? False {1, 2, 4, 3, 6, 5, 7, 8} 4 > 3? True, swap {1, 2, 3, 4, 6, 5, 7, 8} 4 > 6? False {1, 2, 3, 4, 6, 5, 7, 8} 6 > 5? True, swap {1, 2, 3, 4, 5, 6, 7, 8} 6 > 7? False

Right to Left {1, 2, 3, 4, 5, 6, 7, 8} 6 < 5? False {1, 2, 3, 4, 5, 6, 7, 8} 5 < 4? False {1, 2, 3, 4, 5, 6, 7, 8} 4 < 3? False {1, 2, 3, 4, 5, 6, 7, 8} 3 < 2? False {1, 2, 3, 4, 5, 6, 7, 8} 2 < 1? False

Now the array is sorted.

sahanabalappa avatar Sep 06 '20 06:09 sahanabalappa

I would like to work on this issue in C

sahanabalappa avatar Sep 06 '20 06:09 sahanabalappa

I would like to take this in Java for HakinCodes CH20.

ritvij14 avatar Sep 06 '20 06:09 ritvij14

I would like to work on this issue in C++ . Please assign this task to me @plazzy99 @SKAUL05 @Abhijit2505 @atarax665

TEJASWI-TEJASWI avatar Sep 06 '20 06:09 TEJASWI-TEJASWI

I would like to take this issue in Python.

rutujadhanawade avatar Sep 06 '20 07:09 rutujadhanawade

Please go ahead with this issue. C: @sahanabalappa Java: @ritvij14 C++ : @TEJASWI-TEJASWI Python: @rutujadhanawade

Kajol-Kumari avatar Sep 10 '20 16:09 Kajol-Kumari

We are logging the scores for the Contributor's Hack 2020. If you have one or more than one PR merged during the program, make sure to fill this form

HarshCasper avatar Sep 26 '20 10:09 HarshCasper

@iamrajiv @Kajol-Kumari @siddharth25pandey @HarshCasper please add difficulty label to this issue.

rutujadhanawade avatar Sep 26 '20 10:09 rutujadhanawade

Please reopen this issue once you add more information and updates here. If this is not the case and you need some help, feel free to seek help from our Telegram or ping one of the reviewers. Thank you for your contributions!

github-actions[bot] avatar Nov 28 '20 00:11 github-actions[bot]