Question

In: Computer Science

Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers...

Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers by repeatedly calling a “swap” subroutine.

The original unsorted list of integers should be received from the keyboard input. Your program should first prompt the user “Please input an integer for the number of elements:”. After the user enters a number and return, your program outputs message “Now input each element and then a return:”. For example, if the user enters 5 as the number of integers, and then the sequence of integers one by one: 1,-2, 3, 3, -4, it should display “The elements are sorted as: -4, -2, 1, 3, 3” (the output sequence of integers should be either space-separated or comma-separated).

The final sorted list (increasing order) should be stored in the data area, that starts with the label "list:".

Solutions

Expert Solution

.data
array: .space 100   
size: .asciiz "Enter size: "
int: .asciiz "Enter int: "
final: .asciiz "\nSorted: "

.text

main:

la $a0,size                       
li $v0,4
syscall

li $v0,5
syscall

move $s1, $v0                      
sub $s1,$s1,1                       

addint:

la $a0,int                     
li $v0,4
syscall

li $v0,5        
syscall

move $t3,$v0                        
add $t1,$zero,$zero                 
sll $t1,$t0,2                       

sw $t3,array ( $t1 )                
addi $t0,$t0,1                      
slt $t1,$s1,$t0                     
beq $t1,$zero,addint                

la $a0,array                        
addi $a1,$s1,1                      

jal bubble_sort                            

la $a0,final 
li $v0,4
syscall

la $t0,array                        
li $t1,0                            

print:
lw $a0,0($t0)                       
li $v0,1
syscall

addi $t0,$t0,4                      
addi $t1,$t1,1                      
slt $t2,$s1,$t1                     
beq $t2,$zero,print                 

li $v0,10                           
syscall

bubble_sort:
li $t0,0                            

loop1:

addi $t0,$t0,1                      
bgt $t0,$a1,end                  
add $t1,$a1,$zero                   

loop2:

bge $t0,$t1,loop1                  

subi $t1,$t1,1                      

sll $t4, $t1, 2                     
subi $t3, $t4, 4                    

add $t4,$t4,$a0                     
add $t3,$t3,$a0                     
lw $t5,0($t4)
lw $t6,0($t3)

swap:
bgt $t5,$t6,loop2                   
sw $t5,0($t3)                       
sw $t6,0($t4)
j loop2

end:
jr $ra

Related Solutions

Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their working.
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list instead of arrays. You can use any kind of a linked structure, such as single, double, circular lists, stacks and/or queues. You can populate your list from an explicitly defined array in your program. HINT: You will not be using low, middle and high anymore. For finding the middle point, traverse through the linked list while keeping count of the number of nodes. Break...
I have this program, it sorts a file using shell sort and quick sort then prints...
I have this program, it sorts a file using shell sort and quick sort then prints amount of comparisons and swaps. I need to add the insertion algorithm. Here is the code. The language is Java. import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class Sort {    public static int numOfComps = 0,numOfSwaps = 0;     public static void main(String[] args)    {         try{        Scanner scanner = new Scanner(new File("a.txt"));//your text file here          ...
I have this program, it sorts a file using shell sort and quick sort then prints...
I have this program, it sorts a file using shell sort and quick sort then prints amount of comparisons and swaps. I need to add the bubble sort algorithm. Here is the code. The language is Java. import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class Sort {    public static int numOfComps = 0,numOfSwaps = 0;     public static void main(String[] args)    {         try{        Scanner scanner = new Scanner(new File("a.txt"));//your text file here       ...
Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for...
Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for Bubble Sort b. Use a dynamic array of integers in a variable size of n. c. Display the following information: 1) Total counts of comparisons 2) Total counts of shifts / moves / swaps, whichever applies d. Write a main() function to test a best, and an average cases in terms of time efficiency i. Fill out the array with random numbers for an...
USING PYTHON, write a function that takes a list of integers as input and returns a...
USING PYTHON, write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2]. DO NOT use any special or built in functions like append, reverse etc.
Write a program in C++ to test either the selection sort or insertion sort algorithm for...
Write a program in C++ to test either the selection sort or insertion sort algorithm for array-based lists as given in the chapter. Test the program with at least three (3) lists. Supply the program source code and the test input and output. List1: 14,11,78,59 List2: 15, 22, 4, 74 List3: 14,2,5,44
What is the number of comparisons in the bubble sort algorithm, if it is used to...
What is the number of comparisons in the bubble sort algorithm, if it is used to sort a list of n-entries? Justify your formula.
It's time to write a sorting algorithm! In this lab, you'll be writing Bubble Sort. Much...
It's time to write a sorting algorithm! In this lab, you'll be writing Bubble Sort. Much like the previous lab, you will be tasked with prompting the user for a list of values until the user enters 0 (you may use the same initializeVector that you wrote in the last lab). You will then write bubblesort which sorts the vector from smallest to largest. You will then call a function displayVector which displays the vector to the screen. Keep everything...
Write a program that first gets a list of 5 integers from input. Then, get another...
Write a program that first gets a list of 5 integers from input. Then, get another value from the input, and output all integers less than or equal to that value. Ex: If the input is 50 60 140 200 75 100, the output is: 50 60 75 For coding simplicity, follow every output value by a space, including the last one. Then, output a newline. Such functionality is common on sites like Amazon, where a user can filter results....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT