Bubble Sort MIPS assembly language Java sample code Bubble S

Bubble Sort (MIPS {assembly language})

Java (sample code)

++++++++++(Bubble Sort _ for loop _ sample)++++++++++++++++

// arr[i] will be in the correct spot after every iteration

for (int i = n-1; i > 0; i--)

for (int j = 0; j < i; j++) // Put arr[j] and arr[j+1] in order

if (arr[j] > arr[j+1]) { // If they are out of order, swap them

int tmp = arr[j];

arr[j] = arr[j+1];

arr[j+1] = tmp;

}

++++++++++++++++++++++++++++++++

add your code to the area marked “PUT CODE HERE.”

Do not modify anything outside that area.

do not write anything to registers $s0 or $s5.

You may use temporaries as necessary, and saved registers for other variables (like i and j).

After you add the code, re-assemble and run it

++++++++++++++ (MIPS Code _ Assemble language)+++++++++++++
.data
nums: .word 0 : 12 # \"array\" of 12 words to contain values
size: .word 12 # size of \"array\"
.text
la $s0, nums
la $s5, size # load address of size variable
lw $s5, 0($s5)
# Populate fibs with twelve values
addi $t1, $zero, 55
sw $t1, 0($s0)
addi $t1, $zero, 88
sw $t1, 4($s0)
addi $t1, $zero, 0
sw $t1, 8($s0)
addi $t1, $zero, 22
sw $t1, 12($s0)
addi $t1, $zero, 77
sw $t1, 16($s0)
addi $t1, $zero, 44
sw $t1, 20($s0)
addi $t1, $zero, 99
sw $t1, 24($s0)
addi $t1, $zero, 33
sw $t1, 28($s0)
addi $t1, $zero, 110
sw $t1, 32($s0)
addi $t1, $zero, 66
sw $t1, 36($s0)
addi $t1, $zero, 121
sw $t1, 40($s0)
addi $t1, $zero, 11
sw $t1, 44($s0)

##################################################################
# AT THIS POINT: $s0 is the address of the start of the array
# $s5 is the size (n)
#################################################################
# PUT CODE HERE


##################################################################
la $a0, nums # first argument for print (array)
add $a1, $s5, $zero # second argument for print (size)
jal print # call print routine.
li $v0, 10 # system call for exit
syscall # we are out of here.

######### routine to print the numbers on one line.
######### don\'t touch anything below this line!!!!

.data
space:.asciiz \" \" # space to insert between numbers
head: .asciiz \"Sorted array:\ \"
.text
print:add $s0, $zero, $a0 # starting address of array
add $t1, $zero, $a1 # initialize loop counter to array size
la $a0, head # load address of print heading
li $v0, 4 # specify Print String service
syscall # print heading
out: lw $a0, 0($s0) # load fibonacci number for syscall
li $v0, 1 # specify Print Integer service
syscall # print fibonacci number
la $a0, space # load address of spacer for syscall
li $v0, 4 # specify Print String service
syscall # output string
addi $s0, $s0, 4 # increment address
addi $t1, $t1, -1 # decrement loop counter
bgtz $t1, out # repeat if not finished
jr $ra # return
  

Solution

import java.util.Random;

public class bbl_srt
{
static int[] srt(int[] sqnc)
{
//doing bubble sort here
for (int i = 0; i < sqnc.length; i++)
for (int j = 0; j < sqnc.length - 1; j++)
if (sqnc[j] > sqnc[j + 1])
{
sqnc[j] = sqnc[j] + sqnc[j + 1];
sqnc[j + 1] = sqnc[j] - sqnc[j + 1];
sqnc[j] = sqnc[j] - sqnc[j + 1];
}
return sqnc;
}
   //It is to print sequencess.

static void prntSeqnce(int[] srtd_sqnce)
{
for (int i = 0; i < srtd_sqnce.length; i++)
System.out.print(srtd_sqnce[i] + \" \");
}

public static void main(String ars[])
{
System.out
.println(\"Sortingg going on..\");
Random rdnm = new Random();
int N = 20;
int[] sqnc = new int[N];

for (int i = 0; i < N; i++)
sqnc[i] = Math.abs(rdnm.nextInt(1000));

System.out.println(\"\ Original Sequence: \");
prntSeqnce(sqnc);

System.out.println(\"\ Sorted Sequence: \");
prntSeqnce(srt(sqnc));
}
}

Bubble Sort (MIPS {assembly language}) Java (sample code) ++++++++++(Bubble Sort _ for loop _ sample)++++++++++++++++ // arr[i] will be in the correct spot afte
Bubble Sort (MIPS {assembly language}) Java (sample code) ++++++++++(Bubble Sort _ for loop _ sample)++++++++++++++++ // arr[i] will be in the correct spot afte
Bubble Sort (MIPS {assembly language}) Java (sample code) ++++++++++(Bubble Sort _ for loop _ sample)++++++++++++++++ // arr[i] will be in the correct spot afte

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site