forked from tsiege/Tech-Interview-Cheat-Sheet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export function insert(array: number[], rightIndex: number, value: number): void { | ||
let cursor = rightIndex | ||
for(cursor; cursor >= 0 && array[cursor] > value; cursor--) { | ||
array[cursor + 1] = array[cursor] | ||
} | ||
array[cursor + 1] = value | ||
} | ||
|
||
export function insertionSort(array: number[]): void { | ||
for (let i = 1; i < array.length; i++) { | ||
insert(array, i - 1, array[i]) | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* This challenge is purposefully set up to have `merge` be solved first | ||
* */ | ||
export function merge(array: number[], lowHalfBegin: number, boundary: number, highHalfEnd: number): void { | ||
const lowHalf: number[] = [] | ||
const highHalf: number[] = [] | ||
|
||
let cursor = lowHalfBegin | ||
// divide the content of the array into two new temporary arrays, lowHalf and highHalf | ||
for (let i = 0; cursor <= boundary; i++, cursor++) { | ||
lowHalf[i] = array[cursor] | ||
} | ||
for (let i = 0; cursor <= highHalfEnd; i++, cursor++) { | ||
highHalf[i] = array[cursor] | ||
} | ||
/** | ||
* Repeatedly compare the lowest untaken element in | ||
* lowHalf with the lowest untaken element in highHalf | ||
* and copy the lower of the two back into array | ||
*/ | ||
cursor = lowHalfBegin | ||
let lowHalfCursor = 0 | ||
let highHalfCursor = 0 | ||
while (lowHalfCursor < lowHalf.length && highHalfCursor < highHalf.length) { | ||
if (lowHalf[lowHalfCursor] < highHalf[highHalfCursor]) { | ||
array[cursor] = lowHalf[lowHalfCursor] | ||
lowHalfCursor++ | ||
} else { | ||
array[cursor] = highHalf[highHalfCursor] | ||
highHalfCursor++ | ||
} | ||
cursor++ | ||
} | ||
/** | ||
* Once one of lowHalf and highHalf has been fully copied | ||
* back into array, copy the remaining elements from the | ||
* other temporary array back into the array | ||
*/ | ||
while (lowHalfCursor < lowHalf.length) { | ||
array[cursor] = lowHalf[lowHalfCursor] | ||
cursor++ | ||
lowHalfCursor++ | ||
} | ||
while (highHalfCursor < highHalf.length) { | ||
array[cursor] = highHalf[highHalfCursor] | ||
cursor++ | ||
highHalfCursor++ | ||
} | ||
} | ||
|
||
|
||
// Takes in an array and recursively merge sorts it | ||
export function mergeSort(array: number[], lowHalfBegin = 0, highHalfEnd = array.length - 1): void { | ||
if (lowHalfBegin < highHalfEnd){ | ||
const middle = Math.floor((lowHalfBegin + highHalfEnd) / 2) | ||
mergeSort(array, lowHalfBegin, middle) | ||
mergeSort(array, middle + 1, highHalfEnd) | ||
merge(array, lowHalfBegin, middle, highHalfEnd) | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* Swaps two items in an array, changing the original array | ||
*/ | ||
export function swap(array: number[], firstIndex: number, secondIndex: number): void { | ||
var temp = array[firstIndex]; | ||
array[firstIndex] = array[secondIndex]; | ||
array[secondIndex] = temp; | ||
} | ||
/** | ||
* This function partitions given array and returns the index of the pivot. | ||
*/ | ||
export function partition(array, left, right): number { | ||
let pivotPosition = left | ||
|
||
/** | ||
* Compare array[nextToCompare] with array[right], for nextToCompare = left, left + 1,... right - 1 | ||
* maintaining that: | ||
* array[right] (last element) is the pivot | ||
* array[left..pivotPosition-1] are values known to be <= to array[right] | ||
* i.e. Values to left of pivot are less than or equal to pivot | ||
* array[pivotPosition..nextToCompare - 1] are values known to be > array[right] | ||
* i.e. Values to right of pivot are greater than pivot | ||
* array[nextToCompare..right - 1] haven't been compared with array[right] | ||
* If array[nextToCompare] > array[right], just increment nextToCompare. | ||
*/ | ||
for (let nextToCompare = left; nextToCompare < right; nextToCompare++) { | ||
/** | ||
* If array[nextToCompare] <= array[right], swap array[nextToCompare] with array[pivotPosition], | ||
* increment pivotPosition, and increment nextToCompare. | ||
*/ | ||
if (array[nextToCompare] <= array[right]) { | ||
swap(array, nextToCompare, pivotPosition) | ||
pivotPosition++ | ||
} | ||
} | ||
|
||
/** | ||
* Once all elements in array[left..right-1] | ||
* have been compared with array[right], | ||
* swap array[right] with array[pivotPosition], and return pivotPosition. | ||
*/ | ||
swap(array, right, pivotPosition) | ||
|
||
/** | ||
* pivotPosition is the new index of the pivot | ||
*/ | ||
return pivotPosition | ||
} | ||
|
||
/** | ||
* Recursively find the pivot and quickSort your array, dividing each array as you recurse | ||
*/ | ||
export function quickSort(array: number[], left: number = 0, right: number = array.length - 1): void { | ||
if (left < right) { | ||
const pivot = partition(array, left, right) | ||
quickSort(array, left, pivot - 1) | ||
quickSort(array, pivot + 1, right) | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
export function swap(array: number[], firstIndex: number, secondIndex: number): void { | ||
const temp = array[firstIndex] | ||
array[firstIndex] = array[secondIndex] | ||
array[secondIndex] = temp | ||
} | ||
|
||
export function indexOfMinimum(array: number[], startIndex: number): number { | ||
let minValue = array[startIndex] | ||
let minIndex = startIndex | ||
|
||
for(let i = minIndex + 1; i < array.length; i++) { | ||
if(array[i] < minValue) { | ||
minIndex = i | ||
minValue = array[i] | ||
} | ||
} | ||
|
||
return minIndex | ||
} | ||
|
||
export function selectionSort(array: number[]): void { | ||
for(let i = 0; i < array.length; i++) { | ||
const smallestIndex = indexOfMinimum(array, i) | ||
if (smallestIndex !== i) { | ||
swap(array, i, smallestIndex) | ||
} | ||
} | ||
} |