Most Commonly Asked Programs In C++ Language

Most Commonly Asked Programs In C++ Language


Mini Project :~ Sorting Techniques Using Functions.

Posted: 26 May 2013 08:13 AM PDT

/* Program To Perform Different Types Of Sorting Techniques Using Functions */

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int  No2 = 0;
int  Scan(int a[100]);
void BubbleS(int a[100],int);
void InsertionS(int a[100],int);
void SelectionS(int a[100],int);
void ShellS(int a[100],int);
void main()
{
    int No1 = 0;
    int a[100];
    clrscr();
    printf("\n\t\t :: Welcome To 'Sorting' Program ::");
    do
    {
        printf("\nPlease Select One Of The Following Options ::");
        printf("\n1. Bubble Sorting");
        printf("\n2. Insertion Sorting");
        printf("\n3. Selection Sorting");
        printf("\n4. Shell Sorting");
        printf("\n5. Exit From Menu");
        printf("\nPlease Enter Your Choice Here ::\t");
        scanf("%d",&No1);
        switch(No1)
        {
            case 1: No2 = (Scan(a));
                BubbleS(a , No2);
                break;
            case 2: No2 = (Scan(a));
                InsertionS(a , No2);
                break;
            case 3: No2 = (Scan(a));
                SelectionS(a , No2);
                break;
            case 4: No2 = (Scan(a));
                ShellS(a , No2);
                break;
            case 5: exit (0);
                break;
            default:printf("\nSorry!!! Invalid Choice");
        }
    }
    while(No1 <= 5);
    getch();
}
int Scan(int a[100])
{
    int i;
    printf("\nPlease Enter The Limit Of Array A ::\t");
    scanf("%d",&No2);
    printf("\nPlease Enter The Elements For Array A ::\n");
    for(i=0;i<No2;i++)
    {
        scanf("%d",&a[i]);
    }
    return (No2);
}
void BubbleS(int a[100], int No2)
{
    int i , Ptr , Temp = 0;
    for(i=1;i<No2;i++)
    {
        Ptr = 0;
        while(Ptr < (No2 - i))
        {
            if(a[Ptr] > a[Ptr+1])
            {
                Temp = a[Ptr];
                a[Ptr] = a[Ptr+1];
                a[Ptr+1] = Temp;
            }
            Ptr++;
        }
    }
    printf("\nBubble Sorted Array A Is As Follows ::\n");
    for(i=0;i<No2;i++)
    {
        printf("%d \n",a[i]);
    }
}
void InsertionS(int a[100],int No2)
{
    int i , j , Temp = 0;
    for(i=1;i<No2;i++)
    {
        Temp = a[i];
        for(j=(i-1); j>=0 && Temp<a[j] ; j--)
        {
            a[j+1] = a[j];
        }
        a[j+1] = Temp;
    }
    printf("\nInsertion Sorted Array A Is As Follows ::\n");
    for(i=0;i<No2;i++)
    {
        printf("%d \n",a[i]);
    }
}
void SelectionS(int a[100],int No2)
{
    int i , j , k , Temp = 0;

    for(i=0;i<(No2-1);i++)
    {
        k = i;
        for(j=(i+1);j<No2;j++)
        {
            if(a[j] < a[k])
            {
                k = j;
            }
        }
        if(k != i)
        {
            Temp = a[i];
            a[i] = a[k];
            a[k] = Temp;
        }
    }
    printf("\nSelection Sorted Array A Is As Follows ::\n");
    for(i=0;i<No2;i++)
    {
        printf("%d \n",a[i]);
    }
}
void ShellS(int a[100],int No2)
{
    int i, k, Stop, Swap, Limit, Temp;
    int x = (int)(No2/2)-1;
    while(x > 0)
    {
        Stop  = 0;
        Limit = (No2 - x);
        while(Stop == 0)
        {
            Swap = 0;
            for(k=0;k<Limit;k++)
            {
                if(a[k] > a[k+x])
                {
                    Temp   = a[k];
                    a[k]   = a[k+x];
                    a[k+x] = Temp;
                    Swap   = k;
                }
            }
        Limit = (Swap - x);
        if(Swap == 0)
            Stop = 1;
        }
        x=(int)(x/2);
    }
    printf("\nShell Sorted Array A Is As Follows ::\n");
    for(i=0;i<No2;i++)
    {
        printf("%d \n",a[i]);
    }
}

Output :~

Please Select One Of The Following Options ::
1. Bubble Sorting
2. Insertion Sorting
3. Selection Sorting
4. Shell Sorting
5. Exit From Menu
Please Enter Your Choice Here :: 1

Please Enter The Limit Of Array A :: 5

Please enter The Elements Of Array A ::

3  5  4  2  1

Bubble Sorted Array A Is As Follows ::

1  2  3  4  5 

Please Select One Of The Following Options ::
1. Bubble Sorting
2. Insertion Sorting
3. Selection Sorting
4. Shell Sorting
5. Exit From Menu
Please Enter Your Choice Here :: 2

Please Enter The Limit Of Array A :: 5

Please enter The Elements Of Array A ::

3  5  4  2  1

Insertion Sorted Array A Is As Follows ::

1  2  3  4  5 

Please Select One Of The Following Options ::
1. Bubble Sorting
2. Insertion Sorting
3. Selection Sorting
4. Shell Sorting
5. Exit From Menu
Please Enter Your Choice Here :: 3

Please Enter The Limit Of Array A :: 5

Please enter The Elements Of Array A ::

3  5  4  2  1

Selection Sorted Array A Is As Follows ::

1  2  3  4  5 

Please Select One Of The Following Options ::
1. Bubble Sorting
2. Insertion Sorting
3. Selection Sorting
4. Shell Sorting
5. Exit From Menu
Please Enter Your Choice Here :: 4

Please Enter The Limit Of Array A :: 5

Please enter The Elements Of Array A ::

3  5  4  2  1

Shell Sorted Array A Is As Follows ::

1  2  3  4  5 

Please Select One Of The Following Options ::
1. Bubble Sorting
2. Insertion Sorting
3. Selection Sorting
4. Shell Sorting
5. Exit From Menu
Please Enter Your Choice Here :: 5


Post a Comment