menu
techminis

A naukri.com initiative

google-web-stories
Home

>

Programming News

>

Sorting al...
source image

Prodevelopertutorial

1M

read

225

img
dot

Sorting algorithm 8: Cocktail Sort

  • Cocktail sort can be considered as an extension to Bubble Sort Algorithm.
  • Here instead of moving in one direction from left to right we move in the opposite direction right to left.
  • Cocktail sort will be faster than bubble sort, but it will not change the complexity.
  • The complexity will be still O(n^2).
  • Our algorithm uses “is_swapped” variable to know if the array is sorted or not.
  • The sorted array need not do any swapping.
  • Hence taking that as a basis we terminate the main loop.
  • Time complexity is O(n^2).
  • In the first pass, lowest element is at the left and the highest element is at the right.
  • In the second pass, the array is sorted.

Read Full Article

like

13 Likes

For uninterrupted reading, download the app