Skip to content

Commit

Permalink
Completed some JS algorithms (#35)
Browse files Browse the repository at this point in the history
* Added merge sort in JS

* Added bubble sort code

* added heap sort code

* added selection sort code
  • Loading branch information
namantiwari2002 authored Oct 7, 2022
1 parent 4eeffbd commit 9b3b137
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 9 deletions.
20 changes: 18 additions & 2 deletions Javascript/binarysearch.js
Original file line number Diff line number Diff line change
@@ -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];
Expand All @@ -13,4 +29,4 @@ var result = binarySearch(arr, x);
console.log(result);

// Output:
// 3
// true
9 changes: 8 additions & 1 deletion Javascript/bubblesort.js
Original file line number Diff line number Diff line change
@@ -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];
Expand Down
7 changes: 5 additions & 2 deletions Javascript/checkprime.js
Original file line number Diff line number Diff line change
@@ -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));
Expand Down
2 changes: 1 addition & 1 deletion Javascript/fibonacci.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Fibonacci sequence generator in JavaScript

function fibonacci(n) {
// your code here

}

console.log(fibonacci(10));
Expand Down
38 changes: 37 additions & 1 deletion Javascript/heapsort.js
Original file line number Diff line number Diff line change
@@ -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];
Expand Down
61 changes: 61 additions & 0 deletions Javascript/mergesort.js
Original file line number Diff line number Diff line change
@@ -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
// ]
16 changes: 14 additions & 2 deletions Javascript/selectionsort.js
Original file line number Diff line number Diff line change
@@ -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:

Expand Down

0 comments on commit 9b3b137

Please sign in to comment.