Skip to content
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))