C++ Program Code for Selection Sort (Ascending Order) of array elements, with template class

7 comments
/*The following C++ Program Code for Selection sorting algorithm implementation which gives a sorted array arranged in ascending order. The template class is used to have flexibility in the type of data, whether integer, float, or character it will work for all. In order to sort the array in descending order, just change the condition  if(a[j]<a[i]) to if(a[j]>a[i])*/

#include<iostream>
#include<conio.h>

using namespace std;


template <class T>
void s_sort(T a[],int n)
{
     int i,j,t;
     for(i=0;i<n;i++)
     {
                     for(j=i+1;j<n;j++)
                     {
                                       if(a[j]<a[i]) //
for descending order use if(a[j]>a[i])
                                       {
                                                    t=a[i];
                                                    a[i]=a[j];
                                                    a[j]=t;
                                       }
                     }
     }
}


int main()
{
    int a[100],i,n;
    cout<<"Enter The number of Element:\n";
    cin>>n;
    cout<<"\nEnter Elements:\n";
    for(i=0;i<n;i++)
    {

                      cout<<"\nEnter:";
                    cin>>a[i];
    }
    s_sort(a,n);
    cout<<"\nAfter Sorting\n";
    for(i=0;i<n;i++)
    {
                    cout<<a[i]<<"\t";
    }
    getch();
    return 0;
}


Screenshot:
C++ Program Code for Selection Sort (Ascending Order) of array elements, with template class

7 comments:

Unknown said...

declaration of syntax error..
and #include

Unknown said...

declaration of syntax error;
#include

Vibhutesh said...

If using turbo C++ (or ANSI Version) use #include instead of #include, this might solve your problem. If problem persist please give the full error (with the error code)

Anonymous said...

thank you

Unknown said...

Whats the difference... In what you have mentioned... both are "#include"
@VibhuteshKumarSingh

Vibhutesh said...

Yes, both are seen as #include only. The website parser has understood "< something.h >" as a HTML tag. What I had written, was If using turbo C++ (or ANSI Version) use #include instead of #include.

Vibhutesh said...

Yes, both are seen as #include only. The website parser has understood "< something.h >" as a HTML tag. What I had written, was If using turbo C++ (or ANSI Version) use #include "iostream.h" instead of #include "iostream".
PS. it again happened in the above comment.