You are on page 1of 7

Sorting Algorithms:

A sorting algorithm is an algorithm that puts elements of a list in a certain


order. The most-used orders are numerical order and lexicographical order.
Efficient sorting is important for optimizing the use of other algorithms
(such as search and merge algorithms) which require input data to be in
sorted lists; it is also often useful for canonical zing data and for producing
human-readable output. More formally, the output must satisfy two
conditions:

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.
Further, the data is often taken to be in an array, which allows random
access, rather than a list, which only allows sequential access, though often
algorithms can be applied with suitable modification to either type of data.

Some Popular Sorting Algorithms:

• 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.

How Bubble Sort works?


The process is simple, you keep comparing pairs of adjacent elements of the
sequence, if the they are in the wrong order swap them and do this till there
are no swapping to do.

Ex: Sort an array {5, 3, 1, 4, 2} using Bubble Sort

• {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.

Bubble Sort Flow Chart:


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:

.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

beq $s1,$s0,ending # we have sorted everything

#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

You might also like