From 5e29052e7cf23c7fad720dc4f60aad106df31113 Mon Sep 17 00:00:00 2001 From: Muhammad Minhaj Date: Fri, 17 Dec 2021 17:12:09 +0600 Subject: [PATCH 1/5] feat: Added linear search algorithm in javascript and includes my name in the contributors file --- Contributors.md | 77 ++++++++++---------- Search Algorithms/JavaScript/linearSearch.js | 17 +++++ 2 files changed, 56 insertions(+), 38 deletions(-) create mode 100644 Search Algorithms/JavaScript/linearSearch.js diff --git a/Contributors.md b/Contributors.md index bb585a2..e7e20ab 100644 --- a/Contributors.md +++ b/Contributors.md @@ -1,40 +1,41 @@ # Contributors -* [Meet Ranoliya](https://github.com/memr5/) -* [ANIMESH BARUA](https://github.com/LIGHT1210) -* [Vatsal Parsaniya](https://github.com/Vatsalparsaniya) -* [hosseinSafari](https://github.com/hosseinSafari) -* [Vishal Agarwal](https://github.com/Vishal260700) -* [Pranav B](https://github.com/Blaze2305) -* [Adityakumar Yadav](https://github.com/AdiY2j) -* [Antônio Dias](https://github.com/antoniofdias) -* [Roberto Torella](https://github.com/ganglio) -* [Satyajit Ghana](https://github.com/satyajitghana) -* [tazzzzzzz](https://github.com/tazzzzzzz) -* [Tiago Pedutti](https://github.com/tiagopedutti) -* [nn123kkk](https://github.com/nn123kkk) -* [Anway](https://github.com/anwaysomani) -* [Sneha Raina](https://github.com/SnehaR26) -* [Ricardo Stoklosa](https://github.com/RicardoStoklosa) -* [Proma Roy](https://github.com/promaroy) -* [Austin Zuniga](https://github.com/AustinZuniga) -* [Kelly Costa](https://github.com/kellydosocorro) -* [Sriram Iyer](https://github.com/ramiyer1998) -* [Miguel](https://github.com/marxlaml) -* [Sameer Kashyap](https://github.com/Sameerkash) -* [Prajwal Kumar](https://github.com/prajwal72) -* [Ahmet Oguz](https://github.com/asotronot) -* [Janeth Fernando](https://github.com/janethavi) -* [Dhira](https://github.com/ddhira123) -* [Subhojyoti Khastagir](https://github.com/Subho13) -* [DelyB](https://github.com/DelyB) -* [Sumedh Jouras](https://github.com/Sumedhj16) -* [NerdOfCode](https://github.com/nerdofcode) -* [Jay Dave](https://github.com/simson1) -* [Ahmad Emir Alfatah](https://github.com/Criptdestroyer) -* [Ayush Dubey](https://github.com/ayushdubey003) -* [Raj Pathare](https://github.com/RajPathare) -* [ehmoovin](https://github.com/moovinfrontend) -* [Muskan](https://github.com/Muskan-goyal6) -* [Tarannum](https://github.com/giTan7) -* [HCamberos](https://github.com/HCamberos) +- [Meet Ranoliya](https://github.com/memr5/) +- [ANIMESH BARUA](https://github.com/LIGHT1210) +- [Vatsal Parsaniya](https://github.com/Vatsalparsaniya) +- [hosseinSafari](https://github.com/hosseinSafari) +- [Vishal Agarwal](https://github.com/Vishal260700) +- [Pranav B](https://github.com/Blaze2305) +- [Adityakumar Yadav](https://github.com/AdiY2j) +- [Antônio Dias](https://github.com/antoniofdias) +- [Roberto Torella](https://github.com/ganglio) +- [Satyajit Ghana](https://github.com/satyajitghana) +- [tazzzzzzz](https://github.com/tazzzzzzz) +- [Tiago Pedutti](https://github.com/tiagopedutti) +- [nn123kkk](https://github.com/nn123kkk) +- [Anway](https://github.com/anwaysomani) +- [Sneha Raina](https://github.com/SnehaR26) +- [Ricardo Stoklosa](https://github.com/RicardoStoklosa) +- [Proma Roy](https://github.com/promaroy) +- [Austin Zuniga](https://github.com/AustinZuniga) +- [Kelly Costa](https://github.com/kellydosocorro) +- [Sriram Iyer](https://github.com/ramiyer1998) +- [Miguel](https://github.com/marxlaml) +- [Sameer Kashyap](https://github.com/Sameerkash) +- [Prajwal Kumar](https://github.com/prajwal72) +- [Ahmet Oguz](https://github.com/asotronot) +- [Janeth Fernando](https://github.com/janethavi) +- [Dhira](https://github.com/ddhira123) +- [Subhojyoti Khastagir](https://github.com/Subho13) +- [DelyB](https://github.com/DelyB) +- [Sumedh Jouras](https://github.com/Sumedhj16) +- [NerdOfCode](https://github.com/nerdofcode) +- [Jay Dave](https://github.com/simson1) +- [Ahmad Emir Alfatah](https://github.com/Criptdestroyer) +- [Ayush Dubey](https://github.com/ayushdubey003) +- [Raj Pathare](https://github.com/RajPathare) +- [ehmoovin](https://github.com/moovinfrontend) +- [Muskan](https://github.com/Muskan-goyal6) +- [Tarannum](https://github.com/giTan7) +- [HCamberos](https://github.com/HCamberos) +- [MuhammadMinhaj](https://github.com/MuhammadMinhaj) diff --git a/Search Algorithms/JavaScript/linearSearch.js b/Search Algorithms/JavaScript/linearSearch.js new file mode 100644 index 0000000..45496a1 --- /dev/null +++ b/Search Algorithms/JavaScript/linearSearch.js @@ -0,0 +1,17 @@ +// Linear Search +const linearSearch = (x, arr) => { + for (let i = 0; i < arr.length; i++) { + if (arr[i] === x) { + return `Congratulations! Found at position ${i}`; + } + } + return -1; +}; +const arr = [2, 4, 67, 78, 6, 23, 56, 97]; +const x = linearSearch(78, arr); +const y = linearSearch(100, arr); + +console.log(x); +// Output : Congratulations! Found at position +console.log(y); +// Output : -1 From 44ee7a8ab55d17f8758dae02dcbf956df7f193b0 Mon Sep 17 00:00:00 2001 From: Muhammad Minhaj Date: Fri, 17 Dec 2021 17:53:34 +0600 Subject: [PATCH 2/5] feat: Added binary search algorithm in javascript --- Search Algorithms/JavaScript/binarySearch.js | 26 ++++++++++++++++++++ Search Algorithms/JavaScript/linearSearch.js | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 Search Algorithms/JavaScript/binarySearch.js diff --git a/Search Algorithms/JavaScript/binarySearch.js b/Search Algorithms/JavaScript/binarySearch.js new file mode 100644 index 0000000..204287f --- /dev/null +++ b/Search Algorithms/JavaScript/binarySearch.js @@ -0,0 +1,26 @@ +// Binary Search +const binarySearch = (x, arr) => { + let left = 0; + let right = arr.length - 1; + + while (left <= right) { + const mid = Math.floor((left + right) / 2); + if (arr[mid] === x) { + return `Congratulations! Found at position ${mid}`; + } + if (arr[mid] < x) { + left = mid + 1; + } else { + right = mid - 1; + } + } + return -1; +}; +const arr = [3, 6, 8, 32, 45, 76, 89, 92, 94, 100]; +const x = binarySearch(76, arr); +const y = binarySearch(200, arr); + +console.log(x); +// Output : Congratulations! Found at position 5 +console.log(y); +// Output : -1 diff --git a/Search Algorithms/JavaScript/linearSearch.js b/Search Algorithms/JavaScript/linearSearch.js index 45496a1..9542d18 100644 --- a/Search Algorithms/JavaScript/linearSearch.js +++ b/Search Algorithms/JavaScript/linearSearch.js @@ -12,6 +12,6 @@ const x = linearSearch(78, arr); const y = linearSearch(100, arr); console.log(x); -// Output : Congratulations! Found at position +// Output : Congratulations! Found at position 3 console.log(y); // Output : -1 From fa92a3011924f6413d70a870cf898cedaf4bc082 Mon Sep 17 00:00:00 2001 From: Muhammad Minhaj Date: Fri, 17 Dec 2021 19:22:47 +0600 Subject: [PATCH 3/5] feat: Added selection sort algorithm in javascript --- .../JavaScript/selectionSort.js | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Sorting Algorithms/JavaScript/selectionSort.js diff --git a/Sorting Algorithms/JavaScript/selectionSort.js b/Sorting Algorithms/JavaScript/selectionSort.js new file mode 100644 index 0000000..1342208 --- /dev/null +++ b/Sorting Algorithms/JavaScript/selectionSort.js @@ -0,0 +1,28 @@ +// To swap the element of the array +const swap = (arr, minIndex, currentIndex) => { + const temp = arr[minIndex]; + arr[minIndex] = arr[currentIndex]; + arr[currentIndex] = temp; +}; +// Selection sort +const selectionSort = (arr) => { + let minIndex; + for (let i = 0; i < arr.length; i++) { + minIndex = i; + for (let j = i + 1; j < arr.length; j++) { + if (arr[minIndex] > arr[j]) { + minIndex = j; + } + swap(arr, minIndex, i); + } + } +}; + +const unSortedArray = [267, 1, 23, 62, 62, 76, 7, 9]; +// Before sorting the array +console.log(unSortedArray); +// Output : [267, 1, 23, 62, 62, 76, 7, 9] +selectionSort(unSortedArray); +// After sorting the array +console.log(unSortedArray); +// Output : [1, 9, 7, 62, 23, 62, 76, 267] From 5b5b361d93f39b9f121f6208d83369147fc2ed34 Mon Sep 17 00:00:00 2001 From: Muhammad Minhaj Date: Fri, 17 Dec 2021 20:19:34 +0600 Subject: [PATCH 4/5] feat: Added bubble sort algorithm in javascript --- Sorting Algorithms/JavaScript/bubbleSort.js | 25 +++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Sorting Algorithms/JavaScript/bubbleSort.js diff --git a/Sorting Algorithms/JavaScript/bubbleSort.js b/Sorting Algorithms/JavaScript/bubbleSort.js new file mode 100644 index 0000000..c3847f4 --- /dev/null +++ b/Sorting Algorithms/JavaScript/bubbleSort.js @@ -0,0 +1,25 @@ +// To swap the element of the array +const swap = (arr, index) => { + const temp = arr[index]; + arr[index] = arr[index + 1]; + arr[index + 1] = temp; +}; +// Bubble sort +const bubbleSort = (arr) => { + for (let i = 0; i < arr.length; i++) { + for (let j = 0; j < arr.length - i; j++) { + if (arr[j] > arr[j + 1]) { + swap(arr, j); + } + } + } +}; +const unSortedArray = [43, 67, 25, 98, 32, 1, 5, 8, 2, 35]; + +// Before sorting the array +console.log(unSortedArray); +// Output : [43, 67, 25, 98, 32, 1, 5, 8, 2, 35] +selectionSort(unSortedArray); +// After sorting the array +console.log(unSortedArray); +// Output : [1, 2, 5, 8, 25, 32, 35, 43, 67, 98] From d76738cc22d01bc68b65ed885b4c04861a232f12 Mon Sep 17 00:00:00 2001 From: Muhammad Minhaj Date: Fri, 17 Dec 2021 20:43:08 +0600 Subject: [PATCH 5/5] feat: Added insertion sort algorithm in javascript --- .../JavaScript/insertionSort.js | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Sorting Algorithms/JavaScript/insertionSort.js diff --git a/Sorting Algorithms/JavaScript/insertionSort.js b/Sorting Algorithms/JavaScript/insertionSort.js new file mode 100644 index 0000000..061031d --- /dev/null +++ b/Sorting Algorithms/JavaScript/insertionSort.js @@ -0,0 +1,22 @@ +// Insertion sort +const insertionSort = (arr) => { + let i, j, element; + for (i = 1; i < arr.length; i++) { + element = arr[i]; + j = i - 1; + while (j >= 0 && arr[j] > element) { + arr[j + 1] = arr[j]; + j = j - 1; + } + arr[j + 1] = element; + } +}; + +const unSortedArray = [34, 67, 32, 12, 56, 2, 65, 21, 32]; +// Before sorting the array +console.log(unSortedArray); +// Output : [34,67,32,12,56,2,65,21,32] +insertionSort(unSortedArray); +// After sorting the array +console.log(unSortedArray); +// Output : [2, 12, 21, 32, 32, 34, 56, 65, 67]