From 8956d616a27fc123e032d58ff61855157b3f6fe6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Gij=C3=B3n=20Agudo?= Date: Sat, 18 Sep 2021 18:04:54 +0200 Subject: [PATCH 1/4] README.md update with instructions to execute javascript scripts --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 6d87e7f..f15dce4 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,12 @@ g++ script.cpp -o compiled/script * : +You have to have node installed. + +``` +node script.js +``` + * : *** From 12c37cb363398c51859ddf1da47d988b611b7b01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Gij=C3=B3n=20Agudo?= Date: Sat, 18 Sep 2021 18:05:25 +0200 Subject: [PATCH 2/4] Linear Search in JavaScript --- Algorithms/README.md | 2 +- Algorithms/Searching/linearSearch.js | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 Algorithms/Searching/linearSearch.js diff --git a/Algorithms/README.md b/Algorithms/README.md index 14fc2b1..305484d 100644 --- a/Algorithms/README.md +++ b/Algorithms/README.md @@ -15,7 +15,7 @@ | Algorithm | Complexity | Implementations | | --- | --- | --- | -| [Linear Search](#LinearSearch) | O(n) | | +| [Linear Search](#LinearSearch) | O(n) | | | [Binary Search](#BinarySearch) | Theta(logn) | | | [Ternary Search](#TernarySearch) | Theta(logn) | | | [Jump Search](#JumpSearch) | O(sqrt(n)) | | diff --git a/Algorithms/Searching/linearSearch.js b/Algorithms/Searching/linearSearch.js new file mode 100644 index 0000000..61ae580 --- /dev/null +++ b/Algorithms/Searching/linearSearch.js @@ -0,0 +1,27 @@ +/* Linear Search implementation in JavaScript + * O(n) + */ + +function LinearSearch(elements, x){ + // Linear Search performed on elements to found x + for (var i=0; i < elements.length; i++){ + if (elements[i] === x){ + return i; + } + } + return -1 +} + +let elements = [3, 1, 2, 4, 6, 7, 8, 49, 33, 192] +let x = 4; // element to be found + + +console.log("The elemtents are ", elements) +console.log("The element to be found is: ", x) + +let position = LinearSearch(elements, x) +if (position === -1){ + console.log("The elements has not been found") +} else{ + console.log("The element has been found at position ", position + 1) +} From aa9235cd78af8b9a83f527b4f6aead71cf37643b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Gij=C3=B3n=20Agudo?= Date: Sat, 18 Sep 2021 20:00:27 +0200 Subject: [PATCH 3/4] Binary Search in JavaScript --- Algorithms/Searching/binarySearch.js | 40 ++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Algorithms/Searching/binarySearch.js diff --git a/Algorithms/Searching/binarySearch.js b/Algorithms/Searching/binarySearch.js new file mode 100644 index 0000000..a3fb0d3 --- /dev/null +++ b/Algorithms/Searching/binarySearch.js @@ -0,0 +1,40 @@ +/* + * Binary Search implementathon in Theta(log n). + */ + +function binarySearch(elements, start, end, x){ + if (start > end){ + return -1 + } + + let middle = Math.floor((start + end) / 2) + console.log(middle) + if (elements[middle] === x){ + return middle + } + if (x < elements[middle]){ + return binarySearch(elements, start, middle - 1, x) + } else { + return binarySearch(elements, middle + 1, end, x) + } + + return -1 +} + +elements = [1, 3, 4, 5, 12, 16, 43, 39, 99] +x = 12 + +left = 0 +right = elements.length + +position = binarySearch(elements, left, right, x) + +console.log("The elemens are: ", elements) +console.log("The element to find is ", x) + +if (position === -1){ + console.log("The element has not been found") +} else { + console.log("The element has been found at position ", position + 1) +} + From 89f7238208934f573b6bf95edb4776a8c4580a4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Gij=C3=B3n=20Agudo?= Date: Sat, 18 Sep 2021 20:01:40 +0200 Subject: [PATCH 4/4] Binary Search in JavaScript (corrected typo) --- Algorithms/Searching/binarySearch.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Algorithms/Searching/binarySearch.js b/Algorithms/Searching/binarySearch.js index a3fb0d3..6bb924f 100644 --- a/Algorithms/Searching/binarySearch.js +++ b/Algorithms/Searching/binarySearch.js @@ -1,5 +1,5 @@ /* - * Binary Search implementathon in Theta(log n). + * Binary Search implementathon in JavaScript. Theta(log n). */ function binarySearch(elements, start, end, x){