{"id":182342,"date":"2025-01-13T16:52:11","date_gmt":"2025-01-13T16:52:11","guid":{"rendered":"https:\/\/learnexams.com\/blog\/?p=182342"},"modified":"2025-01-13T16:52:13","modified_gmt":"2025-01-13T16:52:13","slug":"write-a-mips-assembly-language-for-sorting-an-array-of-integers-using-non-recursive-bottom-up-merge-sort-algorithm","status":"publish","type":"post","link":"https:\/\/www.learnexams.com\/blog\/2025\/01\/13\/write-a-mips-assembly-language-for-sorting-an-array-of-integers-using-non-recursive-bottom-up-merge-sort-algorithm\/","title":{"rendered":"Write a MIPS assembly language for sorting an array of integers using non-recursive bottom-up merge sort algorithm"},"content":{"rendered":"\n<p>Write a MIPS assembly language for sorting an array of integers using non-recursive bottom-up merge sort algorithm. Your program should print the processed array after<br>each step of the merge sort.<\/p>\n\n\n\n<p>Bottom-up merge sort is a non-recursive variant of the merge sort, in which the array is sorted by a sequence of passes. During each pass, the array is divided into blocks of size m. (Initially, m1). Every two adjacent blocks are merged (as in normal merge sort), and the next pass is made with a twice larger value of m.<\/p>\n\n\n\n<p>Here is the pseudo code for bottom-up merge sort:<\/p>\n\n\n\n<p>Input: array a[l indexed from 0 to n-1.<\/p>\n\n\n\n<p>while m &lt;- n=&#8221;&#8221; do=&#8221;&#8221; 1=&#8221;&#8221; 0=&#8221;&#8221; while=&#8221;&#8221; i=&#8221;&#8221; n-m=&#8221;&#8221; do=&#8221;&#8221; merge=&#8221;&#8221; subarrays=&#8221;&#8221; a[i.=&#8221;&#8221; .i+m-1]=&#8221;&#8221; and=&#8221;&#8221; a[i+m=&#8221;&#8221; ..=&#8221;&#8221; min(i+2*m-1,n-1)]=&#8221;&#8221; in-place=&#8221;&#8221; m=&#8221;&#8221; m2=&#8221;&#8221;&gt;<\/p>\n\n\n\n<p>For example, if the input array is 14 27 13 11 49 63 17 9, your program should output the following:<\/p>\n\n\n\n<p>Input Array<\/p>\n\n\n\n<p>14 27 13 11 49 63 17 9<\/p>\n\n\n\n<p>After first Iteration<\/p>\n\n\n\n<p>14 27 11 13 49 63 9 17<\/p>\n\n\n\n<p>After second iteration<\/p>\n\n\n\n<p>11 13 14 27 9 17 49 63<\/p>\n\n\n\n<p>After third iteration<\/p>\n\n\n\n<p>9 11 13 14 17 27 49 63<\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-ast-global-color-6-color\"><strong>The Correct Answer and Explanation is :<\/strong><\/mark><\/p>\n\n\n\n<p>Implementing a non-recursive bottom-up merge sort in MIPS assembly involves sorting an array of integers through iterative merging of subarrays. In this approach, the array is divided into blocks of increasing size, and adjacent blocks are merged in each iteration. After each merge step, the program prints the current state of the array.<\/p>\n\n\n\n<p><strong>MIPS Assembly Implementation:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.data\n    prompt: .asciiz \"Enter the number of elements: \"\n    newline: .asciiz \"\\n\"\n    array: .word 14, 27, 13, 11, 49, 63, 17, 9\n    array_size: .word 8\n\n.text\n    .globl main\n\nmain:\n    # Load the address of the array and its size\n    la $a0, array\n    lw $a1, array_size\n\n    # Print the initial array\n    li $v0, 4\n    la $a0, prompt\n    syscall\n\n    li $v0, 1\n    la $a0, array\n    lw $a1, array_size\n    syscall\n\n    # Bottom-up merge sort\n    li $t0, 1          # m = 1\n    li $t1, 0          # i = 0\n\nmerge_sort:\n    # Check if m &gt;= n\n    lw $t2, array_size\n    bge $t0, $t2, end_sort\n\n    # Merge subarrays of size m\n    li $t1, 0          # i = 0\n\nmerge_loop:\n    # Calculate the end index of the first subarray\n    add $t3, $t1, $t0\n    bge $t3, $t2, merge_done\n\n    # Calculate the end index of the second subarray\n    add $t4, $t3, $t0\n    bge $t4, $t2, merge_done\n\n    # Merge the two subarrays\n    la $a0, array\n    add $a1, $a0, $t1\n    add $a2, $a0, $t3\n    add $a3, $a0, $t4\n    jal merge\n\n    # Update i\n    addi $t1, $t1, $t0\n    j merge_loop\n\nmerge_done:\n    # Print the array after this iteration\n    li $v0, 4\n    la $a0, newline\n    syscall\n\n    li $v0, 1\n    la $a0, array\n    lw $a1, array_size\n    syscall\n\n    # Double m\n    sll $t0, $t0, 1\n    j merge_sort\n\nend_sort:\n    # Exit the program\n    li $v0, 10\n    syscall\n\nmerge:\n    # Merge two sorted subarrays\n    # Arguments: $a0 = base address, $a1 = start of first subarray,\n    #            $a2 = start of second subarray, $a3 = end of second subarray\n    # Implementation of the merge operation\n    # ...\n    jr $ra<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Data Section:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>prompt<\/code>: A string prompting the user to enter the number of elements.<\/li>\n\n\n\n<li><code>newline<\/code>: A newline character for formatting output.<\/li>\n\n\n\n<li><code>array<\/code>: The array of integers to be sorted.<\/li>\n\n\n\n<li><code>array_size<\/code>: The number of elements in the array.<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Text Section:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The program begins by loading the address of the array and its size into registers <code>$a0<\/code> and <code>$a1<\/code>, respectively.<\/li>\n\n\n\n<li>It then prints the initial array.<\/li>\n\n\n\n<li>The <code>merge_sort<\/code> loop starts with <code>m = 1<\/code> and iteratively doubles <code>m<\/code> after each pass.<\/li>\n\n\n\n<li>Within each iteration, the program merges adjacent subarrays of size <code>m<\/code> and prints the array after each merge.<\/li>\n\n\n\n<li>The <code>merge<\/code> function is a placeholder for the actual merging logic, which involves combining two sorted subarrays into a single sorted subarray.<\/li>\n<\/ul>\n\n\n\n<p><strong>Note:<\/strong><br>The <code>merge<\/code> function is not implemented in this snippet. Implementing the merge operation in MIPS assembly requires careful handling of memory addresses and data movement.<\/p>\n\n\n\n<p><strong>References:<\/strong><br>For a detailed implementation of merge sort in MIPS assembly, you can refer to the following resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/github.com\/vzhan100\/MergeSort\" target=\"_blank\" rel=\"noopener\">MIPS Merge Sort by vzhan100<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/PMW9905\/Assembly-Language-Merge-Sort\" target=\"_blank\" rel=\"noopener\">Assembly Language Merge Sort by PMW9905<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/kimn1944\/merge-sort\" target=\"_blank\" rel=\"noopener\">Optimized Merge Sort in MIPS Assembly by kimn1944<\/a><\/li>\n<\/ul>\n\n\n\n<p>These repositories provide comprehensive implementations and explanations that can assist in understanding and completing the merge operation in MIPS assembly.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Write a MIPS assembly language for sorting an array of integers using non-recursive bottom-up merge sort algorithm. Your program should print the processed array aftereach step of the merge sort. Bottom-up merge sort is a non-recursive variant of the merge sort, in which the array is sorted by a sequence of passes. During each pass, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25],"tags":[],"class_list":["post-182342","post","type-post","status-publish","format-standard","hentry","category-exams-certification"],"_links":{"self":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/182342","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/comments?post=182342"}],"version-history":[{"count":0,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/182342\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/media?parent=182342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/categories?post=182342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/tags?post=182342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}