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 CocktailSort.js #130

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions JavaScript/CocktailSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const cocktailSort = (arrInput) => {
let startIndex = 0, endIndex = arrInput.length, isSwapped = true;
while (isSwapped) {
isSwapped = false;
for (let i = startIndex; i < endIndex - 1; i++) {
if (arrInput[i] > arrInput[i + 1]) {
let temp = arrInput[i];
arrInput[i] = arrInput[i + 1];
arrInput[i + 1] = temp;
isSwapped = true;
}
}
endIndex--;
if (!isSwapped) break;
isSwapped = false;
for (let i = endIndex - 1; i > startIndex; i--) {
if (arrInput[i - 1] > arrInput[i]) {
let temp = arrInput[i];
arrInput[i] = arrInput[i - 1];
arrInput[i - 1] = temp;
isSwapped = true;
}
}
startIndex++;
}
return arrInput;
}
//Cocktail Sort is a variatio of bubble sort.
//Example Usage :
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add the example as actual code

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean all the function code at one level of indentation?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ofcourse not. please use some formatter or something and fix the indentation.

// let myArr = [8, 6, -4, 1, 84, 35]
// console.log(cocktailSort(myArr))