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

/*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:

  1. declaration of syntax error..
    and #include

    ReplyDelete
  2. declaration of syntax error;
    #include

    ReplyDelete
  3. 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)

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

    ReplyDelete
  5. 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.

    ReplyDelete
  6. 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.

    ReplyDelete