From 624b975b5d7b60bf5fd40629f4ecf306b8c5938a Mon Sep 17 00:00:00 2001 From: Aadesh Mirajkar Date: Thu, 1 Oct 2020 14:22:02 +0530 Subject: [PATCH 1/2] added insertion sort --- JSAlgorithms/algorithms_IS.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 JSAlgorithms/algorithms_IS.js diff --git a/JSAlgorithms/algorithms_IS.js b/JSAlgorithms/algorithms_IS.js new file mode 100644 index 0000000..b00c01a --- /dev/null +++ b/JSAlgorithms/algorithms_IS.js @@ -0,0 +1,18 @@ +function insertionSort(unsortedList) { + var len = unsortedList.length; + for (var i = 1; i < len; i++) { + var tmp = unsortedList[i]; //Copy of the current element. + /*Check through the sorted part and compare with the number in tmp. If large, shift the number*/ + for (var j = i - 1; j >= 0 && (unsortedList[j] > tmp); j--) { + //Shift the number + unsortedList[j + 1] = unsortedList[j]; + } + //Insert the copied number at the correct position + //in sorted part. + unsortedList[j + 1] = tmp; + } +} + +var ul = [5, 3, 1, 2, 4]; +insertionSort(ul); +console.log(ul); \ No newline at end of file From 1dde003f037d533374136aa5ce9fa75f8c4523d2 Mon Sep 17 00:00:00 2001 From: Aadesh Mirajkar Date: Thu, 1 Oct 2020 14:37:06 +0530 Subject: [PATCH 2/2] added Binary to string conversion algorithm --- ConvertBinaryToString/index.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 ConvertBinaryToString/index.js diff --git a/ConvertBinaryToString/index.js b/ConvertBinaryToString/index.js new file mode 100644 index 0000000..ed92eca --- /dev/null +++ b/ConvertBinaryToString/index.js @@ -0,0 +1,28 @@ +function binaryAgent(str) { + let bytes = str.split(' '); + let output = ''; + + for (let k = 0; k < bytes.length; k++){ + output += String.fromCharCode(convertToDecimal(bytes[k])); + } + + return output; +} + +function convertToDecimal(byte) { + let result = 0; + + byte = byte.split(''); + + byte.reverse(); + + for (let a = 0; a < byte.length; a++){ + if (byte[a] === '1'){ + result += 2 ** a; + } + } + + return result; +} + +binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111");