From 9206e75ce9db6be76023f6c0f69dc008ef5b37c7 Mon Sep 17 00:00:00 2001 From: Selectaman <67814797+Selectaman@users.noreply.github.com> Date: Thu, 1 Oct 2020 13:04:28 +0530 Subject: [PATCH 1/2] add linear search and reverse a string algorithm --- LinearSearch/linearsearch.js | 15 +++++++++++++++ reversingString/reversestring.js | 6 ++++++ 2 files changed, 21 insertions(+) create mode 100644 LinearSearch/linearsearch.js create mode 100644 reversingString/reversestring.js diff --git a/LinearSearch/linearsearch.js b/LinearSearch/linearsearch.js new file mode 100644 index 0000000..594281f --- /dev/null +++ b/LinearSearch/linearsearch.js @@ -0,0 +1,15 @@ +function linearSearch(value, list) { + let found = false; + let position = -1; + let index = 0; + + while(!found && index < list.length) { + if(list[index] == value) { + found = true; + position = index; + } else { + index += 1; + } + } + return position; +} \ No newline at end of file diff --git a/reversingString/reversestring.js b/reversingString/reversestring.js new file mode 100644 index 0000000..6fa68fb --- /dev/null +++ b/reversingString/reversestring.js @@ -0,0 +1,6 @@ +function reverse(str) { + let reversedString = str.split("").reverse().join("") + return reversedString + }; + reverse('abcd'); + // result --> dcba \ No newline at end of file From f723a1c3cb585edb79b467d37c5ba643405c2173 Mon Sep 17 00:00:00 2001 From: Selectaman <67814797+Selectaman@users.noreply.github.com> Date: Mon, 5 Oct 2020 19:16:53 +0530 Subject: [PATCH 2/2] Added bubble sort algorithms for issue #1 --- bubble-sort/bubbleSort.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 bubble-sort/bubbleSort.js diff --git a/bubble-sort/bubbleSort.js b/bubble-sort/bubbleSort.js new file mode 100644 index 0000000..28d86e0 --- /dev/null +++ b/bubble-sort/bubbleSort.js @@ -0,0 +1,15 @@ +function bubbleSort(items) { + var length = items.length; + for (var i = (length - 1); i >= 0; i--) { + //Number of passes + for (var j = (length - i); j > 0; j--) { + //Compare the adjacent positions + if (items[j] < items[j - 1]) { + //Swap the numbers + var tmp = items[j]; + items[j] = items[j - 1]; + items[j - 1] = tmp; + } + } + } +} \ No newline at end of file