Write a function template selection Sort. Write a program that inputs, sorts and outputs an int array and a float array.


#include<iostream>
#include<limits>
using namespace std;

template<class T>       
       
       
        T selection_sort()        //Template function
        {
        T a[10];
        T temp;
        for(int i=0;i<10;i++)        //Accepting elements
            {
                cout<<"a["<<i<<"]=";
                cin>>a[i];
            }
           
   
         for(int i=0;i<10;i++)            //Sorting logic
         {
              for(int j=i+1;j<10;j++)
              {
                   if(a[i]>a[j])
                   {
                        temp=a[i];
                        a[i]=a[j];
                       a[j]=temp;
                }
            }
         }

         for(int i=0;i<10;i++)        //Displaying logic
        {
            cout<<a[i]<<"\n";
        }
        }


int main()
{
       
       
    cout<<"Integer sorting...\n";
   
    selection_sort<int>();
   
    cout<<"Floating sorting...\n";
   
    selection_sort<float>();
       
       
}

-------------------------------------------------------------------------------

Output

[student@localhost Pratiksha]$ g++ Selection.cpp
[student@localhost Pratiksha]$ ./a.out
Integer sorting...
a[0]=1
a[1]=2
a[2]=4
a[3]=3
a[4]=6
a[5]=5
a[6]=7
a[7]=0
a[8]=9
a[9]=8
0
1
2
3
4
5
6
7
8
9
Floating sorting...
a[0]=9.7
a[1]=6.3
a[2]=5.4
a[3]=8.6
a[4]=3.2
a[5]=1.2
a[6]=4.3
a[7]=7.3
a[8]=9.5
a[9]=8.6
1.2
3.2
4.3
5.4
6.3
7.3
8.6
8.6
9.5
9.7
[student@localhost Pratiksha]$

Comments

  1. selection.cpp:36:9: warning: control reaches end of non-void function [-Wreturn-type]
    }
    ^
    selection.cpp:45:5: note: in instantiation of function template specialization
    'selection_sort' requested here
    selection_sort();
    ^
    selection.cpp:36:9: warning: control reaches end of non-void function [-Wreturn-type]
    }
    ^
    selection.cpp:49:5: note: in instantiation of function template specialization
    'selection_sort' requested here
    selection_sort();
    ^
    2 warnings generated.

    ReplyDelete

Post a Comment

Popular posts from this blog

Priority Scheduling Algorithm Java Program.

Implement UNIX system calls like ps, fork, join, exec family, and wait for process management (use shell script/ Java/ C programming).

Implement a class CppArray which is identical to a one-dimensional C++ array (i.e., the index set is a set of consecutive integers starting at 0) except for the following : 1. It performs range checking. 2.It allows one to be assigned to another array through the use of assignment operator. 3.It supports a function that returns the size of the array.