Showing posts with label Sorting Techniques. Show all posts
Showing posts with label Sorting Techniques. Show all posts

12 June, 2013

C++ Program For Sorting Of A Matrix By Rows.

This Post Contains A C++ Program For Sorting Of A Matrix By Rows With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At Turbo C/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Arrays', 'For-Loops', 'If...else' & 'Nested Loops' 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.



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



C++ Program For Sorting Of A Matrix By Rows.
/* Declaration Of Header Files */
#include <iostream.h>
#include <conio.h>

/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
int a[10][10], i, j, r, c, t;

clrscr();

      /* Asking For The Input From User */

       cout  <<  "  Enter Number Of Rows & Columns Of 2D Array [ Matrix ]  :  ";

       cin  >>  r  >>  c ;

      

       //  Accepting Values Of 2D Array [ Matrix ]

       cout  <<  "  Enter  "  <<  r  *  c  <<  "  Values for 2D Array  :  ";

       for  (  i  =  0;  i  <  r;  i++  )

       {

                for  (  j  =  0;  j  <  c;  j++  )

                {

                         cin  >>  a [ i ][ j ];

                }

       }

       // Printing Values Of 2D Array [ Matrix ]

       cout  <<  "  Values Of 2D Array [ Matrix ] Are  :  ";

       for  (  i  =  0;  i  <  r;  i++  )

       {

                cout  <<  " \n ";

                for  (  j  =  0;  j  <  c;  j++  )

                {

                         cin  >>  a [ i ][ j ];

                }

       }

/* Source Code For Sorting Of A Matrix By Rows */

   for(i=0; i < r; i++)

   {

      for(j=0; j < c; j++)

      {

         for(k=j+1; k < q; k++)

         {

            if(a[i][j] > a[i][k])

            {

               t=a[i][j];

               a[i][j]=a[i][k];

               a[i][k]=t;

            }

         }

      }

   }
/* Printing The Output Onto The Screen/Console */   

   cout  <<  " \n After Sorting Elements Of Matrix A : ";

   for(i=0; i < r; i++)

   {

      cout  <<  "\n";

      for(j=0; j < c; j++)

      {

         cout  <<  a[i][j]  <<  "\t";

      }

   }

getch();

}

/* End Of Main Program */

Output:
Enter Order For Array A : 3 3

Enter 9 Values For Array :

9  8  7  6  5  4  3  2  1

After Sorting Elements Of Matrix A :

7  8  9

4  5  6

1  2  3




C++ Program For Sorting All Elements Of A Matrix.

This Post Contains A C++ Program For Sorting All Elements Of A Matrix With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At Turbo C/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Arrays', 'For-Loop', 'If...else' & 'Nested Loops' 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.


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



C++ Program For Sorting All Elements Of A Matrix.
/* Declaration Of Header Files */
#include <iostream.h>
#include <conio.h>

/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
int a[10][10], b[30], i, j, r, c, s, t;

clrscr();

/* Asking For The Input From User */
cout  <<  "  Enter Number Of Rows & Columns Of 2D Array [ Matrix ]  :  ";
cin  >>  r  >>  c ;

      

//  Accepting Values Of 2D Array [ Matrix ]
cout  <<  "  Enter  "  <<  r  *  c  <<  "  Values for 2D Array  :  ";
t=0;
for  (  i  =  0;  i  <  r;  i++  )
{

      for  (  j  =  0;  j  <  c;  j++  )

     {

            cin  >>  a [ i ][ j ];

            b[t]=a[i][j];

            t++

     }
}

/* Source Code For Sorting All Elements Of A Matrix */
for(i=0; i < t; i++)
{

      for(j=i+1; j b[j])

           s=b[i];

           b[i]=b[j];

           b[j]=s;

      }
}

cout  <<  " \n Elements Of Matrix A : ";
for(i=0; i < r; i++)
{

      cout  <<  "\n";

      for(j=0; j < c; j++)

      {

         cout  <<  a[i][j]  <<  "\t";

      }
}

/* Assigning Sorted Elements */
t=0;
for(i=0; i < r; i++)
{

      for(j=0; j < c; j++)

      {

         a[i][j]=b[t];

         t++;

      }
}

