C++ PRACTICAL PROGRAMS

Class 12 Computer Science | ASSEB

Program to sort the elements of an array using selection sort

#include<iostream>

using namespace std;

int main( )

{

int n,i,j,temp,array[20];

cout<<”Enter total number of elements”;

cin>>n;

cout<<”Enter the elements”;

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

cin>>array[i];

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

{

for(j=i+1;j<n;j++)

{

if(array[i]>array[j])

{

temp=array[i];

array[i]=array[j];

array[j]=temp;

}       

}

}

cout<<”After sorting values are ”;

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

{

cout<<”\n”;

cout<<array[i];

}

return 0;

}

Output

Enter total number of elements6

Enter the elements5 8 2 1 4 3

After sorting values are 1 2 3 4 5 8

QUICK LINKS

Class 11

Class 12

Previous Year Paper

Practical Programs

Scroll to Top