Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added cocktail algorithm #131

Merged
merged 1 commit into from Oct 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
78 changes: 78 additions & 0 deletions C#/cocktail_sort.cs
@@ -0,0 +1,78 @@
// Alogrithm for Cocktail Sort to sort element in ascending order

using System;

class GFG {

static void cocktailSort(int[] a)
{
bool swapped = true;
int start = 0;
int end = a.Length;

while (swapped == true) {

// reset the swapped flag on entering the
// loop, because it might be true from a
// previous iteration.
swapped = false;

// loop from bottom to top same as
// the bubble sort
for (int i = start; i < end - 1; ++i) {
if (a[i] > a[i + 1]) {
int temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
swapped = true;
}
}

// if nothing moved, then array is sorted.
if (swapped == false)
break;

// otherwise, reset the swapped flag so that it
// can be used in the next stage
swapped = false;

// move the end point back by one, because
// item at the end is in its rightful spot
end = end - 1;

// from top to bottom, doing the
// same comparison as in the previous stage
for (int i = end - 1; i >= start; i--) {
if (a[i] > a[i + 1]) {
int temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
swapped = true;
}
}

// increase the starting point, because
// the last stage would have moved the next
// smallest number to its rightful spot.
start = start + 1;
}
}

/* Prints the array */
static void printArray(int[] a)
{
int n = a.Length;
for (int i = 0; i < n; i++)
Console.Write(a[i] + " ");
Console.WriteLine();
}

// Driver method
public static void Main()
{
int[] a = { 5, 1, 4, 2, 8, 0, 2 };
cocktailSort(a);
Console.WriteLine("Sorted array ");
printArray(a);
}
}
57 changes: 57 additions & 0 deletions C++/cocktail_sort.cpp
@@ -0,0 +1,57 @@
// Alogrithm for Cocktail Sort to sort element in ascending order

#include <iostream>
#include <vector>
using namespace std;

void cocktailSort(vector<int >&v, int n){
bool sorted = true;

for(int i=0;i<n-1;i++){
if(v[i]>v[i+1]){
swap(v[i],v[i+1]);
sorted = false;
}
}

if(sorted)
return;

sorted = false;

for(int i=n-1;i>0;i--){
if(v[i]<v[i-1]){
swap(v[i],v[i-1]);
sorted = false;
}
}

if(sorted)
return;

cocktailSort(v,n);

}

int main(){
int n;
cin>>n;

if(n<0){
return 0;
}

vector<int > v(n,0);

for(int i=0;i<n;i++){
cin>>v[i];
}

cocktailSort(v,n);

for(int i=0;i<n;i++){
cout<<v[i]<<" ";
}

return 0;
}
58 changes: 58 additions & 0 deletions C/cocktail_sort.c
@@ -0,0 +1,58 @@
// Alogrithm for Cocktail Sort to sort element in ascending order

#include <stdio.h>

int main(void)
{
int i, j, start=0, n, end, flag;
printf ("Enter value of n: ");
scanf ("%d", &n);
int a[n];
printf ("Please enter %d values\n",n);
for(i=0; i<n; i++)
{
scanf("%d", &a[i]);
}
flag = 0;
end = n;
while(start <= end)
{
for(i=start; i<end-1; i++)
{
if(a[i]>a[i+1])
{
int temp =a[i];
a[i] = a[i+1];
a[i+1] = temp;
flag=1;
}
}
if(flag==0)
{
break;
}
flag=0;
end = end-1;
for(i=end; i>start-1; i--)
{
if(a[i]<a[i-1])
{
int temp = a[i];
a[i] = a[i-1];
a[i-1]= temp;
flag=1;
}
}
if(flag==0)
{
break;
}
start = start+1;
flag=0;
}
for(i=0; i<n; i++)
{
printf("%d ", a[i] );
}
return 0;
}
49 changes: 49 additions & 0 deletions Java/cocktail_sort.java
@@ -0,0 +1,49 @@
// Alogrithm for Cocktail Sort to sort element in ascending order

import java.util.Arrays;

public class CocktailSort {
public static void sort(int[] array) {
boolean swapped = true;
int start = 0;
int end = array.length;

while (swapped) {
swapped = false;
for (int i = start; i < end - 1; i++) {
if (array[i] > array[i + 1]) {
int temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
swapped = true;
}
}

if (!swapped) {
break;
}

swapped = false;
end--;

for (int i = end - 1; i >= start; i--) {
if (array[i] > array[i + 1]) {
int temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
swapped = true;
}
}

start++;
}
}
}

class CocktailSortEvaluator {
public static void main(String[] args) {
int[] array = {9,3,6,1,4};
CocktailSort.sort(array);
System.out.println(Arrays.toString(array));
}
}
30 changes: 30 additions & 0 deletions JavaScript/cocktail_sort.js
@@ -0,0 +1,30 @@
// Alogrithm for Cocktail Sort to sort element in ascending order

let arr = [2, 4, 1, 6, 8, 5, 9, 3, 4];

let is_Sorted = true;
while (is_Sorted) {
for (let i = 0; i< arr.length - 1; i++) {
if (arr[i] > arr[i + 1]) {
let temp = arr[i];
arr[i] = arr[i + 1];
arr[i+1] = temp;
is_Sorted = true;
}
}

if (!is_Sorted) break;

is_Sorted = false;

for (let j = arr.length - 1; j > 0; j--) {
if (arr[j-1] > arr[j]) {
let temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
is_Sorted = true;
}
}
}

console.log(arr)
26 changes: 26 additions & 0 deletions Python/cocktail_sort.py
@@ -0,0 +1,26 @@
# Alogrithm for Cocktail Sort to sort element in ascending order

def cocktailSort(array):
arrayLen = len(array)
check = 1
while(check == 1):
check = 0
for x in range(arrayLen-1):
if (array[x] > array[x+1]):
check = 1
array[x], array[x+1] = array[x+1], array[x]
if (x == arrayLen - 1):
break
if (check == 1):
check = 0
for y in reversed(range(arrayLen -1)):
if array[y] > array[y+1]:
check = 1
array[y], array[y+1] = array[y+1], array[y]
if (y == 0):
break

numbers = [3,45,1,3,2,8,5,0]
print("Unsorted Numbers: ", numbers)
cocktailSort(numbers)
print("Sorted Numbers: ", numbers)