mips冒泡排序伪代码
时间: 2024-05-11 12:13:00 浏览: 97
冒泡排序代码
MIPS冒泡排序的伪代码如下:
```
# $a0 存储数组首地址,$a1 存储数组长度
BubbleSort:
addi $sp, $sp, -12 # 保存 $t0, $t1, $t2
sw $t0, 0($sp)
sw $t1, 4($sp)
sw $t2, 8($sp)
addi $t0, $a0, 0 # $t0 = &a
li $t1, 1 # $t1 = 1
OuterLoop:
bgt $t1, $a1, End # if ($t1 > len) goto End
addi $t2, $t1, 1 # $t2 = $t1 + 1
InnerLoop:
beq $t2, $a1, Next # if ($t2 == len) goto Next
sll $t3, $t1, 2 # $t3 = $t1 * 4
addu $t3, $a0, $t3 # &a[i]
lw $t4, ($t3) # t4 = a[i]
sll $t5, $t2, 2 # $t5 = $t2 * 4
addu $t5, $a0, $t5 # &a[j]
lw $t6, ($t5) # t6 = a[j]
ble $t4, $t6, Next # if (a[i] <= a[j]) goto Next
sw $t4, ($t5) # a[j] = a[i]
sw $t6, ($t3) # a[i] = t6
Next:
addi $t2, $t2, 1 # j++
j InnerLoop
End:
addi $t1, $t1, 1 # i++
j OuterLoop
lw $t0, 0($sp) # 恢复寄存器
lw $t1, 4($sp)
lw $t2, 8($sp)
addi $sp, $sp, 12
jr $ra
```
阅读全文