Bubble sort in Mars Mips returns wrong results

37 views Asked by At

I've been trying to sort an array by bubble sort in Mars Mips. The returned result is indeed in the correct order, but it missed some numbers 1, 2, 7, 7, 3, 7, 4, 5, 6, 7, 7, 8, 8, 8, 7

Here is my work

.data
    arr: .word 1, 2, 7, 7, 3, 7, 4, 5, 6, 7, 7, 8, 8, 8, 7
    blank: .asciiz " "  # Newline string
.text

main:
    la $s0, arr          # Load the base address of the array
    li $t0, 0            # Initialize outer loop counter

for1: 
    beq $t0, 14, refresh
    
    
    addi $t1, $t0,1 #j = i+1
    sll $t3, $t0,2
    add $t3, $s0, $t3
    lw $t4, 0($t3) #a[i]
    addi $t0, $t0, 1
    
    j for2
for2: 
    beq $t1, 15, for1
    sll $t2, $t1,2
    add $s1, $s0, $t2
    lw $s2, 0($s1)#a[j]
    blt $s2, $t4,swap
    
    addi $t1, $t1, 1
    j for2
swap: 
        sw $s2, 0($t3)
        sw $t4, 0($s1)
        addi $t1, $t1,1
        j for2
refresh: 
    li $t0,0
    la $s0, arr 
print: 
    beq $t0, 15,exit
    
    
    li $v0,1
    lw $a0, 0($s0)
    syscall
    
    li $v0, 4
    la $a0, blank
    syscall
    addi $t0, $t0, 1
    addi $s0, $s0, 4
    j print
exit: 
    li $v0, 10            # Exit program syscall
    syscall


    

The return result here is: 1 2 6 7 7 7 7 7 7 7 7 7 8 8 8 instead of 1 2 3 4 5 6 7 7 7 7 7 7 8 8 8

0

There are 0 answers