Write a C++ program to sort array using selection sort method.

Program code
#include<iostream>
using namespace std;
int main()
{
int size, array[50];
int i, j, temp;
cout<<"Enter Array Size for selection sorting : ";
cin>>size;
cout<<endl;
cout<<"Enter Array Elements one by one : ";
for(i=0; i<size; i++)
{
cin>>array[i];
}
cout<<"Sorting array using selection sort...\n";
for(i=0; i<size; i++)
{
for(j=i+1; j<size; j++)
{
if(array[i]>array[j])
{
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
cout<<"Now the Array after sorting is :\n";
for(i=0; i<size; i++)
{
cout<<array[i]<<" ";
}
return 0;
}


The program code is tested on www.jdoodle.com
Output:
Enter Array Size for selection sorting : 9
Enter Array Elements one by one : 
2 4 5 1 3 7 8 9 0
Sorting array using selection sort...
Now the Array after sorting is :
0 1 2 3 4 5 7 8 9 


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: