From fcf89e914e5aa52ddcfd9ff615d6dfe469dcc8a0 Mon Sep 17 00:00:00 2001 From: ProStarGithub Date: Wed, 12 Oct 2022 00:05:16 +0530 Subject: [PATCH] Added JS Sorting Algorithms --- JAVASCRIPT/Algorithms/insertionSort.js | 13 +++++++++++++ JAVASCRIPT/Algorithms/mergeSort.js | 17 +++++++++++++++++ JAVASCRIPT/Algorithms/pallindrome.js | 20 ++++++++++++++++++++ JAVASCRIPT/Algorithms/quickSort.js | 15 +++++++++++++++ JAVASCRIPT/Algorithms/selectionSort.js | 16 ++++++++++++++++ 5 files changed, 81 insertions(+) create mode 100644 JAVASCRIPT/Algorithms/insertionSort.js create mode 100644 JAVASCRIPT/Algorithms/mergeSort.js create mode 100644 JAVASCRIPT/Algorithms/pallindrome.js create mode 100644 JAVASCRIPT/Algorithms/quickSort.js create mode 100644 JAVASCRIPT/Algorithms/selectionSort.js diff --git a/JAVASCRIPT/Algorithms/insertionSort.js b/JAVASCRIPT/Algorithms/insertionSort.js new file mode 100644 index 0000000..01ecb1b --- /dev/null +++ b/JAVASCRIPT/Algorithms/insertionSort.js @@ -0,0 +1,13 @@ +const insertionSort = (arr) => { + for (let i = 1; i < arr.length; i++) { + let currentValue = arr[i]; + let j; + for (j = i - 1; j >= 0 && arr[j] > currentValue; j--) { + arr[j + 1] = arr[j]; + } + arr[j + 1] = currentValue; + } + return arr; +}; + +// console.log(insertionSort([45, 3, 79, 14, 459, 21, 999])); // Output: [3, 14, 21, 45, 79, 459, 999]; diff --git a/JAVASCRIPT/Algorithms/mergeSort.js b/JAVASCRIPT/Algorithms/mergeSort.js new file mode 100644 index 0000000..7510fba --- /dev/null +++ b/JAVASCRIPT/Algorithms/mergeSort.js @@ -0,0 +1,17 @@ +function mergeArrays(leftArr, rightArr) { + let array = []; + while (leftArr.length && rightArr.length) { + if (leftArr[0] < rightArr[0]) array.push(leftArr.shift()); + else array.push(rightArr.shift()); + } + return [...array, ...leftArr, ...rightArr]; +} + +function mergeSort(arr) { + const middle = arr.length / 2; + if (arr.length < 2) return arr; + const leftArr = arr.splice(0, middle); + return mergeArrays(mergeSort(leftArr), mergeSort(arr)); +} + +// console.log(mergeSort([45, 3, 79, 14, 459, 21, 999])); // Output: [3, 14, 21, 45, 79, 459, 999]; diff --git a/JAVASCRIPT/Algorithms/pallindrome.js b/JAVASCRIPT/Algorithms/pallindrome.js new file mode 100644 index 0000000..a319fa8 --- /dev/null +++ b/JAVASCRIPT/Algorithms/pallindrome.js @@ -0,0 +1,20 @@ +const checkPallindrome = (value) => { + const len = value.length; + + for (let i = 0; i < len / 2; i++) { + if (value[i] !== value[len - 1 - i]) { + return false; + } + } + return true; +}; + +const checkPallindrome2 = (value) => { + const data = typeof value === "number" ? value.toString() : value; + return data === data.toString().split("").reverse().join(""); +}; + +// console.log(checkPallindrome(456789313987654)); // Output: true +// console.log(checkPallindrome2(456789313987654)); // Output: true +// console.log(checkPallindrome("456789313987654")); // Output: true +// console.log(checkPallindrome2("456789313987654")); // Output: true diff --git a/JAVASCRIPT/Algorithms/quickSort.js b/JAVASCRIPT/Algorithms/quickSort.js new file mode 100644 index 0000000..cd95118 --- /dev/null +++ b/JAVASCRIPT/Algorithms/quickSort.js @@ -0,0 +1,15 @@ +const quickSort = (arr) => { + if (arr.length <= 1) return arr; + + let pivot = arr[0], + leftArr = [], + rightArr = []; + + for (var i = 1; i < arr.length; i++) { + arr[i] < pivot ? leftArr.push(arr[i]) : rightArr.push(arr[i]); + } + + return quickSort(leftArr).concat(pivot, quickSort(rightArr)); +}; + +console.log(quickSort([45, 3, 79, 14, 459, 21, 999])); // Output: [3, 14, 21, 45, 79, 459, 999]; diff --git a/JAVASCRIPT/Algorithms/selectionSort.js b/JAVASCRIPT/Algorithms/selectionSort.js new file mode 100644 index 0000000..bb99e72 --- /dev/null +++ b/JAVASCRIPT/Algorithms/selectionSort.js @@ -0,0 +1,16 @@ +const selectionSort = (arr) => { + const len = arr.length; + + for (let i = 0; i < len; i++) { + let min = i; + for (let j = i + 1; j < len; j++) { + if (arr[j] < arr[min]) min = j; + } + if (min != i) { + [arr[i], arr[min]] = [arr[min], arr[i]]; + } + } + return arr; +}; + +// console.log(selectionSort([45, 3, 79, 14, 459, 21, 999])); // Output: [3, 14, 21, 45, 79, 459, 999];