{"id":233963,"date":"2025-06-13T15:24:58","date_gmt":"2025-06-13T15:24:58","guid":{"rendered":"https:\/\/learnexams.com\/blog\/?p=233963"},"modified":"2025-06-13T15:25:01","modified_gmt":"2025-06-13T15:25:01","slug":"in-this-project-you-will-write-a-program-to-sort-an-array-using-bubble-sort","status":"publish","type":"post","link":"https:\/\/www.learnexams.com\/blog\/2025\/06\/13\/in-this-project-you-will-write-a-program-to-sort-an-array-using-bubble-sort\/","title":{"rendered":"In this project, you will write a program to sort an array using bubble sort"},"content":{"rendered":"\n<p>In this project, you will write a program to sort an array using bubble sort. Your code should provide the MIPS assembly code for the following C code. Be sure and use the exact same labels created in the C Program. In this project, you will need all of the tools we&#8217;ve already discussed including jumps\/branches, syscalls, and memory access. #include int main() { int numArray [] = {5, 3, 12, 10, 4}; int outIndex, inIndex, index, hold; int size = 5; for(index = 0; index &lt; size; index++) { printf(&#8220;%3d&#8221;, numArray[index]); } for(outIndex = 0; outIndex &lt; size &#8211; 1; outIndex++) { for(inIndex = 0; inIndex &lt; size &#8211; outIndex &#8211; 1; inIndex++) { if(numArray[inIndex] > numArray[inIndex + 1]) { hold = numArray[inIndex]; numArray[inIndex] = numArray[inIndex + 1]; numArray[inIndex + 1] = hold; } } } printf(&#8220;\\n&#8221;); for(index = 0; index &lt; size; index++) { printf(&#8220;%3d&#8221;, numArray[index]); } printf(&#8220;\\n&#8221;); return 0; } 1 NOTE* Your program should work with different array values and more\/less values<\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-ast-global-color-1-color\">The Correct Answer and Explanation is:<\/mark><\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 MIPS Assembly Code for Bubble Sort<\/h3>\n\n\n\n<p>Below is the MIPS assembly translation of the given C program:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">assemblyCopyEdit<code>.data\n    numArray: .word 5, 3, 12, 10, 4       # The unsorted array\n    size: .word 5                         # Size of array\n    newline: .asciiz \"\\n\"                # Newline for output\n\n.text\n.globl main\nmain:\n    # Load size into $t0\n    la $t1, size\n    lw $t0, 0($t1)        # $t0 = size\n\n    # Print original array\n    li $t2, 0             # index = 0\nprint_loop:\n    bge $t2, $t0, sort_outer\n    la $t3, numArray\n    mul $t4, $t2, 4\n    add $t4, $t4, $t3     # address of numArray[index]\n    lw $a0, 0($t4)\n    li $v0, 1             # syscall to print int\n    syscall\n\n    li $a0, 32            # print space\n    li $v0, 11\n    syscall\n\n    addi $t2, $t2, 1\n    j print_loop\n\n# Bubble Sort Outer Loop: outIndex = 0 to size - 2\nsort_outer:\n    li $t5, 0             # outIndex\nouter_loop:\n    sub $t6, $t0, $t5     # t6 = size - outIndex\n    addi $t6, $t6, -1     # t6 = size - outIndex - 1\n    li $t7, 0             # inIndex\ninner_loop:\n    bge $t7, $t6, outer_next\n\n    la $t8, numArray\n    mul $t9, $t7, 4\n    add $t9, $t9, $t8     # address of numArray[inIndex]\n    lw $s0, 0($t9)        # numArray[inIndex]\n\n    addi $t9, $t9, 4      # address of numArray[inIndex + 1]\n    lw $s1, 0($t9)        # numArray[inIndex + 1]\n\n    ble $s0, $s1, skip_swap\n    # Swap\n    sw $s1, -4($t9)       # numArray[inIndex] = numArray[inIndex+1]\n    sw $s0, 0($t9)        # numArray[inIndex+1] = hold\nskip_swap:\n    addi $t7, $t7, 1\n    j inner_loop\nouter_next:\n    addi $t5, $t5, 1\n    sub $t6, $t0, 1\n    blt $t5, $t6, outer_loop\n\n# Print sorted array\n    li $t2, 0\n    li $v0, 4\n    la $a0, newline\n    syscall\n\nprint_sorted:\n    bge $t2, $t0, end_program\n    la $t3, numArray\n    mul $t4, $t2, 4\n    add $t4, $t4, $t3\n    lw $a0, 0($t4)\n    li $v0, 1\n    syscall\n\n    li $a0, 32\n    li $v0, 11\n    syscall\n\n    addi $t2, $t2, 1\n    j print_sorted\n\nend_program:\n    li $v0, 4\n    la $a0, newline\n    syscall\n\n    li $v0, 0\n    jr $ra\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udcd8 Explanation (Like Textbooks)<\/h3>\n\n\n\n<p>This MIPS assembly program translates a bubble sort algorithm implemented in C. It starts by printing the original unsorted array, sorts it using the bubble sort algorithm, and prints the sorted array.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Data Section:<\/strong><br>It defines the array <code>numArray<\/code> and its <code>size<\/code>, as well as a string for newline formatting.<\/li>\n\n\n\n<li><strong>Printing Array:<\/strong><br>A loop uses syscalls to display each integer followed by a space. The index is multiplied by 4 (since each word = 4 bytes) to access the correct array element.<\/li>\n\n\n\n<li><strong>Bubble Sort Algorithm:<\/strong><br>Two nested loops implement the bubble sort. The outer loop runs <code>(size - 1)<\/code> times, and the inner loop compares adjacent elements. If the left element is greater than the right, a swap is performed using temporary registers <code>$s0<\/code> and <code>$s1<\/code>.<\/li>\n\n\n\n<li><strong>Swapping:<\/strong><br>The values are exchanged using simple <code>sw<\/code> (store word) and <code>lw<\/code> (load word) instructions with address calculations.<\/li>\n\n\n\n<li><strong>Final Output:<\/strong><br>After sorting, the sorted array is printed in the same way as the initial print.<\/li>\n<\/ul>\n\n\n\n<p>This MIPS program accurately demonstrates how high-level constructs like loops, arrays, conditionals, and I\/O translate into low-level assembly operations while maintaining program logic integrity.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/learnexams.com\/blog\/wp-content\/uploads\/2025\/06\/learnexams-banner8-376.jpeg\" alt=\"\" class=\"wp-image-233964\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In this project, you will write a program to sort an array using bubble sort. Your code should provide the MIPS assembly code for the following C code. Be sure and use the exact same labels created in the C Program. In this project, you will need all of the tools we&#8217;ve already discussed including [&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-233963","post","type-post","status-publish","format-standard","hentry","category-exams-certification"],"_links":{"self":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/233963","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=233963"}],"version-history":[{"count":0,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/233963\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/media?parent=233963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/categories?post=233963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/tags?post=233963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}