From 9b3b137b18b9aa6b4a971a2363b62427cf049f7a Mon Sep 17 00:00:00 2001 From: Naman Tiwari Date: Fri, 7 Oct 2022 22:10:46 +0530 Subject: [PATCH] Completed some JS algorithms (#35) * Added merge sort in JS * Added bubble sort code * added heap sort code * added selection sort code --- Javascript/binarysearch.js | 20 ++++++++++-- Javascript/bubblesort.js | 9 +++++- Javascript/checkprime.js | 7 +++-- Javascript/fibonacci.js | 2 +- Javascript/heapsort.js | 38 ++++++++++++++++++++++- Javascript/mergesort.js | 61 +++++++++++++++++++++++++++++++++++++ Javascript/selectionsort.js | 16 ++++++++-- 7 files changed, 144 insertions(+), 9 deletions(-) create mode 100644 Javascript/mergesort.js diff --git a/Javascript/binarysearch.js b/Javascript/binarysearch.js index 87de87d..d2ee5b4 100644 --- a/Javascript/binarysearch.js +++ b/Javascript/binarysearch.js @@ -1,7 +1,23 @@ // Binary Search Algorithm in Javascript function binarySearch(arr, x) { - // your code here + let start=0, end=arr.length-1; + + + while (start<=end){ + + let mid=Math.floor((start + end)/2); + + + if (arr[mid]===x) return true; + + else if (arr[mid] < x) + start = mid + 1; + else + end = mid - 1; + } + + return false; } var arr = [2, 3, 4, 10, 40]; @@ -13,4 +29,4 @@ var result = binarySearch(arr, x); console.log(result); // Output: -// 3 \ No newline at end of file +// true \ No newline at end of file diff --git a/Javascript/bubblesort.js b/Javascript/bubblesort.js index 970b718..8aeb909 100644 --- a/Javascript/bubblesort.js +++ b/Javascript/bubblesort.js @@ -1,7 +1,14 @@ // Bubble Sort in JavaScript function bubbleSort(arr) { - // your code here + for (let i = arr.length - 1; i > 0; i--) { + for (let j = 0; j < i; j++) { + if (arr[j] > arr[j + 1]) { + [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]] + } + } + } + return arr } var arr = [76, 22, 21, 96, 70, 20]; diff --git a/Javascript/checkprime.js b/Javascript/checkprime.js index 7d3f6fb..80bb7f7 100644 --- a/Javascript/checkprime.js +++ b/Javascript/checkprime.js @@ -1,8 +1,11 @@ // Check if a number is prime or not using JavaScript function isPrime(num) { - // your code here - // return true or false + for(var i = 2 ; i<=num/2 ; i++){ + if(num%i == 0)return false; + } + + return true; } console.log(isPrime(73)); diff --git a/Javascript/fibonacci.js b/Javascript/fibonacci.js index 269d9ae..4e0941f 100644 --- a/Javascript/fibonacci.js +++ b/Javascript/fibonacci.js @@ -1,7 +1,7 @@ // Fibonacci sequence generator in JavaScript function fibonacci(n) { - // your code here + } console.log(fibonacci(10)); diff --git a/Javascript/heapsort.js b/Javascript/heapsort.js index e936512..18cdd32 100644 --- a/Javascript/heapsort.js +++ b/Javascript/heapsort.js @@ -1,7 +1,43 @@ // Heap Sort in JavaScript +const maxHeapify = (arr, n, i) => { + let largest = i; + let l = 2 * i + 1; + let r = 2 * i + 2; + + if (l < n && arr[l] > arr[largest]) { + largest = l; + } + + if (r < n && arr[r] > arr[largest]) { + largest = r; + } + + + if (largest != i) { + let temp = arr[i]; + arr[i] = arr[largest]; + arr[largest] = temp; + + maxHeapify(arr, n, largest); + } + } + + function heapSort(arr) { - // your code here + var n = arr.length; + for (let i = parseInt(n / 2 - 1); i >= 0; i--) { + maxHeapify(arr, n, i); + } + + for (let i = n - 1; i >= 0; i--) { + let temp = arr[0]; + arr[0] = arr[i]; + arr[i] = temp; + maxHeapify(arr, i, 0); + } + + return arr; } var arr = [76, 22, 21, 96, 70, 20]; diff --git a/Javascript/mergesort.js b/Javascript/mergesort.js new file mode 100644 index 0000000..6c4a56b --- /dev/null +++ b/Javascript/mergesort.js @@ -0,0 +1,61 @@ +// Merge Sort Algorithm in Javascript + +function mergeSortRec(arr){ + + const length = arr.length; + + if(length === 1){ + return arr; + } + + const mid = Math.floor(length / 2); + + const left = arr.slice(0, mid); + + const right = arr.slice(mid, length); + + return merge(mergeSortRec(left), mergeSortRec(right)); + } + + function merge(leftArr, rightArr){ + + const result = []; + + let iL = 0; + let iR = 0; + + + while(iL < leftArr.length && iR < rightArr.length){ + if(leftArr[iL] < rightArr[iR]){ + result.push(leftArr[iL]); + iL++; + }else{ + result.push(rightArr[iR]); + iR++; + } + } + + + while(iL < leftArr.length){ + result.push(leftArr[iL]); + iL++; + } + + + while(iR < rightArr.length){ + result.push(rightArr[iR]); + iR++; + } + + return result; + } + +var arr = [3, 2, 10, 140, 5, 0 , 100, 38]; +var result = mergeSortRec(arr); +console.log(result); + +// Output +// [ +// 0, 2, 3, 5, +// 10, 38, 100, 140 +// ] \ No newline at end of file diff --git a/Javascript/selectionsort.js b/Javascript/selectionsort.js index d923757..40ac4fb 100644 --- a/Javascript/selectionsort.js +++ b/Javascript/selectionsort.js @@ -1,12 +1,24 @@ // Selection Sort in JavaScript function selectionSort(arr) { - // your code here + for (var i = 0; i < arr.length; i++) { + var temp = arr[i]; + for (var j = i + 1; j < arr.length; j++) { + if (temp > arr[j]) { + temp = arr[j]; + } + } + var index = arr.indexOf(temp); + var tempVal = arr[i]; + arr[i] = temp; + arr[index] = tempVal; + } } var arr = [76, 22, 21, 96, 70, 20]; -console.log(selectionSort(arr)); +selectionSort(arr); +console.log(arr); // Output: