Sunteți pe pagina 1din 7

Microprocessors CENG405 Done By :

2018- 2019 Nabil Mohsen ALzeqri


Dr. Sami Al-maqtari ID : 61330237

Report of sort number (Q no.2)

 Sort number

 Sorting Algorithms:

1. The output is in no decreasing order (each element is no smaller than the previous
element according to the desired total order).
2. The output is a permutation (reordering) of the input.

 Some Popular Sorting Algorithms:


• Selection sort:

• Merge sort:
Microprocessors CENG405 Done By :
2018- 2019 Nabil Mohsen ALzeqri
Dr. Sami Al-maqtari ID : 61330237

• Heap sort:

• How Bubble Sort works?

• {5,3,1,4,2} Compare first two, as 5 > 3, they are swapped


• {3,5,1,4,2} Again compare next two, as 5 >1, they are swapped
• {3, 1,5,4,2} Like this it keep swapping and iterate [go through] all the elements once to
get this.
• {3, 1,4,2,5} Now they start doing the second run, compare 3 & 1, as 3> 1 they are
swapped.
• {1,3,4,2,5} compare 3,4 they are in correct order
Likewise, you compare until no swaps occur while examining the whole sequence and then the
function says "I am done with this" and the looping stops.
Microprocessors CENG405 Done By :
2018- 2019 Nabil Mohsen ALzeqri
Dr. Sami Al-maqtari ID : 61330237

 Bubble Sort Flow Chart:


Microprocessors CENG405 Done By :
2018- 2019 Nabil Mohsen ALzeqri
Dr. Sami Al-maqtari ID : 61330237

 Bubble Sort C++ Code:


for( i=n-1, i >1, i++){ for( i=1, i <=n, i++){
for( j=n-1, j >=i, j-){ if (a[j]<a[j-1]) {int t =
a[j] a[j] = a[j-1] a[j-1] = t; }
}
}
}

 Mips code:
.globl main
main:
#printing Name and ID
li $v0,4 #load command to print string
la $a0,msg0 #print string msg
syscall #make system call
#printing Name and ID
li $v0,4 #load command to print string
la $a0,msg #print string msg
syscall #make system call
#printing message 1
li $v0,4 #load command to print string
la $a0,msg1 #print string msg1
syscall #make system call
#read integer for size
li $v0,5 #load command to read number
syscall #make system call
move $s0,$v0 #storing size into $s0
#printing message 2
Microprocessors CENG405 Done By :
2018- 2019 Nabil Mohsen ALzeqri
Dr. Sami Al-maqtari ID : 61330237

li $v0,4 #load command to print string


la $a0,msg2 #print string msg2
syscall #make system call
#reading numbers
la $t0,array #Load based address of array
move $t1,$s0 #move size to t1
loop: #Loop until
li $v0,5 #command to read integer
syscall #make system call
sw $v0,($t0) #save integer to address pointed by t0
#advance $t0 so it point to next index
add $t0,$t0,4 #move to address of next integer
sub $t1,$t1,1 #reduce size by one
bgt $t1,0,loop #branch less than t1 than loop
#sorting array using bubble sort
li $t1,0 #index to zero
for1: #outer for loop
la $t0,array #load address of array
li $t2,1 #load index one
for2: #inner for loop
lw $t3,($t0) #load word from address to
add $t0,$t0,4 #go to next integer
lw $t4,($t0) #store to t4
blt $t3,$t4,skipswap #if t3 is less than t4 skip
#swap content
sub $t0,$t0,4 #reduce 4 from t0
sw $t4,($t0) #save t4 to address t0
add $t0,$t0,4 #add four to t0
sw $t3,($t0) #save t3 to t0
skipswap:
sub $t5,$s0,$t1 #reduce t1 from s0 and store to t5
add $t2,$t2,1 #add one to t2
Microprocessors CENG405 Done By :
2018- 2019 Nabil Mohsen ALzeqri
Dr. Sami Al-maqtari ID : 61330237

blt $t2,$t5,for2 #branch is t2 less than t5


add $t1,$t1,1 #add one
blt $t1,$s0,for1 #branch if t1 less than s0
#printing sorted number
la $t0,array #load address of array
move $t1,$s0 #move s0 to t11
#printing message
li $v0,4 #command to print string
la $a0,msg3 #set to address of msg3
syscall #print
printloop: #loop for print
lw $a0,($t0) #load element to a0
add $t0,$t0,4 #add t0 by four
sub $t1,$t1,1 #reduce one index
li $v0,1 #print integer
syscall
#printing space
li $v0,11
li $a0,32
syscall
bgt $t1,0,printloop
#code for exit
li $v0,10 #terminate
syscall
.data
msg0: .asciiz ">In the first thank you so much best Dr: Sami<\n"
msg: .asciiz "\n~~~~~~Q 2 SORT~~~~~~~ \nName: Nabil Mohsen Alzeqri\nID: 61330237\n"
msg1: .asciiz "Enter size:"
msg2: .asciiz "Enter numbers:\n"
msg3: .asciiz "sorted numbers:\n"
array: .word 0
Microprocessors CENG405 Done By :
2018- 2019 Nabil Mohsen ALzeqri
Dr. Sami Al-maqtari ID : 61330237

S-ar putea să vă placă și