Data Structures & Algorithms Multiple Choice Questions on “Cocktail Sort”.
1. Cocktail sort is also known as ________________ Answer: d 2. Cocktail sort is a variation of _____________ Answer: a 3. Auxiliary space requirement of cocktail sort is _____________ 4. Which of the following sorting algorithm is NOT stable? 5. Which of the following sorting algorithm is in place? 6. Cocktail sort is a comparison based sort. Answer: a 7. Cocktail sort uses which of the following methods for sorting the input? Answer: d 8. What is the worst case time complexity of cocktail sort? Answer: c 9. What is the best case time complexity of cocktail sort? Answer: a 10. What is the average case time complexity of odd-even sort? Answer: c 11. How many iterations are required to sort the array arr={2,3,4,5,1} using bubble sort and cocktail sort respectively? 12. The following function represents which sorting? a) Bubble sort 13. Bubble sort performs better as compared to cocktail sort. and Answers.
a) bidirectional sort
b) bubble sort
c) brick sort
d) ripple sort
Clarification: Cocktail sort is also known by the name of ripple sort. It is also known by other names like – bidirectional bubble sort, cocktail shaker sort, shuttle sort, shuffle sort etc.
a) Bubble sort
b) Selection sort
c) Insertion sort
d) Gnome sort
Clarification: Cocktail sort is very similar to bubble sort. It works by traversing an array in both directions alternatively. It compares the adjacent elements in each iteration and swaps the ones which are out of order.
a) O(n)
b) O(log n)
c) O(1)
d) O(n2)
Answer: c
Clarification: In cocktail sort manipulation is done on the input array itself. So no extra space is required to perform sorting. Thus it requires constant auxiliary space.
a) Quick sort
b) Cocktail sort
c) Bubble sort
d) Merge sort
Answer: a
Clarification: Out of the given options quick sort is the only algorithm which is not stable. Cocktail sort like bubble sort is a stable sorting algorithm.
a) cocktail sort
b) merge sort
c) counting sort
d) radix sort
Answer: a
Clarification: Cocktail sort is an in place sorting technique as it only requires constant auxiliary space for manipulating the input array. Rest all other options are not in place.
a) True
b) False
Clarification: Cocktail sort compares the value of different elements in the array for sorting. Thus, it is a comparison based sort.
a) selection
b) partitioning
c) merging
d) exchanging
Clarification: Cocktail sort uses a method of exchanging as it swaps the elements which are out of order. This swapping is done in two phases i.e. forward phase and backward phase.
a) O(n)
b) O(n log n)
c) O(n2)
d) O(log n)
Clarification: Worst case complexity is observed when the input array is reverse sorted. This is the same as the worst case complexity of bubble sort.
a) O(n)
b) O(n log n)
c) O(n2)
d) O(log n)
Clarification: Best case complexity is observed when the input array is already sorted. This is the same as the best case complexity of bubble sort.
a) O(n)
b) O(n log n)
c) O(n2)
d) O(log n)
Clarification: Cocktail sort takes O(n2) time on average as it keeps on applying bubble sort on the elements in two phases until they are sorted. This is the same as the average time complexity of bubble sort.
a) 4,2
b) 5,3
c) 5,2
d) 4,3
Answer: a
Clarification: Cocktail sort applies bubble sort in two phases until the array gets sorted. So here bubble sort will take 4 iterations to sort the given array whereas cocktail sort takes only 2 iterations. This shows cocktail sort has a comparatively better performance.void Sorting(int a[], int n)
{
bool swap = true;
int first = 0;
int last = n - 1;
while (swap)
{
swap = false;
for (int i = first; i < last;i++)
{
if (a[i] > a[i + 1])
{
swap(a[i], a[i + 1]);
swap = true;
}
}
if (!swap)
break;
swap = false;
--last;
for (int i = last - 1; i >= first; i--)
{
if (a[i] > a[i + 1])
{
swap(a[i], a[i + 1]);
swap = true;
}
}
++first;
}
}
b) Selection sort
c) Bidirectional bubble sort
d) Odd-even sort
Answer: c
Clarification: The given function represents bidirectional bubble sort also known as cocktail sort. In this sort, we apply bubble sort in two phases i.e. forward and backward phase until the array gets sorted.
a) True
b) False
Answer: b
Clarification: Both bubble sort and cocktail sort has the same time complexities. But cocktail sort has a comparatively better performance.