Created
March 20, 2020 05:16
-
-
Save voidnerd/9c71ffb260f14cff3b6436941fd33c46 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const doWhileBubbleSort = arr => { | |
let swapped; | |
do { | |
swapped = false; | |
for (let i = 0; i < arr.length; i++) { | |
let next = i + 1; | |
if (arr[i] > arr[next]) { | |
let temp = arr[i]; | |
arr[i] = arr[next]; | |
arr[next] = temp; | |
swapped = true; | |
} | |
} | |
} while (swapped); | |
return arr; | |
}; | |
const whileBubbleSort = arr => { | |
let swapped = true; | |
while (swapped) { | |
swapped = false; | |
for (let i = 0; i < arr.length; i++) { | |
let next = i + 1; | |
if (arr[i] > arr[next]) { | |
let temp = arr[i]; | |
arr[i] = arr[next]; | |
arr[next] = temp; | |
swapped = true; | |
} | |
} | |
} | |
return arr; | |
}; | |
// whileBubbleSort([2, 3, 1, 9, 5, 7]); | |
const insertionSort = arr => { | |
for (let i = 0; i < arr.length; i++) { | |
for (let j = 0; j < i; j++) { | |
if (arr[i] < arr[j]) { | |
var temp = arr.splice(i, 1); | |
arr.splice(j, 0, temp[0]); | |
} | |
} | |
} | |
return arr; | |
}; | |
// const sorted = insertionSort([2, 3, 1, 9, 5, 7]); | |
// console.log(sorted); | |
const mergeSort = arr => { | |
if (arr.length < 2) { | |
return 1; | |
} | |
const mid = Math.floor(arr.length / 2); | |
const arrA = arr.slice(0, mid); | |
const arrB = arr.slice(mid); | |
const sorter = (arrA, arrB) => { | |
const sorted = []; | |
while (arrA.length && arrB.length) { | |
if (arrA[0] < arrB[0]) { | |
sorted.push(arrA.shift()); | |
} else { | |
sorted.push(arrB.shift()); | |
} | |
} | |
return sorted.concat(arrA, arrB); | |
}; | |
console.log("arrA" + arrA); | |
console.log("arrB" + arrB); | |
console.log("arr" + arr); | |
return mergeSort(arrA); | |
}; | |
// const sorted = mergeSort([1, 9, 7, 6]); | |
// console.log("sorted ---", sorted); | |
const selectionSort = arr => { | |
for (let i = 0; i < arr.length; i++) { | |
let min = i; | |
for (let j = i + 1; j < arr.length; j++) { | |
if (arr[j] < arr[min]) { | |
min = j; | |
} | |
} | |
let temp = arr[i]; | |
arr[i] = arr[min]; | |
arr[min] = temp; | |
} | |
return arr; | |
}; | |
// const sorted = selectionSort([9, 7, 6, 3, 2]); | |
// console.log(sorted); // [ 2, 3, 6, 7, 9 ] | |
const quickSort = arr => { | |
if (arr.length < 2) { | |
return arr; | |
} | |
const index = arr.length - 1 | |
let pivot = arr[index]; | |
let left = []; | |
let right = []; | |
for (let i = 0; i < index; i++) { | |
arr[i] < pivot ? left.push(arr[i]) : right.push(arr[i]); | |
} | |
return quickSort(left).concat(pivot, quickSort(right)); | |
}; | |
const sorted = quickSort([9, 7, 6, 3, 2]); | |
console.log(sorted); // [ 2, 3, 6, 7, 9 ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment