Wednesday 4 July 2012

program to perfomed Shell sort using java.

import java.io.*;
class ShellSort
{
    public static void main(String args[]) throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int a[]=new int[9];
        int h;
        int inc[]=new int[9];
        int t,i,j,k;
        for(i=0;i<9;i++)
        {
            a[i]=Integer.parseInt(br.readLine());
        }  
      
for(i=0,h=1;h<a.length;i++)
        {  
            inc[i]=h;
            h=3*h+1;
        }
        for(i=i-1;i>=0;i--)
        {  
            h=inc[i];
            for(int z=h;z<2*h;z++)
            {
                j=z;
                while(j<a.length)
                {
                    t=a[j];
                    k=j-h;
                    while(k>=0 && t<a[k])
                    {
                        a[k+h]=a[k];
                        k=k-h;
                    }
                a[k+h]=t;
                j=j+h;
                }
            }
        }
        //output
        System.out.println("output is:");
        for(i=0;i<9;i++)
        {
            System.out.println(a[i]);
        }
      
    }          
}

No comments:

Post a Comment