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 {insertionSort, swap} = require('./app'); | |
| //unit test | |
| test('Swaps two array entries', () => { | |
| expect(swap([1,2,3], 0, 1)).toStrictEqual([2,1,3]); | |
| }); | |
| //integration test | |
| test('Sorts array from lowest to highest - insertion sort', () => { | |
| expect (insertionSort([8,5,3,1,2,4])).toStrictEqual([1,2,3,4,5,8]); |
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
| test('Adds 2 numbers', () => { | |
| expect(add(2,2)).toBe(4); | |
| }); |
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 {insertionSort, swap} = require('./app'); | |
| //unit test | |
| test('Swaps two array entries', () => { | |
| const arr1 = [1,2,3] | |
| swap(arr1, 0, 1); | |
| expect(arr1).toStrictEqual([2,1,3]); | |
| }); | |
| //integration test |
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
| class Node { | |
| constructor(value) { | |
| this.value = value; | |
| this.next = null; | |
| } | |
| } | |
| // linkedlist class | |
| class SinglyLinkedList { | |
| constructor() { |
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
| //DOUBLY LINKED | |
| class Node { | |
| constructor(value) { | |
| this.value = value; | |
| this.prev = null; | |
| this.next = null; | |
| } | |
| } | |
| class DoublyLinkedList { | |
| constructor() { |
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 reverse(str){ | |
| // if we arrive at the final character of the string, return that character | |
| if (str.length === 1) return str[0]; | |
| //otherwise return the last character of the string, plus the reverse function called on a sliced version of that string | |
| return str[str.length - 1] + reverse(str.slice(0, str.length - 1)) | |
| } |
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 reverse(str){ | |
| // if we arrive at the final character of the string, return that character | |
| if (str.length === 1) return str[0]; | |
| //otherwise return the last character of the string, plus the reverse function called on a sliced version of that string | |
| return str[str.length - 1] + reverse(str.slice(0, str.length - 1)) | |
| } | |
| //1st Call returns "r" + reverse("ba") | |
| //2nd call "a" + reverse("b") | |
| //3rd Call "b" |
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
| //ASSUMES THE ARRAYS ARE SORTED***** | |
| const merge = (arr1, arr2) => { | |
| //establish a final sorted array to sort the elements, and a pointer for each array | |
| let sortedArray = []; | |
| let firstPointer = 0; | |
| let secondPointer = 0; | |
| //while the first pointer and the second pointer have not reached the end of their respective arrays | |
| while (firstPointer < arr1.length && secondPointer < arr2.length){ | |
| //add lesser number to array, and move pointer | |
| if(arr2[secondPointer] >= arr1[firstPointer]){ |
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 mergeSort = (arr) => { | |
| //if array is SORTED(length is 0 or 1), return array | |
| if (arr.length <= 1) return arr; | |
| //find midpoint | |
| let midpoint = Math.floor(arr.length/2); | |
| //split array by midpoint and merge sort the split arrays | |
| let arr1 = mergeSort(arr.slice(0, midpoint)) | |
| let arr2 = mergeSort(arr.slice(midpoint)) | |
| //return the merged arrays |
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 swap = (arr, index1, index2) => { | |
| let temp = arr[index1]; | |
| arr[index1] = arr[index2]; | |
| arr[index2] = temp; | |
| } | |
| //Quick Sort | |
| // setting default cases for our start and end index since we'll later need to change them when recursively calling | |
| const pivot = (arr, startIndex = 0, endIndex = arr.length - 1) => { | |
| //pick a pivot number (In this case, the 1st number in array) |