SELECTION SORT

#include<stdio.h>
#include<conio.h>

void selection(int p[],int last)
{
     int current,walker,smallest,temp;
     for(current=0;current<last;current++)
     {
           smallest=current;
           for(walker=current+1;walker<last;walker++)
           {
                if(p[walker]<p[smallest])
                {
                     smallest=walker;
                }
           }
           temp=p[current];
           p[current]=p[smallest];
           p[smallest]=temp;
     }
}

void reheapUP(int heap[],int newNode)
{
     int parent,temp;
     if(newNode!=0)
     {
           parent=(newNode-1)/2;
           if(heap[newNode]>heap[parent])
           {
                temp=heap[newNode];
                heap[newNode]=heap[parent];
                heap[parent]=temp;
                reheapUP(heap,parent);
           }
     }
}

void reheapDOWN(int heap[],int root,int last)
{
     int rightkey,leftkey,LCK,LCI,temp;
     if((root*2)+1<=last)
     {
           leftkey=heap[(root*2)+1];
           if((root*2)+2<=last)
           {
                rightkey=heap[(root*2)+2];
           }
           else
           {
                rightkey=0;
           }
           if(leftkey>rightkey)
           {
                LCK=leftkey;
                LCI=(root*2)+1;
           }
           else
           {
                LCK=rightkey;
                LCI=(root*2)+2;
           }
           if(heap[root]<heap[LCI])
           {
                temp=heap[root];
                heap[root]=heap[LCI];
                heap[LCI]=temp;
                reheapDOWN(heap,LCI,last);
           }
     }
}
void heapsort(int heap[],int last)
{
     int walker,sorted,temp;
     for(walker=1;walker<=last;walker++)
     {
           reheapUP(heap,walker);
     }
     sorted=last;
     while(sorted>0)
     {
           temp=heap[0];
           heap[0]=heap[sorted];
           heap[sorted]=temp;
           sorted--;
           reheapDOWN(heap,0,sorted);
     }
}

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 : Selection Sort\t|");
           printf("\n\t|\t2 : Heap 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-: Selection Sort :-\t|");
                     printf("\n\t---------------------------------");
                     selection(list,n);
                      display(list,n);
                     printf("\n\t---------------------------------");
                     break;
                case 2:
                     printf("\n\t---------------------------------");
                     printf("\n\t|\t-: Heap Sort :-\t|");
                     printf("\n\t---------------------------------");
                     heapsort(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 : Selection Sort         |
        |       2 : Heap Sort              |
        --------------------------------------

        Enter The Option : 1

        --------------------------------------
        |       -: Selection 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] = 74
        Value At List[4] = 58
        Value At List[5] = 96
        Value At List[6] = 56
        Value At List[7] = 54
        Value At List[8] = 87
        Value At List[9] = 98
        Value At List[10] = 10

        --------------------------------------
        |       -: Type Of Sort :-         |
        --------------------------------------
        |       1 : Selection Sort         |
        |       2 : Heap Sort              |
        --------------------------------------

        Enter The Option : 1

        --------------------------------------
        |       -: Selection Sort :-       |
        --------------------------------------
        |       Value At List[1] = 10      |
        |       Value At List[2] = 12      |
        |       Value At List[3] = 32      |
        |       Value At List[4] = 54      |
        |       Value At List[5] = 56      |
        |       Value At List[6] = 58      |
        |       Value At List[7] = 74      |
        |       Value At List[8] = 87      |
        |       Value At List[9] = 96      |
        |       Value At List[10] = 98     |
        --------------------------------------

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

        Enter The Size Of List : 6

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

        --------------------------------------
        |       -: Type Of Sort :-         |
        --------------------------------------
        |       1 : Selection Sort         |
        |       2 : Heap Sort              |
        --------------------------------------

        Enter The Option : 2

        --------------------------------------
        |       -: Heap 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] = 98
        Value At List[2] = 78
        Value At List[3] = 56
        Value At List[4] = 45
        Value At List[5] = 32
        Value At List[6] = 65
        Value At List[7] = 12
        Value At List[8] = 10
        Value At List[9] = 37
        Value At List[10] = 19

        --------------------------------------
        |       -: Type Of Sort :-         |
        --------------------------------------
        |       1 : Selection Sort         |
        |       2 : Heap Sort              |
        --------------------------------------

        Enter The Option : 2

        --------------------------------------
        |       -: Heap Sort :-            |
        --------------------------------------
        |       Value At List[1] = 10      |
        |       Value At List[2] = 12      |
        |       Value At List[3] = 19      |
        |       Value At List[4] = 32      |
        |       Value At List[5] = 37      |
        |       Value At List[6] = 45      |
        |       Value At List[7] = 56      |
        |       Value At List[8] = 65      |
        |       Value At List[9] = 78      |
        |       Value At List[10] = 98     |
        --------------------------------------

        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 ...