Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions Data-Structures/Array/QuickSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ function getRandomInt (min, max) {
}

function Swap (arr, x, y) {
const temp = arr[x]
arr[x] = arr[y]
arr[y] = temp
[arr[x], arr[y]] = [arr[y], arr[x]]
}

// testing
Expand Down
8 changes: 2 additions & 6 deletions Sorts/BubbleSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ function bubbleSort (items) {
// Compare the adjacent positions
if (items[j] < items[j - 1]) {
// Swap the numbers
const tmp = items[j]
items[j] = items[j - 1]
items[j - 1] = tmp
[items[j], items[j - 1]] = [items[j - 1], items[j]]
}
}
}
Expand Down Expand Up @@ -47,9 +45,7 @@ function alternativeBubbleSort (arr) {
swapped = false
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i + 1]) {
const temp = arr[i]
arr[i] = arr[i + 1]
arr[i + 1] = temp
[arr[i], arr[i + 1]] = [arr[i + 1], arr[i]]
swapped = true
}
}
Expand Down
10 changes: 3 additions & 7 deletions Sorts/CocktailShakerSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,20 @@
function cocktailShakerSort (items) {
for (let i = items.length - 1; i > 0; i--) {
let swapped = false
let temp, j
let 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
[items[j], items[j - 1]] = [items[j - 1], items[j]]
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
[items[j], items[j + 1]] = [items[j + 1], items[j]]
swapped = true
}
}
Expand Down
4 changes: 1 addition & 3 deletions Sorts/CombSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ function combSort (list) {

while (gap + i < list.length) {
if (list[i] > list[i + gap]) {
const value = list[i]
list[i] = list[i + gap]
list[i + gap] = value
[list[i], list[i + gap]] = [list[i + gap], list[i]]
isSwapped = true
}
i += 1
Expand Down
4 changes: 1 addition & 3 deletions Sorts/GnomeSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ function gnomeSort (items) {
if (items[i - 1] <= items[i]) {
i++
} else {
const temp = items[i]
items[i] = items[i - 1]
items[i - 1] = temp
[items[i], items[i - 1]] = [items[i - 1], items[i]]

i = Math.max(1, i - 1)
}
Expand Down
5 changes: 1 addition & 4 deletions Sorts/Heapsort.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ function heapRoot (input, i) {
}

function swap (input, indexA, indexB) {
const temp = input[indexA]

input[indexA] = input[indexB]
input[indexB] = temp
[input[indexA], input[indexB]] = [input[indexB], input[indexA]]
}

function heapSort (input) {
Expand Down
4 changes: 1 addition & 3 deletions Sorts/SelectionSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ function selectionSort (items) {
if (min !== i) {
// After each pass, if the current min num != initial min num, exchange the position.
// Swap the numbers
var tmp = items[i]
items[i] = items[min]
items[min] = tmp
[items[i], items[min]] = [items[min], [items[i]]]
}
}
}
Expand Down