04 June, 2013

Mini Project :~ Sorting Techniques Using Functions #02.

This Post Contains A Mini Project :~ Sorting Techniques Using Functions #02 With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Quick Sort' & 'Merge Sort' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.

In The Following Program, We Would Perform Following Sorting Techniques Such As :
1. Quick Sort.
2. Merge Sort.



# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



Program To Perform 'Quick' And 'Merge' Types Of Sorting Techniques Using Functions.

/* Declaration Of Header Files */
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int  a[100],b[100],l,k,No2=0,j,t,m,i;
int  Scan(int a[100]);
int  QuickS(int a[100],int ,int);
void Disp1();
void Split(int,int);
void MergeS(int ,int ,int);
void Disp2();

/* Start Of Main Program */
void main()
{
    int No1 = 0;
    clrscr();
    printf("\n\t\t :: Welcome To 'Quick' And 'Merge' Types Of Sorting Program ::");
    do                                                                               // Displaying Menu Onto Screen.
    {
        printf("\n\nPlease Select One Of The Following Options ::");
        printf("\n1. Quick Sorting");
        printf("\n2. Merge Sorting");
        printf("\n3. Exit From Menu");
        printf("\nPlease Enter Your Choice Here ::\t");
        scanf("%d",&No1);
        switch(No1)
        {
            case 1: No2 = (Scan(a));
                l = 0;
                QuickS(a , l , No2);                 // Call To The Function.
                Disp1();
                break;
            case 2: No2 = (Scan(a));
                Split(0,No2-1);
                Disp2();
                break;
            case 3: exit (0);
                break;
            default:printf("\nSorry!!! Invalid Choice");
        }
    }
    while(No1 <= 3);
    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);
}
QuickS(int a[],int l,int No2)
{
    if(l >= No2)
        return(0);
    k=l;
    i=l;
    j=No2;
    m=a[i];
    do
    {
        do
        {
            i++;
        }
        while(a[i]<=m && i<No2);
        do
        {
            j--;
        }
        while(a[j]>=m && j>l);
        if(i<j)
        {
            t=a[i];
            a[i]=a[j];
            a[j]=t;
        }
    }
    while(i<j);

    t=a[j];
    a[j]=m;
    a[k]=t;
    QuickS(a,l,j);
    QuickS(a,j+1,No2);
    return(0);
}
void Disp1()
{
    printf("\nQuick Sorted Array  A Is As Follows ::\n");
    for(i=0;i<No2;i++)
    {
        printf("%d \n",a[i]);
    }
}
void MergeS(int low,int mid,int high)
{
    int i,h,j,k;
    i=low;
    h=low;
    j=mid+1;
    while(h <= mid && j <= high)
    {
        if(a[h] < a[j])
        {
            b[i] = a[h];
            h++;
        }
        else
        {
            b[i] =  a[j];
            j++;
        }
        i++;
    }
    if( h > mid )
    {
        for(k=j ; k <= high ; k++)
        {
            b[i] = a[k];
            i++;
        }
    }
    else
    {
        for(k=h ; k<= high ; k++)
        {
            b[i] =a[k];
            i++;
        }
    }
    for(k=low ; k <= high ; k++)
        a[k] = b[k];
}

void Split(int low,int high)
{
    int mid;
    if(low<high)
    {
        mid = (low+high)/2;
        Split(low,mid);
        Split(mid+1,high);
        MergeS(low,mid,high);                // Call To The Function.
    }
}
void Disp2()
{
    printf("\nMerge Sorted Array A Is As Follows ::\n");
    for(i=0;i<No2;i++)
    {
        printf("%d \n",a[i]);
    }
}
/* End Of Main Program */

 
Output :~
           
Please Select One Of The Following Options ::
1. Quick Sorting
2. Merge Sorting
3. Exit From Menu
Please Enter Your Choice Here :: 1

Please Enter The Limit Of Array A :: 8
   
Please Enter The Elements For Array A ::

7  4  1  0  6  2  8  5

Quick Sorted Array Is As Follows ::

0  1  2  4  5  6  7  8

Please Select One Of The Following Options ::
1. Quick Sorting
2. Merge Sorting
3. Exit From Menu
Please Enter Your Choice Here :: 2

Please Enter The Limit Of Array A :: 10
   
Please Enter The Elements For Array A ::

7  4  1  0  6  2  8  5  9  3

Merge Sorted Array Is As Follows ::

0  1  2  3  4  5  6  7  8  9

Please Select One Of The Following Options ::
1. Quick Sorting
2. Merge Sorting
3. Exit From Menu
Please Enter Your Choice Here :: 3




No comments:

Post a Comment

Subscribe To:

Most Commonly Asked Programs In 'C' & 'C++' Language.

Blog Archive