Expand|Select|Wrap|Line Numbers
- import java.io.*;
- class ShellSort
- {
- static void shSort(int a[])
- {
- int i,k,x,width;
- width=a.length/2;
- while(width>=1)
- {
- for(k=width;k<a.length;k++)
- {
- x=a[k];
- for(i=k-width;i>=0&&x<a[i];i=i-width)
- {
- a[i+width]=a[i];
- }
- a[i+width]=x;
- }
- width=width/2;
- }
- }
- public static void main(String args[])throws IOException
- {
- int a[],n,i;
- BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
- System.out.println("enter number of elements");
- n=Integer.parseInt(br.readLine());
- a=new int[n];
- System.out.println("enter elements");
- for(i=0;i<n;++i)
- a[i]=Integer.parseInt(br.readLine());
- shSort(a);
- System.out.println("sorted list");
- for(i=0;i<n;++i)
- System.out.print(a[i]+" ");
- System.out.println();
- }
- }