editing file names

This commit is contained in:
itsvinayak
2020-05-06 21:07:49 +05:30
parent 1e540e9dec
commit 7ff6b8716f
24 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,44 @@
/*
* Cocktail shaker sort is a sort algorithm that is a bidirectional bubble sort
* more information: https://en.wikipedia.org/wiki/Cocktail_shaker_sort
* more information: https://en.wikipedia.org/wiki/Bubble_sort
*
*/
function cocktailShakerSort (items) {
for (let i = items.length - 1; i > 0; i--) {
let swapped = false
let temp, j
// backwards
for (j = items.length - 1; j > i; j--) {
if (items[j] < items[j - 1]) {
temp = items[j]
items[j] = items[j - 1]
items[j - 1] = temp
swapped = true
}
}
// forwards
for (j = 0; j < i; j++) {
if (items[j] > items[j + 1]) {
temp = items[j]
items[j] = items[j + 1]
items[j + 1] = temp
swapped = true
}
}
if (!swapped) {
return
}
}
}
// Implementation of cocktailShakerSort
var ar = [5, 6, 7, 8, 1, 2, 12, 14]
// Array before Sort
console.log(ar)
cocktailShakerSort(ar)
// Array after sort
console.log(ar)