finish rest of code Course CSC 211 Description Create a al
finish rest of code
# Course: CSC 211
 # Description: Create a algorithm
 #
 #
 # for (i = 0; i < n-1; i++)
 # for (j = i+1; j < n; j++)
 # if ( a[i] > a[j])
 # swap (a[i], a[j]);
 #
 #################################################################
   jal sort                # call the sort subprogram
 PROGRAM LOGIC
   
 sort: la $t0,array    # t0 = address of a[i]
    move $t1,$t0     # t1 = address of a[j]
 add $t1,$t1,4   
lw $t2,count         # t2 = count (i counter)
 add $t2,$t2,-1    # t2 = count-1   
 move $t3,$t2    # t3 = t2 (j counter)
sloop: lw $t4,($t0)    # t4 = a[i]
 lw $t5,($t1)    # t5 = a[j]
      
 ble $t4,$t5,noswap    # if (a[i] <= a[j]) do not swap
      
 sw $t4,($t1)    # swap a[i] and a[j]
 sw $t5,($t0)   
      
 noswap: add $t1,$t1,4     # t1 = next address of a[j]
            # j count --
           # if j count != 0 go back to sort loop
       
             # t0 = next address of a[i]
# i count --
# j count = i count
# if i count = 0 sort is complete
           # t1 = the next address of a[i]
   
            # jump to sort loop
endsort:
            # return to main procedure
Solution
PROGRAM LOGIC
   
 sort: la $t0,array    # t0 = address of a[i]
    move $t1,$t0     # t1 = address of a[j]
 add $t1,$t1,4   
lw $t2,count         # t2 = count (i counter)
 add $t2,$t2,-1    # t2 = count-1   
 move $t3,$t2    # t3 = t2 (j counter)
sloop: lw $t4,($t0)    # t4 = a[i]
 lw $t5,($t1)    # t5 = a[j]
      
 ble $t4,$t5,noswap    # if (a[i] <= a[j]) do not swap
      
 sw $t4,($t1)    # swap a[i] and a[j]
 sw $t5,($t0)   
      
 noswap: add $t1,$t1,4     # t1 = next address of a[j]
            # j count --
           # if j count != 0 go back to sort loop
       
             # t0 = next address of a[i]
# i count --
# j count = i count
# if i count = 0 sort is complete
           # t1 = the next address of a[i]
   
            # jump to sort loop
endsort:
            # return to main procedure
![finish rest of code # Course: CSC 211 # Description: Create a algorithm # # # for (i = 0; i < n-1; i++) # for (j = i+1; j < n; j++) # if ( a[i] > a[j]) finish rest of code # Course: CSC 211 # Description: Create a algorithm # # # for (i = 0; i < n-1; i++) # for (j = i+1; j < n; j++) # if ( a[i] > a[j])](/WebImages/21/finish-rest-of-code-course-csc-211-description-create-a-al-1047853-1761545306-0.webp)
![finish rest of code # Course: CSC 211 # Description: Create a algorithm # # # for (i = 0; i < n-1; i++) # for (j = i+1; j < n; j++) # if ( a[i] > a[j]) finish rest of code # Course: CSC 211 # Description: Create a algorithm # # # for (i = 0; i < n-1; i++) # for (j = i+1; j < n; j++) # if ( a[i] > a[j])](/WebImages/21/finish-rest-of-code-course-csc-211-description-create-a-al-1047853-1761545306-1.webp)
