Program to implement Shell Sort in C | Wave the world

Program to implement Shell Sort in C

/*
   Shell Sort Algorithm
   Created By: Pirate
*/

#include<stdio.h>
#include<conio.h>
int arr[100];
void shell_sort(int n);
int main()
{
                int i,n;
                printf("*** SHELL SORT ***\n");
                printf("Enter the no. of elements : ");
                scanf("%d", &n);
                printf("Enter %d elements:\n",n);
                for(i=0; i<n; i++)
                {
                                scanf("%d",&arr[i]);
                }
                shell_sort(n);
                printf("\n*** After Sorting ***\n");
                for(i=0; i<n; i++)
                {
                                printf("%d\t",arr[i]);
                }
                getch();
                return 0;
}
void shell_sort(int n)
{
                int i,j,k,tmp;
                for(i=n/2; i>0; i=i/2)
                {
                                for(j=i; j<n; j++)
                                {
                                                for(k=j-i; k>=0; k=k-i)
                                                {
                                                                if(arr[k+i]>=arr[k])
                                                                break;
                                                                else
                                                                {
                                                                                tmp=arr[k];
                                                                                arr[k]=arr[k+i];
                                                                                arr[k+i]=tmp;
                                                                }
                                                }
                                }
                }

}


Output














1 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete

 

Pro

About