For this assignment, you will write MIPS assembly language functions to implemen
ID: 3576422 • Letter: F
Question
For this assignment, you will write MIPS assembly language functions to implement bubble sort. You should submit two versions of the function. The first method only uses loops to allow numbers to bubble up to the top. The second version uses recursion to sort one number at a time. In the first (standard) case, you will need to run a loop that keeps track of the number of sorted elements in the list. For each iteration through this loop, you will walk down the array and swap any two elements that are out of order. This forces the largest numbers to move to the back of the array and smaller numbers will slowly move towards the front of the array. In the second case, you will create a recursive function that pulls off the first number from the array until there is only one number left in the array. Since an array with only one number is sorted, the algorithm is then automatically successful. As you backtrack through the recursion, at each stage, you’ll get one number that needs to be bubble sorted into the sorted array. So, you will want a loop to shift the number down the array until it gets to where it belongs. Your program should include appropriate comments indicating what the code should be doing and what registers are being used for. After displaying the results, your program should exit cleanly.
Explanation / Answer
I am not understanding the recursion part though as it is not clear in your question. Anyways, here is the other implementation of bubble sort. .text .globl main main: la $t1,array li $s1,10 L1: beq $s1,$s2,L2 li $v0,5 syscall sw $v0,0($t1) addi $t1,$t1,4 addi $s2,$s2,1 j L1 li $s1,40 li $s2,0 li $s3,4 L2: beq $s1,$s2,printf add $t1,$t1,$s2 lw $t0,0($t1) #a[i] L3: beq $s3,$s1,incc add $t1,$t1,$s3 lw $t2,0($t1) #a[j] slt $t3,$t0,$t2 beq $t3,$0,swap L4: addi $s3,$s3,4 j L3 swap: sw $t0,0($t1) sub $t1,$t1,$s2 sw $t2,0($t1) j L4 incc: addi $s2,$s2,4 j L2 printf: la $t1,array li $t0,0 li $v0,4 la $a0,print syscall li $s2,0 li $s1,10 L5: beq $s1,$s2,out li $v0,1 lw $t0,0($t1) move $a0,$t0 syscall li $v0,4 la $a0,space syscall addi $s2,$s2,1 addi $t1,$t1,4 j L5 out: li $v0,10 syscall .end .data array: .word 0,0,0,0,0,0,0,0,0,0 print: .asciiz " the sorted array : " space: .asciiz " "
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.