Neoalgo: Sorting Algorithm:Cocktail Sort

Created on 6 Sep 2020  路  8Comments  路  Source: TesseractCoding/NeoAlgo

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.

C C-Plus-Plus CH20 Java Python easy no-issue-activity

All 8 comments

I would like to work on this issue in C

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

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

I would like to take this issue in Python.

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

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

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

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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

HarshCasper picture HarshCasper  路  8Comments

Joshitha18 picture Joshitha18  路  9Comments

TEJASWI-TEJASWI picture TEJASWI-TEJASWI  路  9Comments

HarshCasper picture HarshCasper  路  8Comments

shivangi00 picture shivangi00  路  7Comments