Wednesday, 5 November 2014

SORTING

Hye and Assalamualaikum. Today i am going to post about sorting
One of the sorting i will share here is insertion sort and buble sorting


insertion sort

-it is efficient for small number of data
-its is simple implementation and stable

code

for x=1 to n-1
       key=A[x]
       i=x-1
 while i>=0 and A[i]>key
      A[i+1] = A[i]
      i=i-1
     A[i+1]=key

bubble sorting


-bubble sorting is famous and frequently used.
-start comparing with first two array and swapping it.
-and same to others. it will continue swapping until it is arranged and sort


for i from x downto 1 {
for y from 1 to i-1{
       if (A[j]>A[j+1])
       swap(A[j],A[j+1])
}
}
}


No comments:

Post a Comment