Hi, I wrote this program to implement bubble sort method
Everything is fine except I don't want case sensitive sort.Code:#include <iostream> using namespace std; template <class T> class Bubble { T *array; int length; public: Bubble():length(0),array(0),count(0){} Bubble(int siz):length(siz),count(0){ array=new T[length];} ~Bubble(){delete [] array;} void load_list(T []); void sort_list(T []); void show_list(const char *); void swap(T&,T&); int count; }; template <class T> void Bubble<T>::load_list(T list[]) { for(int i=0;i<length;i++) { array[i]=list[i]; } } template <class T> void Bubble<T>::sort_list(T list[]) { int swapped=1; load_list(list); show_list("Before"); for(int i=0;i<(length-1) && swapped;i++) { swapped=0; count++; for(int j=0;j<(length-(i+1));j++) if(array[j]>array[j+1]) { swap(array[j],array[j+1]); swapped=1; } } show_list("Sorted"); } template <class T> void Bubble<T>::show_list(const char *s) { cout<<s<<endl; for(int i=0;i<length;i++) cout<<array[i]<<" "; cout<<endl; } template <class T> void Bubble<T>::swap(T& x,T& y) { T tmp; tmp=x; x=y; y=tmp; } int main() { Bubble <char>b(4); char arr[]={'B','a','F','D'}; b.load_list(arr); b.sort_list(arr); cout<<b.count; return 0; }
I coud write compare function and call it instead of
array[j]>array[j+1];
In that function I would detect char instance of template like
if(sizeof(T)==sizeof(char))
but that seems to me trickier.
Can I overload operator > and inside detect if T==char , then for example compare with toupper or something like that.
If someone has any code it would be good



LinkBack URL
About LinkBacks


