Data Structures Tutorial

Data Structures Tutorial Asymptotic Notation Structure and Union Array Data Structure Linked list Data Structure Type of Linked list Advantages and Disadvantages of linked list Queue Data Structure Implementation of Queue Stack Data Structure Implementation of Stack Sorting Insertion sort Quick sort Selection sort Heap sort Merge sort Bucket sort Count sort Radix sort Shell sort Tree Traversal of the binary tree Binary search tree Graph Spanning tree Linear Search Binary Search Hashing Collision Resolution Techniques

Misc Topic:

Priority Queue in Data Structure Deque in Data Structure Difference Between Linear And Non Linear Data Structures Queue Operations In Data Structure About Data Structures Data Structures Algorithms Types of Data Structures Big O Notations Introduction to Arrays Introduction to 1D-Arrays Operations on 1D-Arrays Introduction to 2D-Arrays Operations on 2D-Arrays Strings in Data Structures String Operations Application of 2D array Bubble Sort Insertion Sort Sorting Algorithms What is DFS Algorithm What Is Graph Data Structure What is the difference between Tree and Graph What is the difference between DFS and BFS Bucket Sort Dijkstra’s vs Bellman-Ford Algorithm Linear Queue Data Structure in C Stack Using Array Stack Using Linked List Recursion in Fibonacci Stack vs Array What is Skewed Binary Tree Primitive Data Structure in C Dynamic memory allocation of structure in C Application of Stack in Data Structures Binary Tree in Data Structures Heap Data Structure Recursion - Factorial and Fibonacci What is B tree what is B+ tree Huffman tree in Data Structures Insertion Sort vs Bubble Sort Adding one to the number represented an array of digits Bitwise Operators and their Important Tricks Blowfish algorithm Bubble Sort vs Selection Sort Hashing and its Applications Heap Sort vs Merge Sort Insertion Sort vs Selection Sort Merge Conflicts and ways to handle them Difference between Stack and Queue AVL tree in data structure c++ Bubble sort algorithm using Javascript Buffer overflow attack with examples Find out the area between two concentric circles Lowest common ancestor in a binary search tree Number of visible boxes putting one inside another Program to calculate the area of the circumcircle of an equilateral triangle Red-black Tree in Data Structures Strictly binary tree in Data Structures 2-3 Trees and Basic Operations on them Asynchronous advantage actor-critic (A3C) Algorithm Bubble Sort vs Heap Sort Digital Search Tree in Data Structures Minimum Spanning Tree Permutation Sort or Bogo Sort Quick Sort vs Merge Sort Boruvkas algorithm Bubble Sort vs Quick Sort Common Operations on various Data Structures Detect and Remove Loop in a Linked List How to Start Learning DSA Print kth least significant bit number Why is Binary Heap Preferred over BST for Priority Queue Bin Packing Problem Binary Tree Inorder Traversal Burning binary tree Equal Sum What is a Threaded Binary Tree? What is a full Binary Tree? Bubble Sort vs Merge Sort B+ Tree Program in Q language Deletion Operation from A B Tree Deletion Operation of the binary search tree in C++ language Does Overloading Work with Inheritance Balanced Binary Tree Binary tree deletion Binary tree insertion Cocktail Sort Comb Sort FIFO approach Operations of B Tree in C++ Language Recaman’s Sequence Tim Sort Understanding Data Processing Applications of trees in data structures Binary Tree Implementation Using Arrays Convert a Binary Tree into a Binary Search Tree Create a binary search tree Horizontal and Vertical Scaling Invert binary tree LCA of binary tree Linked List Representation of Binary Tree Optimal binary search tree in DSA Serialize and Deserialize a Binary Tree Tree terminology in Data structures Vertical Order Traversal of Binary Tree What is a Height-Balanced Tree in Data Structure Convert binary tree to a doubly linked list Fundamental of Algorithms Introduction and Implementation of Bloom Filter Optimal binary search tree using dynamic programming Right side view of binary tree Symmetric binary tree Trim a binary search tree What is a Sparse Matrix in Data Structure What is a Tree in Terms of a Graph What is the Use of Segment Trees in Data Structure What Should We Learn First Trees or Graphs in Data Structures All About Minimum Cost Spanning Trees in Data Structure Convert Binary Tree into a Threaded Binary Tree Difference between Structured and Object-Oriented Analysis FLEX (Fast Lexical Analyzer Generator) Object-Oriented Analysis and Design Sum of Nodes in a Binary Tree What are the types of Trees in Data Structure What is a 2-3 Tree in Data Structure What is a Spanning Tree in Data Structure What is an AVL Tree in Data Structure Given a Binary Tree, Check if it's balanced

Cocktail Sort

C Program executes cocktail sort.

Combo sort is a somewhat straightforward arranging calculation initially planned by Wlodzimierz Dobosiewicz and Artur Borowy in 1980, later rediscovered by Stephen Lacey and Richard Box in 1991.

Combo sort enhances bubble sort similarly that Shellsort enhances inclusion sort.

cocktail sort is a variety of air pocket sort that is both a steady arranging calculation and an examination sort.

The calculation contrasts from an air pocket sort in that it figures out in the two headings on each pass the rundown.

This arranging calculation is simply hardly more challenging to execute than an air pocket sort, and takes care of the issue of turtles in bubble sorts.