/* Printing The Output Onto The Screen/Console */
cout  <<  " \n After Sorting Elements Of Matrix A : ";
for(i=0; i < r; i++)
{

      cout  <<  "\n";

      for(j=0; j < c; j++)

      {

         cout  <<  a[i][j]  <<  "\t";

      }
}

getch();

}
/* End Of Main Program */

Output:
Enter Order For Array A : 3 3

Enter 9 Values For Array :

1  2  3  4  5  6  7  8  9

Array A Is :

1  4  9

2  7  8

5  6  3

After Sorting Elements Of Matrix A :

1  2  3

4  5  6

7  8  9




07 June, 2013

C++ Program To Perform Merge Sort.

This Post Contains A C++ Program To Perform Merge Sort 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 'Merge Sort', 'Array', 'Function', 'Nested Loops' 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.


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


C++ Program To Perform Merge Sort.

/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>

/* Function Declaration */
void Merge(int c[], int r[]);
/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
int a[10], b[10], c[20];
int i=0, j=0, x, y;
clrscr();
/* Asking For The Input From User */
cout<<" \n Enter Dimension For Array A : ";
cin>>x;
cout<<" \n Enter "<<x<<" Values For Array A : ";
for(i=0; i<x; i++)
{
   cin>>a[i];
}

cout<<" \n Enter Dimension For Array B : ";
cin>>y;
cout<<" \n Enter "<<y<<" Values For Array B : ";
for(i=0; i<y; i++)
{
   cin>>b[i];
}

/* Source Code For Computing Merge Sort */
for(i=0; i<x; i++)
c[i]=a[i];
while(j<y)
c[i++]=b[j++];
Merge(c,x+y);           // Function Call.
getch();
}
/* End Of Main Program */

/* Function Definition */
void Merge(int c[], int r[])
{

/* Declaration Of Local Variables Of Function */
int k, p, q, fl, fu, sl, su, size, d[20];
size=1;
while(size<r)
{
   fl=0;k=0;
   while(fl+size<r)
   {
      sl=fl+size;
      fu=sl-1;
      su=(sl+size-1<r)?sl+size-1:r-1;
      for(p=fl, q=sl; p<=fu && q<=su; k++)
      {
         if(c[p]<=c[q])
         {
           d[k]=c[p++];
         }
         else
         {
           d[k]=c[q++];
         }
      }
      for(; p<=fu; k++)
      {
         d[k]=c[p++];
      }
      for(; q<=su; k++)
      {
         d[k]=c[q++];
      }
      fl=su+1;
   }
   for(p=fl; k<r; p++)
   {
      d[k++]=c[p];
   }
   for(p=0; p<r; p++)
   {
      c[p]=d[p];
   }
   size*=2;
}
cout<<"\n";
for(q=0; q<r; q++)
   cout<<c[q]<<"\t";
}
/* End Of Function */


Output :

Enter Dimension For Array A : 4
Enter 4 Values For A :
01  2  83  45

Enter Dimension For Array B : 8
Enter 8 Values For B :
65  64  37  82  19  10  25  63

After Merge Sort :
01  10  19  25  29  37  45  63  64  65  82  83




C++ Program To Perform Shell Sort.

This Post Contains A C++ Program To Perform Shell Sort 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 'Shell Sort', 'Array', 'Nested Loops', 'Functions' 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.


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


C++ Program To Perform Shell Sort.
/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>
/* Function Declaration */
void ShellSort(int a[], int b[], int d, intx);
/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
int i, dim, x, a[20], b[5];
clrscr();

/* Asking For The Input From User */
cout<<" \n Enter Dimension For Array : ";
cin>>dim;
cout<<" \n Enter "<<dim<<" Values For Array : ";
for(i=0; i<dim; i++)
{
   cin>>a[i];
}
/* Source Code For Computing Shell Sort */
cout<<" \n Enter Number Of Increments : ";
cin>>x;
cout<<" \n Enter Increments Last 1 : ";
for(i=0; i<x; i++)
{
   cin>>b[i];
}
cout<<" \n Entered Values Of Array Are : ";
for(i=0; i<dim; i++)
{
   cout<<a[i]<<"\t";
}
cout<<" \n Increments Are : ";
for(i=0; i<x; i++)
{
   cout<<b[i]<<"\t";
}

ShellSort(a, b, dim, x);         // Function Call.

