From fba34e6ea7d28eb59f1d0cc93634020d3d3e5db1 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 12:59:11 +0530 Subject: [PATCH 1/8] Create RecursiveLinearSearch.js --- Recursive/RecursiveLinearSearch.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Recursive/RecursiveLinearSearch.js diff --git a/Recursive/RecursiveLinearSearch.js b/Recursive/RecursiveLinearSearch.js new file mode 100644 index 0000000000..db67399026 --- /dev/null +++ b/Recursive/RecursiveLinearSearch.js @@ -0,0 +1,27 @@ +/** + * Recursive Linear Search + * + * This function searches for a key within an array using a recursive approach. + * + * @param {Array} arr - The array to search within. + * @param {*} key - The element to search for. + * @param {number} index - (Optional) The current index being checked in the array (default is 0). + * @returns {number} - The index of the element if found, or -1 if not found. + */ +function recursiveLinearSearch(arr, key, index = 0) { + // Base case: If we have searched the entire array and haven't found the key, return -1. + if (index === arr.length) { + return -1; + } + + // Base case: If the current element matches the key, return its index. + if (arr[index] === key) { + return index; + } + + // Recursive case: Continue searching in the rest of the array. + return recursiveLinearSearch(arr, key, index + 1); +} + +export { recursiveLinearSearch }; + From c865c35efd24b3332ddda02bdf1ed8dedc1f672f Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 13:01:02 +0530 Subject: [PATCH 2/8] Create RecursiveLinearSearch.test.js --- Recursive/test/RecursiveLinearSearch.test.js | 30 ++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Recursive/test/RecursiveLinearSearch.test.js diff --git a/Recursive/test/RecursiveLinearSearch.test.js b/Recursive/test/RecursiveLinearSearch.test.js new file mode 100644 index 0000000000..b30e2314ba --- /dev/null +++ b/Recursive/test/RecursiveLinearSearch.test.js @@ -0,0 +1,30 @@ +import { recursiveLinearSearch } from '../RecursiveLinearSearch'; + +describe('RecursiveLinearSearch', () => { + const arr = [2, 3, 4, 10, 25, 40, 45, 60, 100, 501, 700, 755, 800, 999]; + + it('should return index 3 for searchValue 10', () => { + const searchValue = 10; + expect(recursiveLinearSearch(arr, searchValue)).toBe(3); + }); + + it('should return index 0 for searchValue 2', () => { + const searchValue = 2; + expect(recursiveLinearSearch(arr, searchValue)).toBe(0); + }); + + it('should return index 13 for searchValue 999', () => { + const searchValue = 999; + expect(recursiveLinearSearch(arr, searchValue)).toBe(13); + }); + + it('should return -1 for searchValue 1', () => { + const searchValue = 1; + expect(recursiveLinearSearch(arr, searchValue)).toBe(-1); + }); + + it('should return -1 for searchValue 1000', () => { + const searchValue = 1000; + expect(recursiveLinearSearch(arr, searchValue)).toBe(-1); + }); +}); From 00c13afd6bfc81655f7d28ba38f98780f9420f1a Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 13:07:02 +0530 Subject: [PATCH 3/8] Update RecursiveLinearSearch.js --- Recursive/RecursiveLinearSearch.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Recursive/RecursiveLinearSearch.js b/Recursive/RecursiveLinearSearch.js index db67399026..556fdc2f8c 100644 --- a/Recursive/RecursiveLinearSearch.js +++ b/Recursive/RecursiveLinearSearch.js @@ -1,3 +1,5 @@ +// Ecplanation:- https://www.geeksforgeeks.org/recursive-c-program-linearly-search-element-given-array/ + /** * Recursive Linear Search * From 3185e53d6b97edda59bb293950fefb8dbb9c2e0d Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 13:12:30 +0530 Subject: [PATCH 4/8] Update RecursiveLinearSearch.js --- Recursive/RecursiveLinearSearch.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Recursive/RecursiveLinearSearch.js b/Recursive/RecursiveLinearSearch.js index 556fdc2f8c..473dd49e81 100644 --- a/Recursive/RecursiveLinearSearch.js +++ b/Recursive/RecursiveLinearSearch.js @@ -10,20 +10,19 @@ * @param {number} index - (Optional) The current index being checked in the array (default is 0). * @returns {number} - The index of the element if found, or -1 if not found. */ -function recursiveLinearSearch(arr, key, index = 0) { +function recursiveLinearSearch (arr, key, index = 0) { // Base case: If we have searched the entire array and haven't found the key, return -1. if (index === arr.length) { - return -1; + return -1;; } // Base case: If the current element matches the key, return its index. if (arr[index] === key) { - return index; + return index;; } // Recursive case: Continue searching in the rest of the array. - return recursiveLinearSearch(arr, key, index + 1); + return recursiveLinearSearch(arr, key, index + 1);; } -export { recursiveLinearSearch }; - +export { recursiveLinearSearch };; From 8e77a5e9c3f349b626a5499d7d36cc0060317286 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 13:15:07 +0530 Subject: [PATCH 5/8] Update RecursiveLinearSearch.js --- Recursive/RecursiveLinearSearch.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Recursive/RecursiveLinearSearch.js b/Recursive/RecursiveLinearSearch.js index 473dd49e81..3eb20df2c5 100644 --- a/Recursive/RecursiveLinearSearch.js +++ b/Recursive/RecursiveLinearSearch.js @@ -13,16 +13,16 @@ function recursiveLinearSearch (arr, key, index = 0) { // Base case: If we have searched the entire array and haven't found the key, return -1. if (index === arr.length) { - return -1;; + return -1 ;; } // Base case: If the current element matches the key, return its index. if (arr[index] === key) { - return index;; + return index ;; } // Recursive case: Continue searching in the rest of the array. - return recursiveLinearSearch(arr, key, index + 1);; + return recursiveLinearSearch(arr, key, index + 1) ;; } -export { recursiveLinearSearch };; +export { recursiveLinearSearch } ;; From a6dd54f09c212719f022a391e9abd2fe61ef8269 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 13:17:00 +0530 Subject: [PATCH 6/8] Update RecursiveLinearSearch.js --- Recursive/RecursiveLinearSearch.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Recursive/RecursiveLinearSearch.js b/Recursive/RecursiveLinearSearch.js index 3eb20df2c5..e0045b11f9 100644 --- a/Recursive/RecursiveLinearSearch.js +++ b/Recursive/RecursiveLinearSearch.js @@ -1,4 +1,4 @@ -// Ecplanation:- https://www.geeksforgeeks.org/recursive-c-program-linearly-search-element-given-array/ +// Explanation:- https://www.geeksforgeeks.org/recursive-c-program-linearly-search-element-given-array/ /** * Recursive Linear Search @@ -13,16 +13,16 @@ function recursiveLinearSearch (arr, key, index = 0) { // Base case: If we have searched the entire array and haven't found the key, return -1. if (index === arr.length) { - return -1 ;; + return -1; } // Base case: If the current element matches the key, return its index. if (arr[index] === key) { - return index ;; + return index; } // Recursive case: Continue searching in the rest of the array. - return recursiveLinearSearch(arr, key, index + 1) ;; + return recursiveLinearSearch(arr, key, index + 1); } -export { recursiveLinearSearch } ;; +export { recursiveLinearSearch }; From c1961f1814bb6e33cccfc8a14914cdf65c58e59f Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 13:19:44 +0530 Subject: [PATCH 7/8] Update RecursiveLinearSearch.js --- Recursive/RecursiveLinearSearch.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Recursive/RecursiveLinearSearch.js b/Recursive/RecursiveLinearSearch.js index e0045b11f9..9b02132527 100644 --- a/Recursive/RecursiveLinearSearch.js +++ b/Recursive/RecursiveLinearSearch.js @@ -13,16 +13,16 @@ function recursiveLinearSearch (arr, key, index = 0) { // Base case: If we have searched the entire array and haven't found the key, return -1. if (index === arr.length) { - return -1; + return -1 } // Base case: If the current element matches the key, return its index. if (arr[index] === key) { - return index; + return index } // Recursive case: Continue searching in the rest of the array. - return recursiveLinearSearch(arr, key, index + 1); + return recursiveLinearSearch(arr, key, index + 1) } -export { recursiveLinearSearch }; +export { recursiveLinearSearch } From a8391840ca61c31ef7c7b38bbe1eb591b673ca24 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Tue, 3 Oct 2023 13:21:32 +0530 Subject: [PATCH 8/8] Update RecursiveLinearSearch.test.js --- Recursive/test/RecursiveLinearSearch.test.js | 36 ++++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/Recursive/test/RecursiveLinearSearch.test.js b/Recursive/test/RecursiveLinearSearch.test.js index b30e2314ba..2a7daa25a3 100644 --- a/Recursive/test/RecursiveLinearSearch.test.js +++ b/Recursive/test/RecursiveLinearSearch.test.js @@ -1,30 +1,30 @@ -import { recursiveLinearSearch } from '../RecursiveLinearSearch'; +import { recursiveLinearSearch } from '../RecursiveLinearSearch' describe('RecursiveLinearSearch', () => { - const arr = [2, 3, 4, 10, 25, 40, 45, 60, 100, 501, 700, 755, 800, 999]; + const arr = [2, 3, 4, 10, 25, 40, 45, 60, 100, 501, 700, 755, 800, 999] it('should return index 3 for searchValue 10', () => { - const searchValue = 10; - expect(recursiveLinearSearch(arr, searchValue)).toBe(3); - }); + const searchValue = 10 + expect(recursiveLinearSearch(arr, searchValue)).toBe(3) + }) it('should return index 0 for searchValue 2', () => { - const searchValue = 2; - expect(recursiveLinearSearch(arr, searchValue)).toBe(0); - }); + const searchValue = 2 + expect(recursiveLinearSearch(arr, searchValue)).toBe(0) + }) it('should return index 13 for searchValue 999', () => { - const searchValue = 999; - expect(recursiveLinearSearch(arr, searchValue)).toBe(13); - }); + const searchValue = 999 + expect(recursiveLinearSearch(arr, searchValue)).toBe(13) + }) it('should return -1 for searchValue 1', () => { - const searchValue = 1; - expect(recursiveLinearSearch(arr, searchValue)).toBe(-1); - }); + const searchValue = 1 + expect(recursiveLinearSearch(arr, searchValue)).toBe(-1) + }) it('should return -1 for searchValue 1000', () => { - const searchValue = 1000; - expect(recursiveLinearSearch(arr, searchValue)).toBe(-1); - }); -}); + const searchValue = 1000 + expect(recursiveLinearSearch(arr, searchValue)).toBe(-1) + }) +})