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
# 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
No comments:
Post a Comment