-
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.
Added Insertion Sort & Updated Read Me
- Loading branch information
1 parent
893d215
commit 6d690b2
Showing
5 changed files
with
191 additions
and
3 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
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,22 @@ | ||
export default function insertionSort(array) { | ||
const auxiliaryArray = [...array]; | ||
const animations = []; | ||
insertionSortHelper(auxiliaryArray, animations); | ||
return animations; | ||
} | ||
|
||
function insertionSortHelper(array, animations) { | ||
for (let i = 1; i < array.length; i++) { | ||
let current = array[i]; | ||
let j = i - 1; | ||
|
||
animations.push([i, j, false]); | ||
|
||
while (j >= 0 && array[j] > current) { | ||
animations.push([j + 1, j, true]); | ||
array[j + 1] = array[j]; | ||
j--; | ||
} | ||
array[j + 1] = current; | ||
} | ||
} |
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,52 @@ | ||
export default function mergeSort(array) { | ||
const auxiliaryArray = [...array]; | ||
const animations = []; | ||
mergeSortHelper(array, auxiliaryArray, 0, array.length - 1, animations); | ||
return animations; | ||
} | ||
|
||
function mergeSortHelper(mainArray, auxiliaryArray, startIdx, endIdx, animations) { | ||
if (startIdx === endIdx) return; | ||
|
||
const middleIdx = Math.floor((startIdx + endIdx) / 2); | ||
|
||
// Recursively divide the array into halves | ||
mergeSortHelper(auxiliaryArray, mainArray, startIdx, middleIdx, animations); | ||
mergeSortHelper(auxiliaryArray, mainArray, middleIdx + 1, endIdx, animations); | ||
|
||
// Merge the sorted halves | ||
merge(mainArray, auxiliaryArray, startIdx, middleIdx, endIdx, animations); | ||
} | ||
|
||
function merge(mainArray, auxiliaryArray, startIdx, middleIdx, endIdx, animations) { | ||
let i = startIdx; | ||
let j = middleIdx + 1; | ||
let k = startIdx; | ||
|
||
while (i <= middleIdx && j <= endIdx) { | ||
// Animation to compare elements at indices i and j | ||
animations.push([i, j, false]); | ||
|
||
if (auxiliaryArray[i] <= auxiliaryArray[j]) { | ||
// Animation to overwrite element at k with auxiliaryArray[i] | ||
animations.push([k, auxiliaryArray[i], true]); | ||
mainArray[k++] = auxiliaryArray[i++]; | ||
} else { | ||
// Animation to overwrite element at k with auxiliaryArray[j] | ||
animations.push([k, auxiliaryArray[j], true]); | ||
mainArray[k++] = auxiliaryArray[j++]; | ||
} | ||
} | ||
|
||
while (i <= middleIdx) { | ||
// Animation to overwrite element at k with auxiliaryArray[i] | ||
animations.push([k, auxiliaryArray[i], true]); | ||
mainArray[k++] = auxiliaryArray[i++]; | ||
} | ||
|
||
while (j <= endIdx) { | ||
// Animation to overwrite element at k with auxiliaryArray[j] | ||
animations.push([k, auxiliaryArray[j], true]); | ||
mainArray[k++] = auxiliaryArray[j++]; | ||
} | ||
} |
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
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