Thursday, January 29, 2015

selection sort and marge sort

/*Write menu driven program to sort numbers using following methods:

Menu for Sorting Methods

1. Selection sort Method
2. Marge Sort Method
3. Exit


Your Choice : 1,2,3 -->
*/

#include < stdio.h >
void selection(int [],int [],int);
void marge(int [],int [],int);
void display(int [],int [],int);
int readdata(int [],int []);
void swap(int *,int *);
void selection1();
void marge1();
void main()
{
    int flag;
    char ch;
    flag=1;
    while(flag==1)
    {
        clrscr();
        gotoxy(25,10);
        printf("Menu for Sorting Methods");
        gotoxy(25,12);
        printf("1. Selection Sort Method");

    /*      (i) To select a Block : ^KB ^KK
     (ii) To Copy a Block : ^KC
    (iii) To Hide selection : ^KH
     (iv) To Move a Block :^KV
      (v) To Remove a Block : ^KY    */

        gotoxy(25,13);
        printf("2. Marge Sort Method");
        gotoxy(25,14);
        printf("3. Exit");
        gotoxy(25,16);
        printf("Your Choice = 1,2,3 ---> ");
       
        ch=getch();
        switch(ch)
        {
            case '1': selection1(); break;
            case '2': marge1(); break;
            case '3': flag=0; clrscr();
                gotoxy(30,13);
                printf("Thank You.");
                getch();
                break;
            default : break;
        }
    }
}

/* void display(int [],int [],int) : Function to display a list */

void display(int a[],int id[],int n)
{
    int i;
    printf("\nNumber Index\n");
    for(i=0;i        printf("%4d %4d\n",a[i],id[i]);
    printf("press any key to continue -->");
    getch();
}

               
/* int readdata(int [],int []) : Function to input a list */

int readdata(int a[],int id[])
{
    int i,n;
    while(1)
    {
        printf("\nEnter size of your list (1-20) : ");
        scanf("%d",&n);
        if(n<1 n="">20)
            printf("\n***Invalid data***\n");
        else
            break;
    }
    printf("\nEnter %d elements one by one -->\n",n);
    for(i=0;i    {
        printf("a[%d]= ",i);
        scanf("%d",&a[i]);
        id[i]=i+1;
    }
    return n;
}

/* void selection1() : Funcion to call selection() function*/
void selection1()
{
    int a[20],id[20],n;
    char ch;
    do
    {
        clrscr();
        printf("\nEnter your List -->\n");
        n=readdata(a,id);
        printf("\n\nPress any key to display unsorted list -->");
        getch();
        clrscr();
        printf("\nUnsorted List\n");
        display(a,id,n);
        selection(a,id,n);
        printf("\n\nPress any key to display sorted list -->");
        getch();
        printf("\nSorted List using Selection sort method\n");
        display(a,id,n);
        printf("\n\nDo you want to continue(Y/N?) : ");
        ch=getch();
    }while(ch=='y'||ch=='Y');
}

/* void marge1() : Funcion to call marge() function*/

void marge1()
{
    int a[20],id[20],n;
    char ch;
    do
    {
        clrscr();
        printf("\nEnter your List -->\n");
        n=readdata(a,id);
        printf("\n\nPress any key to display unsorted list -->");
        getch();
        clrscr();
        printf("\nUnsorted List\n");
        display(a,id,n);
        marge(a,id,n);
        printf("\n\nPress any key to display sorted list -->");
        getch();
        printf("\nSorted List using Marge sort method\n");
        display(a,id,n);
        printf("\n\nDo you want to continue(Y/N?) : ");
        ch=getch();
    }while(ch=='y'||ch=='Y');
}


/* void swap(int *x,int *y) : Funcion to interchange two elements*/

void swap(int *x,int *y)
{
    int t;
    t=*x;
    *x=*y;
    *y=t;
}

/* void selection(int a[],int id[],int n) : Funcion to sort data using selection sort algorithm*/

void selection(int a[],int id[],int n)
{
    int i,pass,j;
    int minimum(int [],int,int);
    pass=0;
    for(i=0;i<(n-1);i++)
    {
        j=minimum(a,i,n);
        swap(&a[i],&a[j]);
        pass=pass+1;
        clrscr();
        printf("\nPass # : %d\n",pass);
        display(a,id,n);
    }
}

/* int minimum(int a[],int i,int n) : Function to locate the index of the minimum value */

int minimum(int a[],int i,int n)
{
    int j,m,mn;
    mn=a[i];
    m=i;
    for(j=i+1;j        if(a[j]        {
            mn=a[j];
            m=j;
        }
    return m;
}

/* void marge(int a[],int id[],int n) : Funcion to sort data using marge sort algorithm*/

void marge(int a[],int id[],int n)
{
    int ia,ua,ib,ub,ix,size,i,j;
    int aux[20],iux[20];
    int pass;
    size=1;
    pass=0;
    while(size    {
        pass=pass+1;
        ia=0;
        ix=0;
        while((ia+size)        {
            ib=ia+size;
            ua=ib-1;
            if((ib+size-1)                ub=ib+size-1;
            else
                ub=n-1;
            for(i=ia,j=ib;i<=ua && j<=ub;ix++)
                if(a[i]<=a[j])
                {
                    aux[ix]=a[i];
                    iux[ix]=id[i];
                    i++;
                }
                else
                {
                    aux[ix]=a[j];
                    iux[ix]=id[j];
                    j++;
                }

            for(;i<=ua;i++)   
            {
                aux[ix]=a[i];
                iux[ix]=id[i];
                ix++;
            }

            for(;j<=ub;j++)   
            {
                iux[ix]=id[j];
                ix++;
            }
            ia=ub+1;
        }
        for(;ix        {
            aux[ix]=a[ia];
            iux[ix]=id[ia];
            ia=ia+1;
        }
        aux[ix]=a[j];

    /* To copy all elements from aux[] to a[] */

        for(i=0;i        {
            a[i]=aux[i];
            id[i]=iux[i];
        }
        clrscr();
        printf("\nPass # : %d\n",pass);
        display(a,id,n);
        size=2*size;
    }
}

No comments:

Post a Comment