mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-08 12:35:16 +08:00
Merge pull request #545 from marsonya/doctests/cocktail-shaker-sort
Improved CocktailShakerSort and added Doctests
This commit is contained in:
@ -1,40 +1,55 @@
|
|||||||
/*
|
/*
|
||||||
* Cocktail shaker sort is a sort algorithm that is a bidirectional bubble sort
|
* Cocktail Shaker Sort is an algorithm that is a Bidirectional Bubble Sort.
|
||||||
* more information: https://en.wikipedia.org/wiki/Cocktail_shaker_sort
|
* The algorithm extends bubble sort by operating in two directions.
|
||||||
* more information: https://en.wikipedia.org/wiki/Bubble_sort
|
* While it improves on bubble sort by more quickly moving items to the beginning of the list,
|
||||||
|
* it provides only marginal performance improvements.
|
||||||
|
*
|
||||||
|
* Wikipedia (Cocktail Shaker Sort): https://en.wikipedia.org/wiki/Cocktail_shaker_sort
|
||||||
|
* Wikipedia (Bubble Sort): https://en.wikipedia.org/wiki/Bubble_sort
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Doctests
|
||||||
|
*
|
||||||
|
* > cocktailShakerSort([5, 4, 1, 2, 3])
|
||||||
|
* [1, 2, 3, 4, 5]
|
||||||
|
* > cocktailShakerSort([])
|
||||||
|
* []
|
||||||
|
* > cocktailShakerSort([1, 2, 3])
|
||||||
|
* [1, 2, 3]
|
||||||
|
*/
|
||||||
|
|
||||||
function cocktailShakerSort (items) {
|
function cocktailShakerSort (items) {
|
||||||
for (let i = items.length - 1; i > 0; i--) {
|
for (let i = items.length - 1; i > 0; i--) {
|
||||||
let swapped = false
|
|
||||||
let j
|
let j
|
||||||
|
|
||||||
// backwards
|
// Backwards
|
||||||
for (j = items.length - 1; j > i; j--) {
|
for (j = items.length - 1; j > i; j--) {
|
||||||
if (items[j] < items[j - 1]) {
|
if (items[j] < items[j - 1]) {
|
||||||
[items[j], items[j - 1]] = [items[j - 1], items[j]]
|
[items[j], items[j - 1]] = [items[j - 1], items[j]]
|
||||||
swapped = true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// forwards
|
// Forwards
|
||||||
for (j = 0; j < i; j++) {
|
for (j = 0; j < i; j++) {
|
||||||
if (items[j] > items[j + 1]) {
|
if (items[j] > items[j + 1]) {
|
||||||
[items[j], items[j + 1]] = [items[j + 1], items[j]]
|
[items[j], items[j + 1]] = [items[j + 1], items[j]]
|
||||||
swapped = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!swapped) {
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Implementation of cocktailShakerSort
|
return items
|
||||||
|
}
|
||||||
|
|
||||||
var ar = [5, 6, 7, 8, 1, 2, 12, 14]
|
/**
|
||||||
// Array before Sort
|
* Implementation of Cocktail Shaker Sort
|
||||||
console.log(ar)
|
*/
|
||||||
cocktailShakerSort(ar)
|
const array = [5, 6, 7, 8, 1, 2, 12, 14]
|
||||||
// Array after sort
|
// Before Sort
|
||||||
console.log(ar)
|
console.log('\n- Before Sort | Implementation of Cocktail Shaker Sort -')
|
||||||
|
console.log(array)
|
||||||
|
// After Sort
|
||||||
|
console.log('- After Sort | Implementation of Cocktail Shaker Sort -')
|
||||||
|
console.log(cocktailShakerSort(array))
|
||||||
|
console.log('\n')
|
||||||
|
Reference in New Issue
Block a user