getch();
}
/* End Of Main Program */

/* Function Definition */
void ShellSort(int a[], int b[], int d, intx)
{

/* Declaration Of Local Variables Of Function */
int j, k , l, y, inc, span;
for(j=0; j<x; j++)
{
   span=b[j];
   for(k=span; k<d; k++)
   {
      y=a[k];
      for(l=k-span; l>=0&&y<a[l]; l-=span)
       a[l+span]=a[l];
      a[l+span]=y;
   }
}

/* Printing The Output Onto The Screen/Console */
cout<<" \n After Sorting Array Is : ";
for(j=0; j<d; j++)
{
   cout<<a[j]<<"\t";
}
}
/* End Of Function */
Output :

Enter Dimension : 10

Enter 10 Values For Array :
91  28  73  46  50  19  82  37  64  05

Enter Number Of Increments : 3

Enter Increments Last 1 : 4  2  1

Values Of Array Are :
91  28  73  46  50  19  82  37  64  05

Increments : 4  2  1

After Sorting Array Is :
05  19  28  37  46  50  64  73  82  91




04 June, 2013

C++ Program To Perform Heap Sort.

This Post Contains A C++ Program To Perform Heap Sort 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 'Heap 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.



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



C++ Program To Perform Heap Sort.
 
/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>
void hsort(int x[], int n);
/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
int i, dim;
int a[20];
clrscr();
/* Asking For The Input From User */
cout<<" \n Enter Dimension : ";
cin>>dim;

cout<<" \n Enter Values For Array : ";
for(i=0;i<dim;i++)
{
   cin>>a[i];
}

cout<<" \n Entered Values Of Array Are : ";
for(i=0;i<dim;i++)
{
   cout<<a[i]<<"\t";
}

hsort(a, dim);        // Call To The Function.

cout<<" \n After Heap Sort : ";
for(i=0;i<dim;i++)
{
   cout<<a[i]<<"\t";
}

getch();
}
/* End Of Main Program */
/* Source Code For Computing Heap Sort */
/* Start Of Heap Sort Function */
void hsort(int x[], int n)
{
int i, s, f, elt, ivalue;
for(i=1;i<n;i++)
{
   elt=x[i];
   s=i;
   f=(s-1)/2;
   while(s>0  && x[f]<elt)
   {
      x[s]=x[f];
      s=f;
      f=(s-1)/2;
   }
   x[s]=elt;
}
for(i=n-1;i>0;i--)
{
   ivalue=x[i];
   x[i]=x[0];
   f=0;
   if(i==1)
   {
      s=-1;
   }
   else
   {
      s=1;
   }
   if( (i>2) && (x[2]>x[1]) )
   {
      s=2;
   }
   while( (s>=0) && (ivalue<x[s]) )
   {
      x[f]=x[s];
      f=s;
      s=2*f+1;
      if( (s+1<=i-1) && (x[s]<x[s+1]) )
      s=s+1;
      if(s>i-1)
      s=-1;
   }
   x[f]=ivalue;
}
}
/* End Of Heap Sort Function */

 
Output :

Enter Dimension :  5

Enter Values For Array :
3  1  5  2  4

After Heap Sort :
1  2  3  4  5




C++ Program To Perform Quick Sort.

This Post Contains A C++ Program To Perform Quick Sort 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' 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.


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



C++ Program To Perform Quick Sort.


/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>
int dim, a[10];
void qsort(int, int);
/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
int i, l, r;
clrscr();
/* Asking For The Input From User */
cout<<" \n Enter Dimension : ";
cin>>dim;

cout<<" \n Enter Values For Array : ";
for(i=0;i<dim;i++)
{
   cin>>a[i];
}

cout<<" \n Entered Values Of Array Are : ";
for(i=0;i<dim;i++)
{
   cout<<a[i]<<"\t";
}

l=0;
r=dim-1;
qsort(l, r);
cout<<" \n After Quick Sort : ";
for(i=0;i<dim;i++)
{
   cout<<a[i]<<"\t";
}

