Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

CPEG 220 – Computer Organization and Architecture

Fall 2021

Project

Dr. Mounib Khanafer

Student Name Student ID

Sarah Al-Sarahid S00055789

Deema Al-Wlayti S00050688

Fatimah Al-Dousari S00053029


100

1. Sorting Algorithm:

1
The sorting algorithm we chose to program is the selection sort. It works by finding the
minimum elements in an unsorted array and sorting it into an ascending order. The algorithm
takes an element as the minimum and compares it to every other element within the array and
swaps it if a smaller element is found thus creating a sorted list of arrays when it is finished with
all iterations.

2. Java Code :
public class SelectionSort

2
{
public static void main(String[] args)
{
int[] a = {12, 9, 15, 3, 7};
System.out.println("Array before sorting :");
for (int i=0;i<a.length;i++)
System.out.print(a[i] + " ");
for (int i=0;i<a.length-1;i++)
{
int min = i;
for (int j=i;j<a.length;j++)
if (a[j] < a[min])
min = j;
if (min != i)
{
int temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
System.out.println("\nArray after sorting :");
for (int i=0;i<a.length;i++)
System.out.print(a[i] + " ");
}
}

3
3. MIPS Assembly Code :
main:
lui $s0,0x1001 # $s0 = &A[0]

addi $t6,$0,0x0 #i($t6) = 0


loopi: slti $t7,$t6,4 #if (i<4), then $t7=1, otherwise $t7=0
beq $t7,$0,iexit #if $t7=0, then break from loop
addi $t5,$t6,0 #min($t5) = i
addi $t4,$t6,0 #j($t4) = i
loopj:
slti $t7,$t4,5 #if (j<5), then $t7=1, otherwise $t7=0
beq $t7,$0,jexit #if $t7=0, then break from loop

sll $t3,$t5,2 #$t3 = min*4


add $t3,$t3,$s0 #$t3 = &A[min]
lw $t2,0($t3) #$t2 = A[min]

sll $t1,$t4,2 #$t1 = j*4


add $t1,$t1,$s0 #$t1 = &A[j]
lw $t0,0($t1) #$t0 = A[j]

slt $t7,$t0,$t2 #if (a[j] < a[min]), $t7=1


beq $t7,$0,jcont
addi $t5,$t4,0 #min = j
jcont:
addi $t4,$t4,1 #j++
j loopj
jexit:
beq $t5,$t6,icont

4
sll $t1,$t6,2 #$t1 = i*4
add $t1,$t1,$s0 #$t1 = &A[i]
lw $t0,0($t1) #$t0 = A[i]

sw $t2,0($t1) #A[i] = old A[min]


sw $t0,0($t3) #A[min] = old A[i]
icont:
addi $t6,$t6,1 #i++
j loopi
iexit: syscall

.data
A: .word 12, 9, 15, 3, 7

5
4. Run and Test :

Before the simulation:

6
After the simulation:

You might also like