Professional Documents
Culture Documents
• Selection sort:
Selection sort is an in-place comparison sort. It has complexity,
making it inefficient on large lists, and generally performs worse than
the similar insertion sort. Selection sort is noted for its simplicity, and
has performance advantages over more complicated algorithms in
certain situations.
• Merge sort:
Merge sort takes advantage of the ease of merging already sorted lists into a
new-sorted list. It starts by comparing every two elements (i.e., 1 with 2,
then 3 with 4...) and swapping them if the first should come after the second.
It then merges each of the resulting lists of two into lists of four, then merges
those lists of four, and so on; until at last two lists are merged into the final
sorted list.
• Heap sort:
Heap sort is a much more efficient version of selection sort. It also works by
determining the largest (or smallest) element of the list, placing that at the
end (or beginning) of the list, then continuing with the rest of the list, but
accomplishes this task efficiently by using a data structure called a heap, a
special type of binary tree. Once the data list has been made into a heap, the
root node is guaranteed to be the largest (or smallest) element. When it is
removed and placed at the endp of the array).
Instead of searching an array as a whole, the bubble sort works by
comparing adjacent pairs of the objects in the array. If the objects are not in
the correct order, they are swapped, so that the largest of the two moves up.
This process continues until the largest of the objects, eventually “bubbles”
up to the highest position in the array. After this occurs, the search for the
next largest object begins. The swapping continues until the whole array is
in the correct order.
Mips code:
.data
msg1: .asciiz "\nEnter integer values followed by return (-1
terminates input):/n " msg2: .asciiz "," msg3:
.asciiz "Bubble Sort" msg4: .asciiz
"#########pass#########"
msg5: .asciiz "\n"
msg6: .asciiz "\nNumber list has been sorted\n"
.text
.globl main
main:
move $s0,$gp #get the intial pointer to
save array
addi $t0,$zero,1 # $t0 = 1
add $t1,$zero,$zero # Intial value for t
Rigesters
add $t2,$zero,$zero # Intial value t2=0
add $t3,$zero,$zero # t3=0
add $t6,$zero,$zero add
$t4,$zero,$zero sub $t7,$zero,1
# terminate
li $v0,4 # system call to put the string
la $a0,msg1 # //
syscall #//
add $s1,$s0,$zero # copy the pointer to array in $s1
entervalues:
li $v0,5 # get the value in v0
syscall #
beq $v0,$t7,bubblesort # end of string run to bubblesort
sb $v0,0($s1) # put the value at the position pointed by
$s1
addi $s1,$zero,1 # move the $s1 pointer by one
add $t5,$s1,$zero # $t5 stores the end value
j entervalues
bubblesort: #This Label To test if all
elements sorted
add $t4,$s0,$zero
addi $t6,$zero,1
#s1-1 -> s0
sub $s1,$s1,$t0
#s0 -> s1
add $s2,$s0,$zero
loopinterno: #This label contains bubble
swap method if a[i]>a[i+1]
lb $t1,0($s2) # first element lb
$t2,1($s2) # second element slt $t3,$t2,$t1
# Test who is greater beq $t3,$zero,proximo # if
yes branch to next label
sb $t2,0($s2) # re saved them in save register after swap
sb $t1,1($s2) #// swap both 2 elements
proximo: # Test if all stages are
completed and then Print msgs
addi $s2,$zero,1 # decrement 1 from array size bne
$s2,$s1,loopinterno #resort element in each stage which =i-1
li $v0,4 # system call to put the string
la $a0,msg5 #
syscall #
li $v0,4 # system call to put the string
la $a0,msg4 #
syscall #
li $v0,4 # system call to put the string
la $a0,msg5 #
syscall #
imprime: # loading element sorted
li $v0,1
lb $a0,0($t4)
syscall
li $v0,4 # system call to put the string
la $a0,msg2
syscall
addi $t4,$zero,1
bne $t4,$t5,imprime
jal bubblesort
ending: # End the program by printing msg6
li $v0,4 # system call to put the string
la $a0,msg6 #
syscall #
li $v0,5 syscall