getch();
}
/* Source Code For Computing Quick Sort */
void qsort(int left, int right)
{
int i, j, p, temp;
if(right>left)
{
   i=left;
   j=right;
   p=a[left];
   while(right>left)
   {
      do
      {
          i++;
      }
      while( (a[i]<=p) && (i<=right) );
      while( (a[j]>=p) && (j>left) )
      {
          --j;
      }
      if(j>i)
      {
         temp=a[i];
         a[i]=a[j];
         a[j]=temp;
      }
   }
   temp=a[left];
   a[left]=a[j];
   a[j]=temp;
   qsort(left, j-1);
   qsort(i, right);
}
}
/* End Of Main Program */

 
Output :
Enter Dimension :  5

Enter Values For Array :
3  1  5  2  4

After Quick Sort :
1  2  3  4  5




C++ Program To Perform Insertion Sort In Ascending Order.

This Post Contains A C++ Program To Perform Insertion Sort In Ascending Order. 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 'Insertion 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.


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



C++ Program To Perform Insertion Sort In Ascending Order.

/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>

/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
int i, j, dim, val;
int a[20];
clrscr();

/* Asking For The Input From User */
cout<<" \n Enter Dimension : ";
cin>>dim;
cout<<" \n Enter The Value : ";
cin>>val;

/* Source Code For Computing Insertion Sort In Ascending Order */
i=0;
while(i<dim)
{
   j=i-1;
   while(val<a[j] && j>=0)
   {
       a[j+1]=a[j];
       --j;
   }
   a[j+1]=val;
   if(i != dim-1)
   {
      cout<<" \n Enter The Value : ";
      cin>>val;
   }
}
getch();
}
/* End Of Main Program */




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




03 June, 2013

C++ Program To Perform Bubble Sort In Descending Order.

This Post Contains A C++ Program To Perform Bubble Sort In Descending Order. With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Bubble 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.


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



C++ Program To Perform Bubble Sort In Descending Order.


/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>

/* Start Of Main Program */
void main()
{
int i, j, dim=0, temp=0;
int a[20];
clrscr();

// Accepting Range Of Array From User.
cout<<" \n Enter Dimension : ";
cin>>dim;

// Accepting Values Of Array From User.
cout<<" \n Enter Values For Array : ";
for(i=0;i<dim;i++)
{
cin>>a[i];
}

// Printing Values Of Array.
for(i=0;i<dim;i++)
{
cout<<a[i]<<"\t";
}

// Sorting Values In Descending Order.
for(i=1;i<=dim;i++)
{
  for(j=1; j<=(dim-i); j++)
  {
    if(a[j]<a[j+1])
    {
       temp=a[j];
       a[j]=a[j+1];
       a[j+1]=temp;
    }
  }
}

// Printing Values Of Array 'a' After Sorting.
cout<<" \n Sorted Array Is : ";
for(i=1;i<=dim;i++)
{
cout<<a[i]<<"\t";
}
getch();
}
/* End Of Main Program */

 
Output :

Enter Dimension : 5

Enter 5 Values :
44  22  11  55  33

Sorted Array Is :
55  44  33  22  11




C++ Program To Perform Selection Sort In Ascending Order.

This Post Contains A C++ Program To Perform Selection Sort In Ascending Order With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Selection 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.


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



C++ Program To Perform Selection Sort In Ascending Order.

/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>

/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
int i, j, dim=0, temp=0;
int a[20];

clrscr();

// Accepting Range Of Array From User.
cout<<" \n Enter Dimension : ";
cin>>dim;

// Accepting Values Of Array From User.
cout<<" \n Enter Values For Array : ";
for(i=0;i<dim;i++)
{
cin>>a[i];
}

// Printing Values Of Array.
for(i=0;i<dim;i++)
{
cout<<a[i]<<"\t";
}

// Sorting Values In Ascending Order.
for(i=0;i<dim;i++)
{
  for(j=i+1;j<dim;j++)
  {
    if(a[i]>a[j])
    {
       temp=a[i];
       a[i]=a[j];
       a[j]=temp;
    }
  }
}

// Printing Values Of Array 'a' After Sorting.
cout<<" \n Sorted Array Is : ";
for(i=0;i<dim;i++)
{
cout<<a[i]<<"\t";
}
getch();
}
/* End Of Main Program */

 
Output :

Enter Dimension : 5

Enter 5 Values :
44  22  11  55  33

Sorted Array Is :
11  22  33  44  55




Subscribe To:

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

Blog Archive