The different names of mixed drink sort are bidirectional cocktail sort, mixed drink shaker sort, cocktail sort , swell sort, mix sort, transport sort or party time sort

Here is source code of the C Program to execute cocktail sort. The C program is effectively incorporated and run on a Linux framework.

The program yield is likewise displayed underneath.

The mixed drink shaker sort is an enhancement for the Bubble Sort.

The improvement is fundamentally that qualities "bubble" the two bearings through the exhibit, in light of the fact that on every emphasis the cocktail sort bubble sorts once advances and once in reverse.

 Pseudocode for the calculation

capability cocktailSorting( A : rundown of sortable things )
   swap:= true
   for every I in 0 to length( A ) - 2 do
     on the off chance that A[ I ] > A[ i+1 ],//test whether the two
                               // components are off base
                               // request
       swap( A[ I ], A[ i+1 ] )//let the two components
                                // change places
       swap := true;
   in the event that swap = false,
     // we can leave the external circle here assuming no trades happened.
     break do-while loop;
   swap := false
   for every I long( A ) - 2 down to 0 do
     in the event that A[ I ] > A[ i+1 ],
       swap( A[ I ], A[ i+1 ] )
       swap := true;
 while swaped;//in the event that no components have been traded,
                // then the rundown is arranged

Cocktail sort is the variety of Bubble Sort, which crosses the rundown in the two bearings on the other hand.

 It is not the same as air pocket sort as in bubble sort navigates the rundown in the forward heading just, while this calculation crosses in forward as well as in reverse course in one emphasis.

Cocktail sort is likewise called as bi-directional bubble sort. In bubble sort, components are navigated from left to right, i.e., in one course.

In first cycle, bubble sort first moves the biggest component to its right position, then, at that point, the second-biggest component in its accurate position, etc. Be that as it may, mixed drink sort crosses in the two headings on the other hand.

As like the bubble sort, the most terrible and normal case of cocktail sort is O(n2). Cocktail sort is quicker than bubble sort.

There are 2 stages in this sorting

1. In first stage, like the bubble sort, loop through the cluster from left to right. The nearby components are analyzed, and assuming the left component is more prominent than the right one, we swap those components. The biggest component of the rundown is set toward the finish of the exhibit in the forward pass.

2.In second stage, loop through the cluster from the furthest right unsorted component to the left. The neighboring components are analyzed, and on the off chance that the right component is more modest than the left component, we trade those components. The littlest component of the rundown is put toward the start of the cluster in the regressive pass.

This loop goes on until the elements are sorted.


Best Case Complexity - It happens when there is no arranging required, i.e., the array is now arranged. The best-case time complexity of cocktail sort is O(n).

Average Case Complexity - It happens when the array components are in confused request that isn't as expected climbing and not appropriately plummeting. The average case time complexity of cocktail sort is O(n2).

Worst Case Complexity - It happens when the array elemets are expected to be arranged backward request. That implies guess you need to sort the array elements in rising request however, its components are in diving request. The worstcase time complexity of cocktail sort is O(n2).

Space Complexity – the space complexity of cocktail sorting is O(1)

 Example for cocktail sorting:


#When its fast

Mixed drink sort is at its quickest when it can arrive at an arranged rundown with an insignificant number of passes.

Since just adjoining components are traded, this implies that mixed drink sort performs best when components are actually close by their arranged positions.


Comparision with bubble sort:

1.Time complexities are same, yet Cocktail performs better compared to Bubble Sort. Normally cocktail sort is under twice quicker than bubble sort.

2.As the cocktail sort goes bidirectionally, the scope of potential trades, which is the reach to be tried, will diminish per pass, in this way decreasing the general running time marginally.

// Java program for execution of Cocktail Sort
public class CocktailSort1
	void cocktailSort(int a[])
		boolean swapped = true;
		int start = 0;
		int end = a.length;

		while (swap == true)
			// reset the traded banner on entering the
			// circle, since it very well may be valid from a
			// past cycle.
			Swapped = false;

			// circle from base to top same as
			// the cocktail sort
			for (int i = begin; i< end - 1; i++)
				int (a[i] > a[i + 1]) {
					int temp = a[i];
					a[i] = a[i + 1];
					a[i + 1] = temp;
					swapped = true;

			// in the case of nothing moved, then, at that point, exhibit is arranged.
			if (traded == bogus)

			// in any case, reset the traded banner so it
			// can be utilized in the following stage
			swapped = false;

			// move the end point back by one, in light of the fact that
			// thing toward the end is in its legitimate spot
			end = end - 1;

			// start to finish, doing the
			// same examination as in the past stage
			for (int I = end - 1; I >= begin; I- - )
				on the off chance that (a[i] > a[i + 1])
					int temp = a[i];
					a[i] = a[i + 1];
					a[i + 1] = temp;
					traded = valid;

			// increment the beginning stage, on the grounds that
			// the last stage would have moved the following
			// most modest number to its legitimate spot.
			begin = begin + 1;

	/* Prints the exhibit */
	void printArray(int a[])
		int n = a.length;
		for (int I = 0; I < n; i++)
			System.out.print(a[i] + " ");

// Driver code

public static void main(String[] args)
		CocktailSort ob = new CocktailSort();
		int a[] = { 5, 1, 4, 2, 8, 0, 2 };
		System.out.println("Sorted exhibit");