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
// Creating a sample n-ary tree | |
// 1 | |
// / | \ | |
// 2 3 4 | |
// / \ | | |
// 5 6 7 | |
class NaryTreeNode { | |
constructor(value) { | |
this.value = value; |
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
// Creating a sample n-ary tree | |
// 1 | |
// / | \ | |
// 2 3 4 | |
// / \ | | |
// 5 6 7 | |
class NaryTreeNode { | |
constructor(value) { | |
this.value = value; |
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
// Root | |
// | | |
// | | |
// 11 12 | |
// | | | |
// | | | |
// 13 14 15 16 | |
// | | |
// | | |
// 17 18 |
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
// Root | |
// | | |
// | | |
// 11 12 | |
// | | | |
// | | | |
// 13 14 15 16 | |
// | | |
// | | |
// 17 18 |
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
function mergeSort(array) { | |
if (array.length <= 1) { | |
return array; // Base case: arrays with 0 or 1 elements are already sorted | |
} | |
const mid = Math.floor(array.length / 2); // Find the middle index | |
const left = array.slice(0, mid); // Divide the array into two halves | |
const right = array.slice(mid); | |
// Recursively sort both halves and then merge them |
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
function binarySearch(arr, value) { | |
let leftIndex = 0; | |
let rightIndex = arr.length - 1; | |
while(leftIndex <= rightIndex) { | |
let middleIndex = Math.floor((leftIndex + rightIndex)/2); | |
if(arr[middleIndex] === value) { | |
return middleIndex; | |
} else if(arr[middleIndex] < value) { |
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
function quickSort(arr) { | |
if(arr.length <= 1) { | |
return arr; | |
} | |
let leftArr = []; | |
let rightArr = []; | |
const pivot = arr[arr.length - 1]; | |
for (let index = 0; index < arr.length - 1; index++) { |
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
function insertionSort(arr) { | |
for(let i = 1; i < arr.length; i += 1) { | |
let temp = arr[i]; | |
for(let j = i - 1; j >= 0; j -= 1) { | |
if(arr[j] > temp) { | |
arr[j+1] = arr[j]; | |
arr[j] = temp; | |
} else { | |
// arr[j+1] = temp; |
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
function selectionSort(arr) { | |
for(let i = 0; i < arr.length - 1; i += 1) { // how many times | |
let min_index = i; | |
for(let j = i + 1; j < arr.length; j += 1) { // starts from where | |
if(arr[j] < arr[min_index]) { | |
min_index = j; | |
} | |
} |
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
function bubbleSort(arr) { | |
for(let i = 0; i < arr.length - 1; i += 1) { // how many times | |
let swapped = false; | |
for(let j = 0; j < arr.length - 1 - i; j += 1) { // till where | |
if(arr[j + 1] < arr[j]) { | |
[arr[j + 1], arr[j]] = [arr[j], arr[j + 1]]; | |
swapped = true; | |
} |