compare issue while sorting

This is a discussion on compare issue while sorting within the C++ Programming forums, part of the General Programming Boards category; Hi, I wrote this program to implement bubble sort method Code: #include <iostream> using namespace std; template <class T> class ...

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712

    compare issue while sorting

    Hi, I wrote this program to implement bubble sort method
    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;
    }
    Everything is fine except I don't want case sensitive sort.
    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

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Have you considered specializing the template for sorting characters?
    Code:
    template <>
    class Bubble<char>
    {
      char *array;
      int length;
    public:
      Bubble():length(0),array(0),count(0){}
      Bubble(int siz):length(siz),count(0){ array=new char[length];}
      ~Bubble(){delete [] array;}
      void load_list(char []);
      void sort_list(char []);
      void show_list(const char *);
      void swap(char&,char&);
      int count;
    };
    
    void Bubble<char>::load_list(char list[])
    {
      for(int i=0;i<length;i++)
      {
        array[i]=list[i];
      }
    }
    
    void Bubble<char>::sort_list(char 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(toupper(array[j])>toupper(array[j+1]))
          {
            swap(array[j],array[j+1]);
            swapped=1;
          }
    
      }
      show_list("Sorted");
    
    }
    
    void Bubble<char>::show_list(const char *s)
    {
      cout<<s<<endl;
      for(int i=0;i<length;i++)
        cout<<array[i]<<" ";
      cout<<endl;
    
    }
    
    void Bubble<char>::swap(char& x,char& y)
    {
      char tmp;
      tmp=x;
      x=y;
      y=tmp;
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  3. Sorting Algorithm Help
    By cjwenigma in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2007, 02:04 PM
  4. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  5. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21