Bubble sorting with Function templates
Jun 21, 2013 at 4:49pm UTC
hey guys i'm new to C++ i came across this bubblesort program earlier relating to class templates i was wondering how to make this into
simple function templates
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
#include<iostream>
#include<iomanip.h>
#include<conio.h>
#include<stdio.h>
using namespace std;
template <class t>
class bubble
{
t a[25];
public :
void get(int );
void sort(int );
void display(int );
};
template <class t>
void bubble <t>::get(int n)
{
int i;
cout<<"Enter the array elements:\n" ;
for (i=0; i<n;i++)
cin>>a[i];
}
template <class t>
void bubble <t>::display(int n)
{
int i;
cout<<"\nThe sorted array is\n" ;
for (i=0;i<n;i++)
cout<<a[i]<<setw(10);
}
template <class t>
void bubble <t>::sort(int n)
{
int i,j;
t temp;
for (i=0;i<n;i++)
{
for (j=i+1;j<n;j++)
{
if (a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
int main()
{
int n,ch;
while (1)
{
cout<<"\n\n----BUBBLE SORT TEMPLATES----\n" ;
cout<<"\nEnter your choice\n1.Integer\n2.Floating Point\n3.Character\n4.Exit\n\n" ;
cin>>ch;
switch (ch)
{
case 1:
{
cout<<"\n### Integer Sortmenu ###\n\n" ;
bubble<int > b1;
cout<<"Enter the size of array:\n" ;
cin>>n;
b1.get(n);
b1.sort(n);
b1.display(n);
break ;
};
case 2:
{
cout<<"\n### Floating Point SortMenu ###\n\n" ;
bubble<float > b2;
cout<<"Enter the size of array:\n" ;
cin>>n;
b2.get(n);
b2.sort(n);
b2.display(n);
break ;
};
case 3:
{
cout<<"\n### Character SortMenu ###\n\n" ;
bubble<char > b3;
cout<<"Enter the size of array:\n" ;
cin>>n;
b3.get(n);
b3.sort(n);
b3.display(n);
break ;
};
case 4:
{
cout<<"<Program terminating>" ;
exit(0);
};
default :
cout<<"Invalid Choice try again!\n" ;
return 0;
}
}
getch();
}
Thanks
dexter
Jun 21, 2013 at 7:09pm UTC
Identical to your code for the member function sort().
Except that the array (pointer to first element) is an extra parameter:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// template <class t>
// void bubble <t>::sort(int n)
template < typename T > void bubble_sort( T a[], int n )
{
int i,j;
//t temp;
T temp ;
for (i=0; i<n; i++)
{
for (j=i+1; j<n; j++)
{
if (a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
Jun 22, 2013 at 1:24am UTC
What would other functions be changed into then ?
Jun 22, 2013 at 6:32am UTC
Oh, come on!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include <iostream>
template < typename T > void get( T a[], int n )
{
std::cout << "enter " << n << " array elements: " ;
for ( int i = 0 ; i < n ; ++i ) std::cin >> a[i] ;
}
template < typename T > void sort( T a[], int n )
{
for ( int i=0 ; i<n ; ++i )
{
for ( int j=i+1 ; j<n ; ++j )
{
if ( a[j] < a[i] )
{
int temp = a[i] ;
a[i] = a[j] ;
a[j] = temp ;
}
}
}
}
template < typename T > void display( const T a[], int n )
{
std::cout << "the sorted array: " ;
for ( int i = 0 ; i < n ; ++i ) std::cout << a[i] << ' ' ;
std::cout << '\n' ;
}
int main()
{
constexpr int N = 5 ;
int array[N] = {0} ;
get( array, N ) ;
sort( array, N ) ;
display( array, N ) ;
}
http://ideone.com/7sV6aq
Topic archived. No new replies allowed.