Saturday, June 26, 2021

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

 

Problem Statement:

            Implement a function template selection sort. Write a program that inputs, sort & output on integer array and a float array.

 

Objectives:

       To learn the concept of template.

 

Algorithm:

    1)     Start
    2)    Declare the template parameter T.
    3)    Define template function for selection sort.
    4)    In main() Define two arrays, one for integer & another for float and take input for both the arrays            all sorting function template to sort the number.
    5)    Stop.


Program:

#include<iostream>

using namespace std;

int n;

#define size 10

template<class T>

void sel(T A[size])

{

int i,j,min;

T temp;

for(i=0;i<n-1;i++)

{

min=i;

for(j=i+1;j<n;j++)

{

if(A[j]<A[min])

min=j;

}

temp=A[i];

A[i]=A[min];

A[min]=temp;

}

cout<<"\nSorted array:";

for(i=0;i<n;i++)

{

cout<<" "<<A[i];

}

}

int main()

{

int A[size];

float B[size];

int i;

int ch;

do

{

cout<<"\n* * * * * SELECTION SORT SYSTEM * * * * *";

cout<<"\n--------------------MENU-----------------------";

cout<<"\n1. Integer Values";

cout<<"\n2. Float Values";

cout<<"\n3. Exit";

cout<<"\n\nEnter your choice : ";

cin>>ch;

switch(ch)

{

case 1:

cout<<"\nEnter total no of int elements:";

cin>>n;

cout<<"\nEnter int elements:";

for(i=0;i<n;i++)

{

cin>>A[i];

}

sel(A);

break;

case 2:

cout<<"\nEnter total no of float elements:";

cin>>n;

cout<<"\nEnter float elements:";

for(i=0;i<n;i++)

{

cin>>B[i];

}

sel(B);

break;

case 3:

exit(0);

}

}while(ch!=3);

return 0;

}

 

Output:

* * * * * SELECTION SORT SYSTEM * * * * *

--------------------MENU-----------------------

1. Integer Values

2. Float Values

3. Exit

 

Enter your choice : 1

 

Enter total no of int elements:5

 

Enter int elements:45

99

79

05

11

 

Sorted array: 5 11 45 79 99

* * * * * SELECTION SORT SYSTEM * * * * *

--------------------MENU-----------------------

1. Integer Values

2. Float Values

3. Exit

 

Enter your choice : 2

 

Enter total no of float elements:5

 

Enter float elements:25.3

55.9

11.8

78.4

68.0

 

Sorted array: 11.8 25.3 55.9 68 78.4

* * * * * SELECTION SORT SYSTEM * * * * *

--------------------MENU-----------------------

1. Integer Values

2. Float Values

3. Exit

 

Enter your choice : 3

 

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

Process exited after 184.3 seconds with return value 0

Press any key to continue . . .








0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home