Program to sort an array of integers in ascending order using selection sort.

#include<iostream>
using namespace std;
int main()
{
    int i,j,n,loc,temp,min,a[30];
    cout<<"Enter the number of elements:";
    cin>>n;
    cout<<"\nEnter the elements\n";
    for(i=0;i<n;i++)
   {
       cin>>a[i];
   }
    for(i=0;i<n-1;i++)
   {
       min=a[i];
       loc=i;
       for(j=i+1;j<n;j++)
       {
           if(min>a[j])
           {
               min=a[j];
               loc=j;
           }
       }

       temp=a[i];
       a[i]=a[loc];
       a[loc]=temp;
   }
    cout<<"\nSorted list is as follows\n";
   for(i=0;i<n;i++)
   {
       cout<<a[i]<<" ";
   }
    cout<<"\n/*\nName-Sanjampreet Singh\nRoll Number-1507967\n*/";
    return 0;
}

Output:


-Thanks Sachin Bagga Sir!

Comments

Popular posts from this blog

To create an html page with frames and frameset

1. Write a program to insert a new element at end as well as at a given position in an array in Java

1. To familiarize with network devices like switch, hub, routers and bridges in LaTex