HELP NEEDS TO BE IN MIPS PLEASE ADD COMMENTS Sort array using Bubble sort algori
ID: 3671846 • Letter: H
Question
HELP NEEDS TO BE IN MIPS
PLEASE ADD COMMENTS
Sort array using Bubble sort algorithm.
1) First ask the user how many elements of his/her array.
2) Then, read the integer array elements as input from the User.
3) Then, print out the array before the sorting
4) Apply Bubble sort algorithm on your array
5) Print out the array after the sorting
6) Print some welcome text to th user
7) Add comments to your code to describe how is your code work
Bubble Sort and MIPS guidelines
for (int i=0; i<n-1;i++){ for(int j=i+1;j<n;j++){
if a[i] > a[j]{
int temp = a[i]; a[i] = a[j];
a[j] = temp;
} }
la $t0, Array
li $t1, 0 OuterLoop:
..... InnerLoop:
lw $s0, 0($t3) add $t4, $t2, $t0 lw $s1, 0($t4) ......
sw $s0, 0($t4) sw $s1, 0($t3)
NotSwap:
.....
blt $t1, 16, OuterLoop
}
MIPS GUIDELINES
la $t0, Array
li $t1, 0 OuterLoop:
..... InnerLoop:
lw $s0, 0($t3) add $t4, $t2, $t0 lw $s1, 0($t4) ......
sw $s0, 0($t4) sw $s1, 0($t3)
NotSwap:
.....
blt $t1, 16, OuterLoop
Explanation / Answer
# Procedure: bubbleSort
# Objective: sort an array of integer elements in nondecreasing order
# Input: an address of an array of integers
# Output: an array sorted in nondecreasing order
bubbleSort:
move $t0, $a0 # move address of the array into $t0
li $s0, 1 # boolean swap = false. 0 --> false, 1 --> true
li $t1, 0 # j = 0;
li $t2, 0 # i = 0;
li $s1, 9 # array length
loop:
beqz $s0, exit # exit if swap = false
li $s0, 0 # swap = false;
addiu $t1, $t1, 1 # j++;
move $t2, $0 # i = 0;
subu $s2, $s1, $t1 # s2 = length - j
forLoop:
bge $t2, $s2, exitForLoop # if i>=s2, exit
lw $a0, 0($t0) # a0 = array[i]
lw $a1, 4($t0) # a1 = array[i+1]
ble $a0, $a1, update # if array[i]<=array[i+1] skip
sw $a1, 0($t0) # a[i+1] = a[i]
sw $a0, 4($t0) # a[i] = a[i+1]
li $s0, 1 # swap = true;
update:
addiu $t2, $t2, 1 # i++
sll $t3, $t2, 2 # t3 = i*4
addu $t0, $t0, $t3 # point to next element -->
j forLoop
exitForLoop:
j loop
exit:
jr $ra
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.