Sunday 6 October 2013

Selection Sort code in C++

//selection sorting
#include<iostream.h>
using namespace std;
#include<conio.h>
template <class T>
void selectionsort(T a[],int n)
{
     int i,j,temp;
     for(i=0;i<n;i++)
     {
                     for(j=i+1;j<n;j++)
                     {
                                       if(a[j]<a[i])
                                       {
                                                    temp=a[i];
                                                    a[i]=a[j];
                                                    a[j]=temp;
                                       }
                     }
     }
}
int main()
{
    int a[50],i,n;
    cout<<"enter number of elements do u want to sort :";
    cin>>n;
    cout<<"enter elements to sort:";
    for(i=0;i<n;i++)
    {
                    cin>>a[i];
    }
    selectionsort(a,n);
    cout<<"after sorting the elements are....\n";
    for(i=0;i<n;i++)
    {
                    cout<<a[i]<<"\n";
    }
    _getch();
    return 0;
}

No comments:

Post a Comment