/* Problem Statement: Function template to perform selection sort on integer and float array in C++
Author: Pavan R Jaiswal
*/ #include "iostream" using namespace std; class SelectionSort{ public: template <typename type> //keyword "typename" can be replaced by keyword "class" type selectionSort(type tarr[5]){ // cout<<tarr[4]; int min; float temp; for(int i=0;i<5;i++){ min=i; for(int j=i+1;j<5;j++){ if(tarr[min] > tarr[j]){ min=j; } } temp = tarr[min]; tarr[min] = tarr[i]; tarr[i] = temp; } cout<<"\nSorted array: "<<endl; for(int cnt=0; cnt<5; cnt++) cout<<tarr[cnt]<<endl; } }; int main(){ SelectionSort ob; int intArr[5]; cout<<"\n Enter 5 elements of type integer: "; for(int cnt=0; cnt<5; cnt++) cin>>intArr[cnt]; cout<<"\nInteger Array entered by you: "; for(int cnt=0; cnt<5; cnt++) cout<<"\t"<<intArr[cnt]; ob.selectionSort(intArr); float floatArr[5]; cout<<"\n Enter 5 elements of type float: "; for(int cnt=0; cnt<5; cnt++) cin>>floatArr[cnt]; cout<<"\nfloat Array entered by you: "; for(int cnt=0; cnt<5; cnt++) cout<<"\t"<<floatArr[cnt]; ob.selectionSort(floatArr); return 0; } /* ####### OUTPUT ######## [pavan@localhost Programs]$ g++ SelectionSort.cpp -o SelectionOp [pavan@localhost Programs]$ ./SelectionOp Enter 5 elements of type integer: 2 3 1 4 54 Integer Array entered by you: 2 3 1 4 54 Sorted array: 1 2 3 4 54 Enter 5 elements of type float: 2.3 1.2 3.5 56.7 67 float Array entered by you: 2.3 1.2 3.5 56.7 67 Sorted array: 1.2 2.3 3.5 56.7 67 [pavan@localhost Programs]$ */
No comments:
Post a Comment