{"componentChunkName":"component---src-templates-blog-post-jsx","path":"/blogs/mergeSort","result":{"data":{"blog":{"frontmatter":{"title":"MERGE SORT","thumbnail":"blog61","date":"May 2, 2021","dsaCppCodeFile":"https://drive.google.com/file/d/1NeaBoa0c-OvYpi-qgL-3JeRvmJdIUmnT/view?usp=sharing"},"excerpt":"<div class=\"my-2 p-2\">\n              <h4>Introduction</h4>\n              <div class=\"m-2\">\n                <p>\n                  Merge Sort divides the array(or given data structure) into two\n                  halves, and again calls itself recursively until single\n                  element is left and then merges the two sorted halves. It\n                  requires a temporary array to merge and store the sorted\n                  array.\n                </p>\n                <p>\n                  This algorithm falls under Divide &amp; Conquer Technique which is\n                  divides the problem, finds the solution to smaller problem and\n                  then merges these to get the solution to the original problem.\n                  Let&apos;s look at the working of this algorithm.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Working Procedure</h4>\n              <p class=\"text-muted\">\n                Do refer the code available the end of this section to\n                understand the following theory.\n              </p>\n              <ol class=\"pl-4\">\n                <li>\n                  Consider an array,arr of n elements. Let left be the starting\n                  index and right be the ending index.\n                </li>\n                <li>\n                  <strong>Merge Sort: </strong>\n                  <ol class=\"pl-4\">\n                    <li>\n                      Recursion calls will be made if there exist more than one\n                      element in the array.\n                    </li>\n                    <li>\n                      Find the mid point of the array, mid=(left+right)/2.\n                    </li>\n                    <li>Make the first recursive call from left to mid.</li>\n                    <li>Make the second recursive call from mid+1 to right.</li>\n                    <li>\n                      Now merge this array from index left to right using\n                      <strong>merge</strong> function.\n                    </li>\n                  </ol>\n                </li>\n                <li>\n                  <strong>Merge Function:</strong>\n                  <ol class=\"pl-4\">\n                    <li>\n                      Let i=left be the start index for left subarray, j=mid+1\n                      be the start index for right subarray and k=left be the\n                      start index for temporary array.\n                    </li>\n                    <li>\n                      Loop until i&lt;=left and r&lt;=right(That is either of the\n                      subarray is looped completely).\n                    </li>\n                    <ol class=\"pl-4\">\n                      <li>\n                        If arr[i] is lesser than arr[j](if the current element\n                        in left subarray is lesser than current element in right\n                        subarray) then copy arr[i] to temp[k] and increment i,\n                        k.\n                      </li>\n                      <li>Else copy arr[j] to temp[k] and increment j, k.</li>\n                    </ol>\n                    <li>\n                      Copy the remaining elements of left subarray to temporary\n                      array(if there are any).\n                    </li>\n                    <li>\n                      Copy the remaining elements of right subarray to temporary\n                      array(if there are any).\n                    </li>\n                    <li>\n                      Finally copy the elements from temporary array to our\n                      required array(temp-&gt;arr).\n                    </li>\n                  </ol>\n                </li>\n              </ol>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Time Complexity</h4>\n              <div class=\"m-2\">\n                <p class=\"text-muted\">\n                  <strong>PS: We give random inputs because the no. of input will\n                    generally be 100 or 1000 to find the time complexity and\n                    manually giving so many inputs is cumbersome work.</strong>\n                </p>\n                <p>\n                  There exists two recursive calls in which the elements gets\n                  halved in each call. And the transfer of elements from\n                  arr-&gt;temp and at last from temp-&gt;arr will effect the time\n                  complexity. So declare a global variable count, increment this\n                  variable in all the loops present in the merge function\n                  (within the loops at step 3.2, 3.3, 3.4 &amp; 3.5 in the above\n                  working procedure).\n                </p>\n                <span class=\"gatsby-resp-image-wrapper\" style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 590px; \">\n      <a class=\"gatsby-resp-image-link\" href=\"/static/4db2f402aa06121dac7fd7f10bede9fe/ec3e2/graph.png\" style=\"display: block\" target=\"_blank\" rel=\"noopener\">\n    <span class=\"gatsby-resp-image-background-image\" style=\"padding-bottom: 75%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAYAAADkmO9VAAAACXBIWXMAABJ0AAASdAHeZh94AAABxklEQVQ4y5VUi3KCMBDM//9d8VGVChR8VIKAgK1PtnvR0Fhbp83MDbl42dvb3Knwh9U0DfI8R13XtOZhrGrbFlVVoeal4/GI3W53Y4fDAVqnmEwmiKIImc5wPp9xOp2M7fd77HhP4lqeKzl8Hg7heT2kWqMsS8NmvV6bvXwvzGoURY6QoKPRCGEQwPd9pGmKcrGAnk6xJSklNKMwxGAwNACPliTPsgzL5RIFYzWTHekzK5qiMFUqTVavzNrvDzpAkeEnQ+ugszyKC+py0Xm7NVqrYBpgFifo9fosqbgHvPpw9i01b5m8JWObQyQxgCHLTQxg7w7w6nyx4gNAYj4+uiMbZ3VW89kMSZIYDfPvgC4og9kOl1LtmZO4A3zhS61WKzx53i1DS0HYbDadVjesfwIcj8cG0HNLFmNvGa0otqvl3d4BNBoKSCqA7MNSSrKiiwnog1d3AQWssX24ZGN6fOXy7Q14fwdHBv9dW9s2NsuCoAs+jjDOOCnSn7nzlYmQvUzO5azofGl2+X1DrZWlXFUbxNcXn/EbRaFJEoQRNIPjODb+fC42NyajJ1PzwrETP2SscnvptyW96k98THkxjl/5R/C7JJ8IoIalSb7YdQAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;\"></span>\n  <img class=\"gatsby-resp-image-image\" alt=\"Merge Sort\" title=\"Merge Sort\" src=\"/static/4db2f402aa06121dac7fd7f10bede9fe/fcda8/graph.png\" srcset=\"/static/4db2f402aa06121dac7fd7f10bede9fe/12f09/graph.png 148w,\n/static/4db2f402aa06121dac7fd7f10bede9fe/e4a3f/graph.png 295w,\n/static/4db2f402aa06121dac7fd7f10bede9fe/fcda8/graph.png 590w,\n/static/4db2f402aa06121dac7fd7f10bede9fe/efc66/graph.png 885w,\n/static/4db2f402aa06121dac7fd7f10bede9fe/ec3e2/graph.png 997w\" sizes=\"(max-width: 590px) 100vw, 590px\" style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\" loading=\"lazy\">\n  </a>\n    </span>\n                <p class=\"lead\">\n                  <strong>Time Complexity: O(nlog(n))</strong>\n                </p>\n              </div>\n            </div>\n","html":"<div class=\"my-2 p-2\">\n              <h4>Introduction</h4>\n              <div class=\"m-2\">\n                <p>\n                  Merge Sort divides the array(or given data structure) into two\n                  halves, and again calls itself recursively until single\n                  element is left and then merges the two sorted halves. It\n                  requires a temporary array to merge and store the sorted\n                  array.\n                </p>\n                <p>\n                  This algorithm falls under Divide &amp; Conquer Technique which is\n                  divides the problem, finds the solution to smaller problem and\n                  then merges these to get the solution to the original problem.\n                  Let&apos;s look at the working of this algorithm.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Working Procedure</h4>\n              <p class=\"text-muted\">\n                Do refer the code available the end of this section to\n                understand the following theory.\n              </p>\n              <ol class=\"pl-4\">\n                <li>\n                  Consider an array,arr of n elements. Let left be the starting\n                  index and right be the ending index.\n                </li>\n                <li>\n                  <strong>Merge Sort: </strong>\n                  <ol class=\"pl-4\">\n                    <li>\n                      Recursion calls will be made if there exist more than one\n                      element in the array.\n                    </li>\n                    <li>\n                      Find the mid point of the array, mid=(left+right)/2.\n                    </li>\n                    <li>Make the first recursive call from left to mid.</li>\n                    <li>Make the second recursive call from mid+1 to right.</li>\n                    <li>\n                      Now merge this array from index left to right using\n                      <strong>merge</strong> function.\n                    </li>\n                  </ol>\n                </li>\n                <li>\n                  <strong>Merge Function:</strong>\n                  <ol class=\"pl-4\">\n                    <li>\n                      Let i=left be the start index for left subarray, j=mid+1\n                      be the start index for right subarray and k=left be the\n                      start index for temporary array.\n                    </li>\n                    <li>\n                      Loop until i&lt;=left and r&lt;=right(That is either of the\n                      subarray is looped completely).\n                    </li>\n                    <ol class=\"pl-4\">\n                      <li>\n                        If arr[i] is lesser than arr[j](if the current element\n                        in left subarray is lesser than current element in right\n                        subarray) then copy arr[i] to temp[k] and increment i,\n                        k.\n                      </li>\n                      <li>Else copy arr[j] to temp[k] and increment j, k.</li>\n                    </ol>\n                    <li>\n                      Copy the remaining elements of left subarray to temporary\n                      array(if there are any).\n                    </li>\n                    <li>\n                      Copy the remaining elements of right subarray to temporary\n                      array(if there are any).\n                    </li>\n                    <li>\n                      Finally copy the elements from temporary array to our\n                      required array(temp-&gt;arr).\n                    </li>\n                  </ol>\n                </li>\n              </ol>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Time Complexity</h4>\n              <div class=\"m-2\">\n                <p class=\"text-muted\">\n                  <strong>PS: We give random inputs because the no. of input will\n                    generally be 100 or 1000 to find the time complexity and\n                    manually giving so many inputs is cumbersome work.</strong>\n                </p>\n                <p>\n                  There exists two recursive calls in which the elements gets\n                  halved in each call. And the transfer of elements from\n                  arr-&gt;temp and at last from temp-&gt;arr will effect the time\n                  complexity. So declare a global variable count, increment this\n                  variable in all the loops present in the merge function\n                  (within the loops at step 3.2, 3.3, 3.4 &amp; 3.5 in the above\n                  working procedure).\n                </p>\n                <span class=\"gatsby-resp-image-wrapper\" style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 590px; \">\n      <a class=\"gatsby-resp-image-link\" href=\"/static/4db2f402aa06121dac7fd7f10bede9fe/ec3e2/graph.png\" style=\"display: block\" target=\"_blank\" rel=\"noopener\">\n    <span class=\"gatsby-resp-image-background-image\" style=\"padding-bottom: 75%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAYAAADkmO9VAAAACXBIWXMAABJ0AAASdAHeZh94AAABxklEQVQ4y5VUi3KCMBDM//9d8VGVChR8VIKAgK1PtnvR0Fhbp83MDbl42dvb3Knwh9U0DfI8R13XtOZhrGrbFlVVoeal4/GI3W53Y4fDAVqnmEwmiKIImc5wPp9xOp2M7fd77HhP4lqeKzl8Hg7heT2kWqMsS8NmvV6bvXwvzGoURY6QoKPRCGEQwPd9pGmKcrGAnk6xJSklNKMwxGAwNACPliTPsgzL5RIFYzWTHekzK5qiMFUqTVavzNrvDzpAkeEnQ+ugszyKC+py0Xm7NVqrYBpgFifo9fosqbgHvPpw9i01b5m8JWObQyQxgCHLTQxg7w7w6nyx4gNAYj4+uiMbZ3VW89kMSZIYDfPvgC4og9kOl1LtmZO4A3zhS61WKzx53i1DS0HYbDadVjesfwIcj8cG0HNLFmNvGa0otqvl3d4BNBoKSCqA7MNSSrKiiwnog1d3AQWssX24ZGN6fOXy7Q14fwdHBv9dW9s2NsuCoAs+jjDOOCnSn7nzlYmQvUzO5azofGl2+X1DrZWlXFUbxNcXn/EbRaFJEoQRNIPjODb+fC42NyajJ1PzwrETP2SscnvptyW96k98THkxjl/5R/C7JJ8IoIalSb7YdQAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;\"></span>\n  <img class=\"gatsby-resp-image-image\" alt=\"Merge Sort\" title=\"Merge Sort\" src=\"/static/4db2f402aa06121dac7fd7f10bede9fe/fcda8/graph.png\" srcset=\"/static/4db2f402aa06121dac7fd7f10bede9fe/12f09/graph.png 148w,\n/static/4db2f402aa06121dac7fd7f10bede9fe/e4a3f/graph.png 295w,\n/static/4db2f402aa06121dac7fd7f10bede9fe/fcda8/graph.png 590w,\n/static/4db2f402aa06121dac7fd7f10bede9fe/efc66/graph.png 885w,\n/static/4db2f402aa06121dac7fd7f10bede9fe/ec3e2/graph.png 997w\" sizes=\"(max-width: 590px) 100vw, 590px\" style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\" loading=\"lazy\">\n  </a>\n    </span>\n                <p class=\"lead\">\n                  <strong>Time Complexity: O(nlog(n))</strong>\n                </p>\n              </div>\n            </div>"},"thumbnail":{"childImageSharp":{"fluid":{"tracedSVG":"data:image/svg+xml,%3csvg%20xmlns='http://www.w3.org/2000/svg'%20width='400'%20height='225'%20viewBox='0%200%20400%20225'%20preserveAspectRatio='none'%3e%3cpath%20d='M49%2095h-5l-1%206c0%208-1%208-1%201%200-8-3-10-6-3l-2%203-1-3-3-4c-3%200-4%202-4%2012%200%207%200%207%203%207l2-2c0-1%200-2%201-1l2%201%202-1c1-1%201-1%201%201%200%203%2018%203%2019-1%200-2%200-2-4-2s-5-1-2-2l3-1%202-2c0-2-1-2-3-2-6%200-5-2%201-2%204-1%204-1%204-3-1-3-1-3-8-2m13%200h-4v7c-1%2011-1%2012%202%2012%202%200%202%200%202-3l1-3%202%203c1%203%201%203%204%202%202-1%202-1%200-4v-5c5-5%201-10-7-9m29%200h-5v17c0%201%200%202%206%202s7-1%207-3%200-2-4-2c-5%200-5-2-1-3%202%200%203-1%203-2%200-2-1-2-3-2-4%200-4-2%201-2%203-1%204-2%204-4l-2-2-6%201m34%200c-4%201-6%2014-2%2017%205%205%2011%200%2011-9%200-7-4-10-9-8m29%200c-5%200-5%200-5%203%200%202%201%202%203%202s2%200%202%207c-1%207%200%207%202%207s3-4%203-11c0-3%200-4%202-4l2-2c0-2-1-3-2-3l-7%201m-78%201c-4%201-6%2013-2%2016%203%203%207%202%209-1%204-4%202-9-3-7-2%200-2%201-2%203v3c-2%200-1-9%201-10h1c0%201%201%202%203%202l2-2c0-4-4-6-9-4m34%200c-3%204-2%206%201%209%204%204%204%205%200%204-3%200-5%202-2%204%207%204%2014-3%209-9l-2-2h2c2%200%202%200%202-3s-1-4-5-4l-5%201m26%201l-1%209c0%207%200%208%202%208s2%200%202-3c0-4%202-4%204%200%201%203%202%204%204%202v-8l1-6c0-3-1-4-6-4s-6%200-6%202m163%209l-1%208v7h9c8%200%209%201%209%203%200%201%200%202-2%202l-2-1c0-2-8%207-13%2015-4%208-8%2017-8%2026-1%207-1%207%205-1%204-5%206-7%209-8%206-3%207-2%207%201%200%202%200%202%201%201h3l-2-5-2-7-1-3v-3c3-2%2013-2%2013%200l-1%201h-2c0%202%203%202%204%201h1l-1%202-1%203-2%207-2%204h3c2%200%202-1%201-3%200-2%200-2%207%200%204%201%205%202%207%205l3%204%201%203%202%206%202%204%201%204c2%207-1%2010-9%208h-4v-24h-15v-3l-1-3-1%203v3h-17v7l-1%208c-2%202-2%203-1%203%202%200%203%204%202%206h-15c-2-3-2-5%201-8%204-6%202-7-2-2-3%203-4%205-4%207%200%203%200%203-9%204-9%200-9%200-9%202s1%203%205%203h5l-1%204v3h97l1-4c1-2%202-3%205-3%205-1%205-4%200-5h-3v-14c0-14-1-21-4-21l-2-2c-1-2-1-2-1%200%200%203-8%203-8-1%200-2%200-2%202-2%202%201%203%200%201-1-2-2-1-4%203-12%206-11%206-14%200-20l-5-3-1-4-1-5c-1-1-1%201-1%204v5l-3-1c-3-1-22%200-24%201-1%201-1-1-1-4v-5l-14-1-13%201m2%208v6h22v-3l1-3v-1l-1-3v-3h-22v7m-31%2027l1%209h12v-18h-13v9m3%2023l-1%205c0%203%200%204-1%203%200-4-2-3-3%200l2%204%202%203%203%201c2%200%202%200%202-3%200-2%200-3%202-3s2-1%202-4l-1-4-1%203c0%203-2%202-2-2%200-3-1-4-4-3'%20fill='%23d3d3d3'%20fill-rule='evenodd'/%3e%3c/svg%3e","aspectRatio":1.7699115044247788,"src":"/static/15abc829f473c74d4dcb0db617905c53/ee604/blog61.png","srcSet":"/static/15abc829f473c74d4dcb0db617905c53/69585/blog61.png 200w,\n/static/15abc829f473c74d4dcb0db617905c53/497c6/blog61.png 400w,\n/static/15abc829f473c74d4dcb0db617905c53/ee604/blog61.png 800w,\n/static/15abc829f473c74d4dcb0db617905c53/f3583/blog61.png 1200w,\n/static/15abc829f473c74d4dcb0db617905c53/e4d72/blog61.png 1280w","sizes":"(max-width: 800px) 100vw, 800px"}}}},"pageContext":{"blog":"mergeSort","thumbnail":"thumbnails/blog61.png"}},"staticQueryHashes":["2987289216","63159454"]}