Bubble Sort Cocktail Sort: A Group Project On Fundamentals of Computing 1
Bubble Sort Cocktail Sort: A Group Project On Fundamentals of Computing 1
COCKTAIL SORT
A Group Project on
Fundamentals of Computing 1
BS Mathematics-1
Group 7
Atienza, Nicole G.
Ellaga, Adam Bryll A.
Landicho, Karl Christian R.
Martinez, Francis Niel A.
Orquia, Jan Michael A.
Pondivida, Mary Jeniel R.
Tagbo, Maria Andrea O.
2022
TABLE OF CONTENTS
I.BUBBLE SORT
INTRODUCTION 1
TIME COMPLEXITY 1
SPACE COMPLEXITY 1
STEP-BY-STEP PROCEDURE 2
EXAMPLE 2
FLOWCHART 3
SAMPLE CODE 4
OUTPUT 4
References 10
BUBBLE SORT
Introduction
Bubble sort is a sorting algorithm in which two adjacent elements are compared and
swapped until they are no longer in the intended order. Each iteration moves each element of the
array closer to the end, similar to how air bubbles rise to the surface of the water. As a result, it’s
known as a bubble sort.
The name bubble sort comes from the fact that smaller or larger elements “bubble” to the
top of a dataset. In this example [3, 1, 4, 2], the 3 and 4 are bubbling up the dataset to find their
proper positions. Bubble sort is slow at moving small items that are near the end of the list and fast
at moving large items to the end. These are often referred to as turtles and rabbits, based on the
characters from Aesop’s fable “The Tortoise and the Hare.” The small items are often called
“turtles” since they are slow to move into position. The large items are often called “rabbits” since
they move quickly.
Bubble sort is a comparison-based sorting algorithm wherein comparing adjacent elements
is a primitive operation. Each pass compares the adjacent elements in the array and exchanges
those not in order. Basically, each pass through the array places the next largest value in its proper
place; hence the number of comparisons reduces by one at each pass. In other words, each item
“bubbles” up to the location where it is supposed to be in the sorted sequence. First, each element
is compared to its neighbor. Then the larger of the two moves to the spot closest to the end of the
list. These two steps are repeated until the last item is found. Once that largest element is brought
to the end, the end of the list is considered sorted, reducing the length of the “unsorted” list.
Time Complexity
Case Time Complexity
Space Complexity
Space Complexity O (1)
Stable YES
1
STEP-BY-STEP PROCEDURE
Step 1. Look at the first number in the list.
Step 2. Compare the current number with the next number.
Step 3. Is the next number smaller than the current number? If so, swap the two numbers
around. If not, do not swap.
Step 4. Move to the next number along in the list and make this the current number.
Step 5. Repeat from step 2 until the last number in the list has been reached.
Step 6. If any numbers were swapped, repeat again from step 1.
Step 7. If the end of the list is reached without any swaps being made, then the list is ordered,
and the algorithm can stop.
EXAMPLE:
2
Take an array of numbers “5 1 4 2 8”, and sort the array from lowest number to greatest number
using bubble sort. In each step, elements written in bold are being compared. Three passes will
be required;
First Pass
( 5 1 4 2 8 ) → ( 1 5 4 2 8 ), Here, the algorithm compares the first two elements and
swaps since 5 > 1.
( 1 5 4 2 8 ) → ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) → ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) → ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), the
algorithm does not swap them.
Second Pass
(14258)→(14258)
( 1 4 2 5 8 ) → ( 1 2 4 5 8 ), Swap since 4 > 2
(12458)→(12458)
(12458)→(12458)
Now, the array is already sorted, but the algorithm does not know if it is completed. The
algorithm needs one additional whole pass without any swap to know it is sorted.
Third Pass
(12458)→(12458)
(12458)→(12458)
(12458)→(12458)
(12458)→(12458)
FLOWCHART
3
SAMPLE CODE
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("Enter the number of elements: ");
scanf("%d", &n);
return 0;
}
OUTPUT
4
COCKTAIL SORT
Introduction
The Cocktail Sort algorithm is a modernized version of the Bubble Sort. The traditional
bubble sort always starts with the largest element and moves it to its correct position in the first
iteration, then does this again for the second-largest size until all elements are sorted. By switching
things up at every other iteration, cocktail sorts break down barriers that limit bubble sorts from
being efficient enough on large arrays by not allowing them to go through unnecessary iterations
on one specific region (or cluster) before moving onto another section of an array.
Turtles are small numbers at the end of the unsorted list that slowly moves to the front of
the list one position at a time using bubble sort. Turtles significantly reduce the performance of
bubble sort by increasing the number of passes the bubble sort must make through the list.
Cocktail sort can be defined as the variation of bubble sort. In the case of bubble sort, the
adjacent elements are compared from the beginning of the array, and the maximum element will
be stored at the proper sorted position after the first iteration. In the case of the cocktail sort, each
iteration has two parts: In the first part, the forward comparison is taken between the adjacent pairs
of elements that are precisely similar to the bubble sort. If the left item is greater than the right
one, it will be swapped. In the 2nd part, the backward comparison will be taken place from the
right side to the left. The comparison will start from the element just before the sorted element of
the forward iteration. After this backward iteration, the minimum element will be sorted and
positioned correctly.
After the first backward comparison, the minimum element will be stored in the sorted
position; after 2nd time of the backward comparison, the 2nd smallest element will be stored in its
sorted position, and so on. Here, in the case of the cocktail sort, one flag variable is taken, which
is used to check whether there is any swapping in either the forward pass or in the backward pass.
Any swapping in any pass will go for the next iteration; otherwise, the iteration will be stopped. It
takes a half number of iterations than the bubble sort to sort the array elements.
Time Complexity
Case Time Complexity
o Best Case Complexity occurs when there is no sorting required, i.e., the array is already
sorted. The best-case time complexity of cocktail sort is O(n).
o Average Case Complexity occurs when the array elements are in jumbled order that is not
properly ascending and not properly descending. The average case time complexity of
cocktail sort is O(n2).
5
o Worst Case Complexity occurs when the array elements are required to be sorted in
reverse order. That means suppose you have to sort the array elements in ascending order,
but its elements are in descending order. The worst-case time complexity of cocktail sort
is O(n2).
Space Complexity
Space Complexity O(1)
Stable YES
STEP-BY-STEP PROCEDURE
Step 1. The array is looped from left to right in the first stage.
Step 2. Compared the left and right items.
Step 3. If the left value is greater than the right value, and that value is swapped.
Step 4. After the first iteration, the number with the largest value will be at the end of the array.
Step 5. After the second stage, the array is looped through in reverse (starting from the item
immediately following the most recently sorted item and returning to the start).
Step 6. A similar comparison is conducted here, and if necessary, adjacent items are swapped.
EXAMPLE
Let us consider an example array (5 1 4 2 8 0 2)
First Forward Pass:
(5 1 4 2 8 0 2) → (1 5 4 2 8 0 2), Swap since 5 > 1
(1 5 4 2 8 0 2) → (1 4 5 2 8 0 2), Swap since 5 > 4
(1 4 5 2 8 0 2) → (1 4 2 5 8 0 2), Swap since 5 > 2
(1 4 2 5 8 0 2) → (1 4 2 5 8 0 2)
(1 4 2 5 8 0 2) → (1 4 2 5 0 8 2), Swap since 8 > 0
(1 4 2 5 0 8 2) → (1 4 2 5 0 2 8), Swap since 8 > 2
After the first forward pass, the greatest element of the array will be present at the last array
index.
6
Second Forward Pass:
(0 1 4 2 5 2 8) → (0 1 4 2 5 2 8)
(0 1 4 2 5 2 8) → (0 1 2 4 5 2 8), Swap since 4 > 2
(0 1 2 4 5 2 8) → (0 1 2 4 5 2 8)
(0 1 2 4 5 2 8) → (0 1 2 4 2 5 8), Swap since 5 > 2
FLOWCHART
7
SAMPLE CODE
#include<stdio.h>
int main(void){
int a[] = {5, 1, 4, 10, 0, 8};
int i;
size_t n = sizeof(a)/sizeof(a[0]);
printf("Original Array: \n");
for (i=0; i<n; i++)
printf("%d%s", a[i], i==n-1 ? "\n" : " ");
printf("\nSorted Array:\n");
cocktailsort(a, n);
for (i=0; i<n; ++i)
printf("%d ", a[i]);
return 0;
}
8
OUTPUT
The difference between cocktail sort and bubble sort is that instead of repeatedly passing through
the list from bottom to top, cocktail sort passes alternately from bottom to top and then from top
to bottom. The result is that it has a slightly better performance than bubble sort because it sorts in
both directions. Bubble sort can only move items backward one step per iteration.
Usually, cocktail or shaker sort passes (one time in both directions) are counted as two bubble sort
passes. The cocktail sort is less than two times faster than a bubble sort implementation. You have
to implement a loop in both directions that changes each pass that is slightly more difficult to
implement.
The time complexities are the same, but Cocktail performs better than Bubble Sort. Typically, the
cocktail sort is less than two times faster than the bubble sort. Consider the example (2, 3, 4, 5, 1).
For this example, bubble sort requires four traversals of an array, while Cocktail sort requires only
two traversals. As similar to the bubble sort, the worst and average case complexities of the cocktail
sort is O(n2). Cocktail sort is faster than bubble sort. And in the application, it can be used instead
of bubble sort to reduce the time to sort the data.
9
References
Astrachan, O. (2003). Bubble sort. ACM SIGCSE Bulletin, 35(1), 1–5.
https://doi.org/10.1145/792548.611918
Bubble sort. (n.d.). C Programming Simple Steps. Retrieved January 5, 2022, from
https://www.c-programming-simple-steps.com/bubble-sort.html
Bubble Sort (With Code). (n.d.). Programiz. Retrieved January 1, 2022, from
https://www.programiz.com/dsa/bubble-sort
C exercises: Sort numbers using Cocktail Sort method. (2021, December 20). W3resource.
Retrieved January 5, 2022, from https://www.w3resource.com/c-programming-
exercises/searching-and-sorting/c-search-and-sorting-exercise-15.php
Cocktail shaker sort. (2021, January 1). Wikipedia. Retrieved January 2, 2022, from
https://en.m.wikipedia.org/wiki/Cocktail_shaker_sort#:%7E:text=Cocktail%20shaker%2
0sort%20is%20a,than%20a%20standard%20bubble%20sort.
Cocktail Sort. (n.d.-a). MyCareerwise. Retrieved January 5, 2022, from
https://mycareerwise.com/programming/category/sorting/cocktail-sort
Cocktail Sort. (n.d.-b). Koderdojo. Retrieved January 3, 2022, from
https://www.koderdojo.com/blog/cocktail-sort
Cocktail Sort - javatpoint. (n.d.). Www.Javatpoint.Com. Retrieved December 30, 2021, from
https://www.javatpoint.com/cocktail-sort
Cocktail Sort Algorithm or Shaker Sort Algorithm » CodingUnit Programming Tutorials. (n.d.).
Codingunit. Retrieved January 4, 2022, from https://www.codingunit.com/cocktail-sort-
algorithm-or-shaker-sort-algorithm
cocktail sort c program code example | Newbedev. (n.d.). NewbeDEV. Retrieved January 5,
2022, from https://newbedev.com/java-cocktail-sort-c-program-code-example
Cocktail Sort Visualization. (n.d.). Coderstool. Retrieved January 5, 2022, from
https://www.coderstool.com/cocktail-sort
GeeksforGeeks. (2021, June 28). Cocktail Sort. Retrieved January 3, 2022, from
https://www.geeksforgeeks.org/cocktail-sort/
Kennisgeving voor omleiding. (2021, December 17). w3resource. Retrieved December 17, 2021,
from
https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.w3resource.com%2Fcsh
arp-exercises%2Fsearching-and-sorting-algorithm%2Fsearching-and-sorting-algorithm-
exercise-
3.php&psig=AOvVaw2ZG60XsJY1mlZEPbGzBrrq&ust=1640576764047000&source=i
mages&cd=vfe&ved=0CAsQjRxqFwoTCNjLwPDGgPUCFQAAAAAdAAAAABAD
Min, W. “Analysis on Bubble Sort Algorithm Optimization,” 2010 International Forum on
Information Technology and Applications, 2010, pp. 208-211, doi:
10.1109/IFITA.2010.9.
Sadagopan, N. COM 209T Design and Analysis of Algorithms -Lecture Notes. Retrieved
December 30, 2021, from
http://www.iiitdm.ac.in/old/Faculty_Teaching/Sadagopan/pdf/DAA/SortingAlgorithms.p
df
What Is Bubble Sort? Bubble Sort Definition, Advantages, & FAQ. (n.d.). Airfocus. Retrieved
January 5, 2022, from https://airfocus.com/glossary/what-is-bubble-
sort/#:%7E:text=The%20name%20bubble%20sort%20comes,the%20top%20of%20a%2
10
0dataset.&text=This%20algorithm%20is%20alternatively%20called,the%202%20are%2
0sinking%20elements.
Woltmann, S. (2021, November 27). Bubble Sort – Algorithm, Source Code, Time Complexity.
HappyCoders.Eu. Retrieved January 5, 2022, from
https://www.happycoders.eu/algorithms/bubble-sort/#Bubble_Sort_Time_Complexity
11