Skip to content

Commit ee8dcd8

Browse files
committed
Merge Sort comment updated and README added
1 parent 92e1999 commit ee8dcd8

File tree

4 files changed

+65
-27
lines changed

4 files changed

+65
-27
lines changed

Sorting/Merge Sort/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Merge Sort
2+
3+
**Merge sort** is a divide-and-conquer algorithm based on the idea of breaking down a list into several sub-lists until each sublist consists of a single element and merging those sublists in a manner that results into a sorted list.
4+
5+
#### Idea:
6+
- Divide the unsorted list into **N** sublists, each containing **1** element.
7+
- Take adjacent pairs of two singleton lists and merge them to form a list of 2 elements. **N** will now convert into **N/2** lists of size 2.
8+
- Repeat the process till a single sorted list of obtained.
9+
10+
While comparing two sub-lists for merging, the first element of both lists is taken into consideration. While sorting in ascending order, the element that is of a lesser value becomes a new element of the sorted list. This procedure is repeated until both the smaller sub-lists are empty and the new combined sub-list comprises all the elements of both the sub-lists.
11+
12+
![Merge Sort](./images/merge-sort.png)
13+
14+
#### A visualization on Merge Sort
15+
![Merge sort demonstration](./images/merge-sort-animation.gif)
16+
17+
*An example of merge sort. First divide the list into the smallest unit (1 element), then compare each element with the adjacent list to sort and merge the two adjacent lists. Finally all the elements are sorted and merged. (image source-wikipedia)*
18+
19+
#### Complexity Analysis
20+
- Worst Case - O(nlogn)
21+
- Average Case - O(nlogn)
22+
- Best Case - O(nlogn)
23+
24+
### More on this topic
25+
- [Merge Sort - Wikipedia](https://en.wikipedia.org/wiki/Merge_sort)
26+
- [Merge Sort - KhanAcademy Tutorial](https://www.khanacademy.org/computing/computer-science/algorithms/merge-sort/a/overview-of-merge-sort)
27+
- [Merge Sort - HackerEarch Tutorial](https://www.hackerearth.com/practice/algorithms/sorting/merge-sort/tutorial/)
28+
- [Sorting - TopCoder Tutorial](https://www.topcoder.com/community/data-science/data-science-tutorials/sorting/)
92.1 KB
Loading
37.9 KB
Loading

Sorting/Merge Sort/merge-sort.js

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,46 @@
1-
// Merge Sort Implementation
2-
function mergeSort(arr){
3-
var len = arr.length;
1+
/* Merge Sort Implementation in JavaScript */
42

5-
if(len === 1) return arr;
3+
function mergeSort(arr) {
4+
var len = arr.length;
65

7-
var mid = Math.floor(len/2);
8-
var left = arr.slice(0, mid);
9-
var right = arr.slice(mid, len);
6+
if (len === 1) return arr; //Single element left, No need to further divide
107

11-
return merge(mergeSort(left), mergeSort(right));
8+
var mid = Math.floor(len / 2);
9+
var left = arr.slice(0, mid); //grab the left half of the current list
10+
var right = arr.slice(mid, len); //grab the right half of the current list
11+
12+
return merge(mergeSort(left), mergeSort(right)); //recursively call with the list to divide and then merge them two together to get the sorted result
1213
}
1314

14-
function merge(left, right){
15-
var result = [];
16-
var leftCount = 0, rightCount = 0;
15+
//merge the given two portions of a list
16+
function merge(left, right) {
17+
var result = [];
18+
var leftCount = 0,
19+
rightCount = 0;
20+
21+
//Compare taking two elements from two list each time and set the smallest one to the new list. continue doing the same until one list is empty
22+
while (leftCount < left.length && rightCount < right.length) {
23+
if (left[leftCount] < right[rightCount])
24+
result.push(left[leftCount++]);
25+
else
26+
result.push(right[rightCount++]);
27+
}
1728

18-
while(leftCount < left.length && rightCount < right.length){
19-
if(left[leftCount] < right[rightCount])
20-
result.push(left[leftCount++]);
21-
else
22-
result.push(right[rightCount++]);
23-
}
29+
/*If a single list is empty the previous loop will be ended. But we also need to get the leftover from the another list. Following two will just grab the leftovers(if any) from the given two lists.*/
2430

25-
while(leftCount < left.length){
26-
result.push(left[leftCount++]);
27-
}
31+
//take the leftovers from the left array(if any)
32+
//Following loop can also replaced by a single line code like: result.concat(left.slice(leftCount, left.length))
33+
while (leftCount < left.length) {
34+
result.push(left[leftCount++]);
35+
}
2836

29-
while(rightCount < right.length){
30-
result.push(right[rightCount++]);
31-
}
37+
//take the leftovers from the right array(if any)
38+
//Following loop can also replaced by a single line code like: result.concat(right.slice(rightCount, right.length))
39+
while (rightCount < right.length) {
40+
result.push(right[rightCount++]);
41+
}
3242

33-
return result;
43+
return result;
3444
}
3545

3646

@@ -41,13 +51,13 @@ function merge(left, right){
4151
* Using Math.round() will give you a non-uniform distribution!
4252
*/
4353
function getRandomInt(min, max) {
44-
return Math.floor(Math.random() * (max - min + 1)) + min;
54+
return Math.floor(Math.random() * (max - min + 1)) + min;
4555
}
4656

4757
var arr = [];
4858

49-
for(var i=0;i<10;i++){ //initialize a random integer unsorted array
50-
arr.push(getRandomInt(1, 100));
59+
for (var i = 0; i < 10; i++) { //initialize a random integer unsorted array
60+
arr.push(getRandomInt(1, 100));
5161
}
5262

5363
console.log("Unsorted array: ");

0 commit comments

Comments
 (0)