Cocktail sort

Cocktail sort, also known as bidirectional bubble sort, cocktail shaker sort, shaker sort, ripple sort, or shuttle sort, is a stable sorting algorithm that varies from bubble sort in that instead of repeatedly passing through the list from top to bottom, it passes alternately from top to bottom and then from bottom to top. It can achieve slightly better performance than a standard bubble sort.

Complexity in Big O notation is O(n²) for a worst case, but becomes closer to O(n) if the list is mostly ordered at the beginning.

Contents

Implementation

Python

 def cocktail_sort (aList, n):
     aList = aList[:]
     left = 0
     right = n
     while True:
         if finished:
             break
         finished = True
         right -= 1
         for index in range(left, right):
             if aList[index] > aList[index+1]:
                 aList[index], aList[index+1] = aList[index+1], aList[index]
                 finished = False
         if finished:
             return aList
         finished = True
         for index in range(right, left, -1)
             if aList[index] < aList[index-1]:
                 aList[index], aList[index-1] = aList[index-1], aList[index]
                 finished = False
         left+= 1

C++

void cocktail_sort (int A[], int n)
{
    int left = 0, right = n;
    bool finished;
    do
    {
        finished = true;
        --right;
        for (int i = left; i < right; i++)
            if (A[i] > A[i+1]) {
                std::swap(A[i], A[i+1]);
                finished = false;
            }
        if (finished) return; finished = true;
        for (int i = right; i > left; i--)
            if (A[i] < A[i-1]) {
                std::swap(A[i], A[i-1]);
                finished = false;
            }
        ++left;
    } while (!finished);
}

Perl

sub cocktail_sort(@)
{
  my @a = @_;
  my ($left,$right) = (0,$#_);
  while ($left < $right) {
    foreach $i ($left..$right-1) {
      ($a[$i],$a[$i+1]) = ($a[$i+1],$a[$i]) if ($a[$i] > $a[$i+1]);
    }
    $right--;
    foreach $i (reverse $left+1..$right) {
      ($a[$i],$a[$i-1]) = ($a[$i-1],$a[$i]) if ($a[$i] < $a[$i-1]);
    }
    $left++;
  }
  return @a;
}


FORTRAN 77

      SUBROUTINE cocktail_sort (A,LEN)
      INTEGER A, LEN, COUNTR, TEMP
      LOGICAL FLIP
      DIMENSION A(LEN)
      FLIP = .TRUE.
      WHILE (FLIP) DO
            COUNTR = 1
            FLIP = .FALSE.
            DO 16 COUNTR = 1, LEN - 1, 1
                  IF (A(COUNTR) .GT. A(COUNTR+1)) THEN
                        TEMP = A(COUNTR)
                        A(COUNTR) = A(COUNTR+1)
                        A(COUNTR+1) = TEMP
                        FLIP = .TRUE.
                  END IF
16          CONTINUE
            COUNTR = LEN
            DO 25 COUNTR = LEN, 2, -1
                  IF(A(COUNTR) .LT. A(COUNTR-1)) THEN
                        TEMP = A(COUNTR)
                        A(COUNTR) = A(COUNTR-1)
                        A(COUNTR-1) = TEMP
                        FLIP = .TRUE.
                  END IF
25          CONTINUE
      END WHILE                
      ENDde:Cocktailsort
Navigation

  • Art and Cultures
    • Art (https://academickids.com/encyclopedia/index.php/Art)
    • Architecture (https://academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (https://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (https://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools