From 1835c360e511da70d8c65ee87dd29e0b4ae69aca Mon Sep 17 00:00:00 2001 From: Naman Tiwari Date: Wed, 5 Oct 2022 22:19:20 +0530 Subject: [PATCH 1/7] Added merge sort in JS --- Javascript/mergesort.js | 61 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Javascript/mergesort.js 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 From 90864449b6a0493c25e33182e33e2df10a7309f9 Mon Sep 17 00:00:00 2001 From: Naman Tiwari Date: Wed, 5 Oct 2022 22:45:24 +0530 Subject: [PATCH 2/7] Added bubble sort code --- Javascript/binarysearch.js | 20 ++++++++++++++++++-- Javascript/bubblesort.js | 9 ++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) 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]; From e12514b45937eb9aa8b4945b1df1d7cd18b22e3d Mon Sep 17 00:00:00 2001 From: Naman Tiwari Date: Wed, 5 Oct 2022 22:59:11 +0530 Subject: [PATCH 3/7] added heap sort code --- Javascript/checkprime.js | 7 +++++-- Javascript/fibonacci.js | 2 +- Javascript/heapsort.js | 38 +++++++++++++++++++++++++++++++++++++- 3 files changed, 43 insertions(+), 4 deletions(-) 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]; From 3d08399c9074c75d4d08f19948a5b78d663d54e9 Mon Sep 17 00:00:00 2001 From: Naman Tiwari Date: Wed, 5 Oct 2022 23:02:27 +0530 Subject: [PATCH 4/7] added selection sort code --- Javascript/selectionsort.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) 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: From 52fe34dcc299ab8bcadb9f14e3afd3dbba341adc Mon Sep 17 00:00:00 2001 From: Naman Tiwari Date: Sat, 8 Oct 2022 01:19:38 +0530 Subject: [PATCH 5/7] added infix_to_postfix code --- Javascript/infixtopostfix.js | 58 ++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/Javascript/infixtopostfix.js b/Javascript/infixtopostfix.js index 8cc1002..5abafb2 100644 --- a/Javascript/infixtopostfix.js +++ b/Javascript/infixtopostfix.js @@ -1,9 +1,63 @@ // Infix to Postfix Conversion using Stack in JavaScript -function infixToPostfix(str) { - // your code here +function precedence (c) { + if (c == '^') { + return 3; + } + else if (c == '/' || c == '*') { + return 2; + } + else if (c == '+' || c == '-') { + return 1; + } + else { + return 0; + } } +function isOperand (c) { + if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) { + return 1; + } + return 0; +} + +function infixToPostfix(s){ + let st = []; + let postFix = ""; + + for (let i = 0; i < s.length; i++) { + if (isOperand(s[i])) { + postFix += s[i]; + } + else if (s[i] == '(') { + st.push('('); + } + else if (s[i] == ')') { + while (st[st.length - 1] != '(') { + postFix += st[st.length - 1]; + st.pop(); + } + st.pop(); + } + else { + while (st.length != 0 && precedence(s[i]) <= precedence(st[st.length - 1])) { + postFix += st[st.length - 1]; + st.pop(); + } + st.push(s[i]); + } + } + + while (st.length != 0) { + postFix += st[st.length - 1]; + st.pop(); + } + return postFix; +} + + + console.log(infixToPostfix("a+b*(c^d-e)^(f+g*h)-i")); // Output: From a0918224c8b284995ef55e0832ee65ab5e452f74 Mon Sep 17 00:00:00 2001 From: Naman Tiwari Date: Sat, 8 Oct 2022 01:25:19 +0530 Subject: [PATCH 6/7] added fibinacci code --- Javascript/fibonacci.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Javascript/fibonacci.js b/Javascript/fibonacci.js index 4e0941f..c7d98cd 100644 --- a/Javascript/fibonacci.js +++ b/Javascript/fibonacci.js @@ -1,7 +1,16 @@ // Fibonacci sequence generator in JavaScript function fibonacci(n) { + let array = []; + array[0] = 0; + array[1] = 1; + + for (let i = 2; i < n; i++) { + array[i] = array[i - 2] + array[i - 1]; + } + + return array; } console.log(fibonacci(10)); From bc3768660ce94db524cf2cdf4e1b87f53d2f1d42 Mon Sep 17 00:00:00 2001 From: Naman Tiwari Date: Sat, 8 Oct 2022 01:30:45 +0530 Subject: [PATCH 7/7] added code for armstring number --- Javascript/armstrong_no.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Javascript/armstrong_no.js diff --git a/Javascript/armstrong_no.js b/Javascript/armstrong_no.js new file mode 100644 index 0000000..8050205 --- /dev/null +++ b/Javascript/armstrong_no.js @@ -0,0 +1,24 @@ + +// Program to chech whether is a number is armstrong number or not in javascript + +function armstrong(num){ + + var temp=num; + var arm = 0; + while(temp>0) + { + var a=temp%10; + temp=parseInt(temp/10); + arm=arm+a*a*a; + } + + if(arm == num)return true; + + return false; + +} + +console.log(armstrong(370)); + +//Output +// true \ No newline at end of file