-
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.
* enable targetting test data * js insertion sort
- Loading branch information
1 parent
50a60d6
commit a97cf95
Showing
3 changed files
with
103 additions
and
6 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
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,75 @@ | ||
var fs = require('fs'); | ||
|
||
/////////////////////// | ||
// sort script start // | ||
/////////////////////// | ||
|
||
// insertion sort! | ||
// | ||
// docs: https://en.wikipedia.org/wiki/Insertion_sort | ||
// | ||
// Insertion sort steps through an input list, using it to grow an output list. | ||
// On every step, it sorts the current element into its proper location on the | ||
// output list.It continues until the input list is empty. | ||
|
||
function doSorting(inputList) { | ||
return insertionSort(inputList); | ||
} | ||
|
||
function insertionSort(inputList) { | ||
outputList = []; | ||
|
||
inputList.forEach((element, idx) => { | ||
outputList.push(element) | ||
outputList = sortElementAtIndex(outputList, element, idx) | ||
}); | ||
|
||
return outputList | ||
} | ||
|
||
function sortElementAtIndex(inputList, element, idx) { | ||
target_index = idx; | ||
outputList = inputList; | ||
|
||
while ( | ||
(target_index != 0) && | ||
(element < outputList[target_index - 1]) | ||
) { | ||
swapWithPrevious(outputList, target_index) | ||
target_index -= 1 | ||
} | ||
|
||
return outputList | ||
} | ||
|
||
function swapWithPrevious(list, idx) { | ||
tmp = list[idx - 1] | ||
list[idx - 1] = list[idx] | ||
list[idx] = tmp | ||
return list | ||
} | ||
|
||
///////////////////// | ||
// sort script end // | ||
///////////////////// | ||
|
||
// 👇🏽 copy pasted helpers | ||
|
||
// read input file | ||
let inputFilePath = process.env.INPUT_PATH; | ||
let inputString = fs.readFileSync(inputFilePath, 'utf8'); | ||
let inputStringSplit = inputString.split(/\n/); | ||
|
||
// clean input data | ||
if (inputStringSplit[inputStringSplit.length - 1] === "") { | ||
inputStringSplit = inputStringSplit.slice(0, inputStringSplit.length - 1); | ||
} | ||
|
||
// do sorting | ||
sortedData = doSorting(inputStringSplit); | ||
|
||
// write output file | ||
let outputFilePath = process.env.OUTPUT_PATH; | ||
let outputString = sortedData.join("\n"); | ||
outputString += "\n"; // trailing newline | ||
fs.writeFileSync(outputFilePath, outputString); |