INSERTION SORT

#include<stdio.h>
#include<conio.h>
void insertion(int p[],int last)
{
     int current,walker,hold;
     for(current=1;current<last;current++)
     {
           hold=p[current];
           for(walker=current-1;walker>=0&&hold<p[walker];walker--)
           {
                p[walker+1]=p[walker];
           }
           p[walker+1]=hold;
     }
}
void shellsort(int p[],int last)
{
     int incre,hold,walker,current,temp;
     incre=last/2;
     while(incre!=0)
     {
           current=incre;
           while(current<=last)
           {
           hold=p[current];
                walker=current-incre;
                while(walker>=0 && hold<p[walker])
                {
                     p[walker+incre]=p[walker];
                     walker=walker-incre;
                }
                p[walker+incre]=hold;
                current++;
           }
           incre=incre/2;
     }
}
void display(int p[],int last)
{
     int i;
     for(i=0;i<last;i++)
     {
           printf("\n\t|\tValue At List[%d] = %d\t|",i+1,p[i]);
     }
}
void main()
{
     int list[100],i,n,opt;
     char cont='y';
     while(cont=='y' || cont=='Y')
     {
           clrscr();
           printf("\tEnter The Size Of List : ");
           scanf("%d",&n);
           printf("\n\n\tEnter The Values In The List : \n");
           for(i=0;i<n;i++)
           {
                printf("\tValue At List[%d] = ",i+1);
                scanf("%d",&list[i]);
           }
           printf("\n\t---------------------------------");
           printf("\n\t|\t-: Type Of Sort :-\t|");
           printf("\n\t---------------------------------");
           printf("\n\t|\t1 : Insertion Sort\t|");
           printf("\n\t|\t2 : Shell Sort\t\t|");
           printf("\n\t---------------------------------");
           correct:
           printf("\n\n\tEnter The Option : ");
           scanf("%d",&opt);

           switch(opt)
           {
                case 1:
                     printf("\n\t---------------------------------");
                     printf("\n\t|\t-: Insertion Sort :-\t|");
                     printf("\n\t---------------------------------");
                     insertion(list,n);
                     display(list,n);
                     printf("\n\t---------------------------------");
                     break;
                case 2:
                     printf("\n\t---------------------------------");
                     printf("\n\t|\t-: Shell Sort :-\t|");
                     printf("\n\t---------------------------------");
                     shellsort(list,n-1);
                     display(list,n);
                     printf("\n\t---------------------------------");
                     break;
                default:
                     printf("\n\t ! Incorrect Option");
                     goto correct;
                }
           printf("\n\n\tDo You Want To Continue (Y/N) : ");
           cont=getch();
     }
}

OUTPUT:

        Enter The Size Of List : 6

        Enter The Values In The List :
        Value At List[1] = 23
        Value At List[2] = 78
        Value At List[3] = 45
        Value At List[4] = 8
        Value At List[5] = 32
        Value At List[6] = 56

        --------------------------------------
        |       -: Type Of Sort :-         |
        --------------------------------------
        |       1 : Insertion Sort   |
        |       2 : Shell  Sort                 |
        --------------------------------------

        Enter The Option : 1

        ------------------------------------
        |       -: Insertion Sort :-       |
        ------------------------------------
        |       Value At List[1] = 8     |
        |       Value At List[2] = 23   |
        |       Value At List[3] = 32   |
        |       Value At List[4] = 45   |
        |       Value At List[5] = 56   |
        |       Value At List[6] = 78   |
        ------------------------------------

        Do You Want To Continue (Y/N) : Y

        Enter The Size Of List : 10

        Enter The Values In The List :
        Value At List[1] = 12
        Value At List[2] = 32
        Value At List[3] = 56
        Value At List[4] = 89
        Value At List[5] = 78
        Value At List[6] = 45
        Value At List[7] = 14
        Value At List[8] = 25
        Value At List[9] = 36
        Value At List[10] = 58

        --------------------------------------
        |       -: Type Of Sort :-         |
        --------------------------------------
        |       1 : Insertion Sort         |
        |       2 : Shell  Sort      |
        --------------------------------------

        Enter The Option : 1

        --------------------------------------
        |       -: Insertion Sort :-       |
        --------------------------------------
        |       Value At List[1] = 12      |
        |       Value At List[2] = 14      |
        |       Value At List[3] = 25      |
        |       Value At List[4] = 32      |
        |       Value At List[5] = 36      |
        |       Value At List[6] = 45      |
        |       Value At List[7] = 56      |
        |       Value At List[8] = 58      |
        |       Value At List[9] = 78      |
        |       Value At List[10] = 89     |
        --------------------------------------

        Do You Want To Continue (Y/N) : Y

        Enter The Size Of List : 10

        Enter The Values In The List :
        Value At List[1] = 77
        Value At List[2] = 62
        Value At List[3] = 14
        Value At List[4] = 9
        Value At List[5] = 30
        Value At List[6] = 21
        Value At List[7] = 80
        Value At List[8] = 25
        Value At List[9] = 70
        Value At List[10] = 55

        --------------------------------------
        |       -: Type Of Sort :-         |
        --------------------------------------
        |       1 : Insertion Sort         |
        |       2 : Shell  Sort            |
        --------------------------------------

        Enter The Option : 2

        --------------------------------------
        |       -: Shell Sort :-           |
        --------------------------------------
        |       Value At List[1] = 9       |
        |       Value At List[2] = 14      |
        |       Value At List[3] = 21      |
        |       Value At List[4] = 25      |
        |       Value At List[5] = 30      |
        |       Value At List[6] = 55      |
        |       Value At List[7] = 62      |
        |       Value At List[8] = 70      |
        |       Value At List[9] = 77      |
        |       Value At List[10] = 80     |
        --------------------------------------

        Do You Want To Continue (Y/N) :  Y
        Enter The Size Of List : 6

        Enter The Values In The List :
        Value At List[1] = 12
        Value At List[2] = 23
        Value At List[3] = 56
        Value At List[4] = 89
        Value At List[5] = 78
        Value At List[6] = 45

        --------------------------------------
        |       -: Type Of Sort :-         |
        --------------------------------------
        |       1 : Insertion Sort         |
        |       2 : Shell Sort             |
        --------------------------------------

        Enter The Option : 2

        --------------------------------------
        |       -: Shell Sort :-           |
        --------------------------------------
        |       Value At List[1] = 12      |
        |       Value At List[2] = 23      |
        |       Value At List[3] = 45      |
        |       Value At List[4] = 56      |
        |       Value At List[5] = 78      |
        |       Value At List[6] = 89      |
        --------------------------------------

        Do You Want To Continue (Y/N) : N



No comments:

Post a Comment

Python Code Loop Program-1

 #Chapter -7 Qno - 2 num=4; while(num<10):     print("Number is %d"%num);     num=num+1; Output Number is 4 Number is 5 Number ...