-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added merge sort in JS * Added bubble sort code * added heap sort code * added selection sort code
- Loading branch information
1 parent
4eeffbd
commit 9b3b137
Showing
7 changed files
with
144 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
// ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters