From 90b5634bf5d1638bd38e3e212d16db4d49ef0135 Mon Sep 17 00:00:00 2001 From: jainaman224 Date: Sat, 4 Jun 2016 21:03:30 +0530 Subject: [PATCH 1/4] updated merge-sort --- Algorithms-Merge-Sort.md | 110 +++++++++++++++++++++++++++++++++++---- 1 file changed, 100 insertions(+), 10 deletions(-) diff --git a/Algorithms-Merge-Sort.md b/Algorithms-Merge-Sort.md index 77ca596d3..e953b8e34 100644 --- a/Algorithms-Merge-Sort.md +++ b/Algorithms-Merge-Sort.md @@ -2,7 +2,7 @@ Most modern languages have an inbuilt sort() function which automatically sorts an input array or list. Did you ever wonder how the sort function actually works on the inside?. Knowing common sorting algorithms and their implementations is the most important part of a coding interview. In this series of articles, we will look at several important sorting algorithms. How they are implemented, the time and space complexity etc. Our very first post is on Merge Sort. -## +## To learn about Merge Sort, a basic knowledge about [Recursion](http://programmers.stackexchange.com/questions/25052/in-plain-english-what-is-recursion) is a pre-requisite. Merge Sort is based on the principle of Divide and Conquer. The whole process of sorting an array of N integers can be summarized into three steps- @@ -13,36 +13,124 @@ To learn about Merge Sort, a basic knowledge about [Recursion](http://programmer The biggest advantage of using Merge sort is that the [time complexity](https://www.youtube.com/watch?v=V42FBiohc6c&list=PL2_aWCzGMAwI9HK8YPVBjElbLbI3ufctn) is only n*log(n) to sort an entire Array. It is a lot better than n^2 running time of bubble sort or insertion sort. Before we write the JavaScript code, let us understand how merge sort works with the help of a diagram. -![alt tag] (https://i67.tinypic.com/2ahe49y.png) +![Merge Sort] (https://i67.tinypic.com/2ahe49y.png) - Initially we have an array of 6 unsorted integers Arr(5, 8, 3, 9, 1, 2) - We split the array into two halves Arr1 = (5, 8, 3) and Arr2 = (9, 1, 2). - Again, we divide them into two halves: Arr3 = (5, 8) and Arr4 = (3) and Arr5 = (9, 1) and Arr6 = (2) - Again, we divide them into two halves: Arr7 = (5), Arr8 = (8), Arr9 = (9), Arr10 = (1) and Arr6 = (2) -- We will now compare the elements in these sub arrays in order to merge them. +- We will now compare the elements in these sub arrays in order to merge them. + +## Implementation + +### C++ Implementation + +```c++ +void merge(int array[], int left, int mid, int right) +{ + int i, j, k; + + // Size of left sublist + int size_left = mid - left + 1; + + // Size of right sublist + int size_right = right - mid; + + /* create temp arrays */ + int Left[size_left], Right[size_right]; + + /* Copy data to temp arrays L[] and R[] */ + for(i = 0; i < size_left; i++) + { + Left[i] = array[left+i]; + } + + for(j = 0; j < size_right; j++) + { + Right[j] = array[mid+1+j]; + } + + // Merge the temp arrays back into arr[left..right] + i = 0; // Initial index of left subarray + j = 0; // Initial index of right subarray + k = left; // Initial index of merged subarray + + while (i < size_left && j < size_right) + { + if (Left[i] <= Right[j]) + { + array[k] = Left[i]; + i++; + } + else + { + array[k] = Right[j]; + j++; + } + k++; + } + + // Copy the remaining elements of Left[] + while (i < size_left) + { + array[k] = Left[i]; + i++; + k++; + } + + // Copy the rest elements of R[] + while (j < size_right) + { + array[k] = Right[j]; + j++; + k++; + } +} +``` + +```c++ +void mergeSort(int array[], int left, int right) +{ + if(left < right) + { + int mid = (left+right)/2; + + // Sort first and second halves + mergeSort(array, left, mid); + mergeSort(array, mid+1, right); + + // Finally merge them + merge(array, left, mid, right); + } +} +``` + +:rocket: [Run Code](https://repl.it/CYVc) + +### Javascript Implementation Let's write MergeSort in JavaScript: ```javascript -function mergeSort (arr) { +function mergeSort (arr) { if (arr.length < 2) return arr; var mid = Math.floor(arr.length /2); var subLeft = mergeSort(arr.slice(0,mid)); var subRight = mergeSort(arr.slice(mid)); return merge(subLeft, subRight); - } - +} ``` -First we check the length of the array. If it is 1 then we simply return the array. This would be our base case. Else, we will find out the middle value and divide the array into two halves. We will now sort both of the halves with recursive calls to MergeSort function. +First we check the length of the array. If it is 1 then we simply return the array. This would be our base case. Else, we will find out the middle value and divide the array into two halves. We will now sort both of the halves with recursive calls to MergeSort function. + ```javascript function merge (a,b) { var result = []; while (a.length >0 && b.length >0) result.push(a[0] < b[0]? a.shift() : b.shift()); return result.concat(a.length? a : b); - } - +} ``` + When we merge the two halfs, we store the result in an auxilliary array. We will compare the starting element of left array to the starting element of right array. Whichever is lesser will be pushed into the results array and we will remove it from there respective arrays using [shift() operator](https://github.com/FreeCodeCamp/FreeCodeCamp/wiki/js-Array-prototype-shift). If we still end up with values in either of left or right array, we would simply concatenate it in the end of the result. Here is the sorted result: @@ -54,4 +142,6 @@ console.log(mergeSort(test)); >> [1, 3, 3, 5, 6, 7, 15] ``` -If you still have problem in understanding MergeSort, a [video explanation] (https://www.youtube.com/watch?v=TzeBrDU-JaY) will make it even more clear. +:rocket: [Run Code](https://repl.it/CYVd) + +If you still have problem in understanding MergeSort, a [video explanation] (https://www.youtube.com/watch?v=TzeBrDU-JaY) will make it even more clear. From 81a469e0a466e33344a04a1a32a61b44811d28ff Mon Sep 17 00:00:00 2001 From: jainaman224 Date: Sat, 4 Jun 2016 21:06:04 +0530 Subject: [PATCH 2/4] updated merge-sort --- Algorithms-Merge-Sort.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Algorithms-Merge-Sort.md b/Algorithms-Merge-Sort.md index e953b8e34..6f83e6873 100644 --- a/Algorithms-Merge-Sort.md +++ b/Algorithms-Merge-Sort.md @@ -105,7 +105,7 @@ void mergeSort(int array[], int left, int right) } ``` -:rocket: [Run Code](https://repl.it/CYVc) +:rocket: [Run Code](https://repl.it/CYVc/1) ### Javascript Implementation From 2b7626e970b987482cae6d01d6fe513589c0c17b Mon Sep 17 00:00:00 2001 From: jainaman224 Date: Sat, 4 Jun 2016 22:56:33 +0530 Subject: [PATCH 3/4] updated merge-sort --- Algorithms-Merge-Sort.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Algorithms-Merge-Sort.md b/Algorithms-Merge-Sort.md index 6f83e6873..fab25d75e 100644 --- a/Algorithms-Merge-Sort.md +++ b/Algorithms-Merge-Sort.md @@ -2,7 +2,7 @@ Most modern languages have an inbuilt sort() function which automatically sorts an input array or list. Did you ever wonder how the sort function actually works on the inside?. Knowing common sorting algorithms and their implementations is the most important part of a coding interview. In this series of articles, we will look at several important sorting algorithms. How they are implemented, the time and space complexity etc. Our very first post is on Merge Sort. -## +## To learn about Merge Sort, a basic knowledge about [Recursion](http://programmers.stackexchange.com/questions/25052/in-plain-english-what-is-recursion) is a pre-requisite. Merge Sort is based on the principle of Divide and Conquer. The whole process of sorting an array of N integers can be summarized into three steps- @@ -13,7 +13,7 @@ To learn about Merge Sort, a basic knowledge about [Recursion](http://programmer The biggest advantage of using Merge sort is that the [time complexity](https://www.youtube.com/watch?v=V42FBiohc6c&list=PL2_aWCzGMAwI9HK8YPVBjElbLbI3ufctn) is only n*log(n) to sort an entire Array. It is a lot better than n^2 running time of bubble sort or insertion sort. Before we write the JavaScript code, let us understand how merge sort works with the help of a diagram. -![Merge Sort] (https://i67.tinypic.com/2ahe49y.png) +![Merge Sort](https://i67.tinypic.com/2ahe49y.png) - Initially we have an array of 6 unsorted integers Arr(5, 8, 3, 9, 1, 2) - We split the array into two halves Arr1 = (5, 8, 3) and Arr2 = (9, 1, 2). From c458b65f82eaee7da216afb3d31857f329954f31 Mon Sep 17 00:00:00 2001 From: jainaman224 Date: Sun, 5 Jun 2016 09:04:09 +0530 Subject: [PATCH 4/4] Update --- Algorithms-Merge-Sort.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Algorithms-Merge-Sort.md b/Algorithms-Merge-Sort.md index fab25d75e..bb397a82b 100644 --- a/Algorithms-Merge-Sort.md +++ b/Algorithms-Merge-Sort.md @@ -11,9 +11,9 @@ To learn about Merge Sort, a basic knowledge about [Recursion](http://programmer - Merge the sorted halves. The biggest advantage of using Merge sort is that the [time complexity](https://www.youtube.com/watch?v=V42FBiohc6c&list=PL2_aWCzGMAwI9HK8YPVBjElbLbI3ufctn) is only n*log(n) to sort an entire Array. It is a lot better than n^2 running time of bubble sort or insertion sort. -Before we write the JavaScript code, let us understand how merge sort works with the help of a diagram. +Before we write code, let us understand how merge sort works with the help of a diagram. -![Merge Sort](https://i67.tinypic.com/2ahe49y.png) +![Merge Sort](https://i.imgur.com/ac98ccu.png) - Initially we have an array of 6 unsorted integers Arr(5, 8, 3, 9, 1, 2) - We split the array into two halves Arr1 = (5, 8, 3) and Arr2 = (9, 1, 2). @@ -120,6 +120,7 @@ function mergeSort (arr) { return merge(subLeft, subRight); } ``` + First we check the length of the array. If it is 1 then we simply return the array. This would be our base case. Else, we will find out the middle value and divide the array into two halves. We will now sort both of the halves with recursive calls to MergeSort function. ```javascript