Wednesday, 23 January 2013

Shell Sort


By on 01:22

Shell Sorting is the sorting algorithm or a type of sorting which is used to sort data items in ascending or descending order.This method is developed by D.L Shell.Worst case Complexity of Shell sort is O(n).This technique is used in c ++,java,c# language etc.Shell Sort technique is also called as modified insertion sorting.


Shell Sorting Example and Algorithm:-

Gap=n/2.
10/2=5
gap=5.

=q  d  i  e  c  h  b  t  n  j (From q to h,d to b,i to t,e to n,c to j).

=h  b  i  e  c  q  d  t  n  j (after arrangement)

Now no value will swap,so gap=gap/2.
5/2=2.5=2

=h  b  i  e  c  q  d  t  n  j

=h  b  c  e  d  q  i  j  n  t (From h to c,b to e,c to d,e to q,d to i,q to j,i to n,j to t).

=c  b  d  e  h  j  i  q  n  t (after arrangement)


Now no value will swap,so gap=gap/2
2/1=1.


=h  b  c  e  d  q  i  j  n  t (From h to b,b to c,c to e,d to q,q to i,i to j,j to n,n to t).

=b  c  d  e  h  j  n  q  t (Sorted list).


Function cum Program:-

void shell(int a[],int n)
{
int gap,flag,i,j,t;
gap=gap/2;
flag=1;
while(gap)
{
while(flag)
{
flag=0;
for(i=0;i<n-gap;i++)
{
if(a[i]>a[i+gap])
{
t=a[i];
a[i]=a[i+gap];
a[i+gap]=t;
}
}
}
gap=gap/2;
}
}


That's all about Shell Sorting in data structure and computer science.

If you have any query then leave your comments and don't forgot to follow me on Google+,Facebook,Twitter.

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...