Thread: Help with array sorting please

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    25

    Help with array sorting please

    I don't understand why this won't sort it properly?

    Code:
    template<class T>
    class Prob2Sort
    {
    	private:
    		int *index;                                 //Index that is utilized in the sort
    	public:
    		Prob2Sort(){index=NULL;};                   //Constructor
    		~Prob2Sort(){delete []index;};              //Destructor
    		T * sortArray(const T*,int,bool);           //Sorts a single column array
    		T * sortArray(const T*,int,int,int,bool);   //Sorts a 2 dimensional array
    }; 
    
    
    
    template <class T>
    T * Prob2Sort<T>::sortArray(const T* a,int ind1,int ind2,int col,bool ord)
    {
        index=new int[ind1];
        T *sorted;
        for(int i=0; i<ind1; i++)
        {
            index[i]=i;
        }
    
    
        if (ord==false)
        {
            for(int i=0;i<ind1;i++)
            {
                for(int j=0;j<ind1;j++)
                {
                    if(a[index[i]*col]>a[index[j]*col])
                    {
                        index[i]=index[i]^index[j];
                        index[j]=index[i]^index[j];
                        index[i]=index[i]^index[j];
                    }
                }
            }
        }
        if (ord==true)
        {
            for(int i=0;i<ind1;i++)
            {
                for(int j=0;j<ind1;j++)
                {
                    if(a[index[i]*col]<a[index[j]*col])
                    {
                        index[i]=index[i]^index[j];
                        index[j]=index[i]^index[j];
                        index[i]=index[i]^index[j];
                    }
                }
            }
        }
    
        sorted= new T[ind1*ind2];
        for(int i=0; i<ind1; i++)
        {
            for(int j=0; j<ind2; j++)
            {
                sorted[i*ind2+j]=a[index[i]*ind2+j];
            }
        }
        return sorted;
    }
    
    int main()
    {
      	cout<<"The start of Problem 2, the sorting problem"<<endl;
    	Prob2Sort<char> rc;
    	bool ascending=true;
    	ifstream infile;
    	infile.open("Problem2.txt",ios::in);
    	char *ch2=new char[10*16];
    	char *ch2p=ch2;
    	while(infile.get(*ch2)){cout<<*ch2;ch2++;}
    	infile.close();
    	cout<<endl;
    
    	cout<<"Sorting on which column"<<endl;
    	int column;
    	cin>>column;
    	cout<<endl;
    	char *zc=rc.sortArray(ch2p,10,16,column,ascending);
    	for(int i=0;i<10;i++)
    	{
    		for(int j=0;j<16;j++)
    		{
    			cout<<zc[i*16+j];
    		}
    	}
    	delete []zc;
    	cout<<endl;
            return 0;
    
    }
    this is my output

    The start of Problem 2, the sorting problem
    Lcekoeddhoffbmg
    Lkcmggjcdhhglif
    Cgldjhcekjigcdd
    Cgldjhcekjigcdn
    Bffmdbkcenlafjk
    Fggdijijegfblln
    Jjlncnimjldfedj
    Amliglfohajcdmm
    Balgfcaelhfkgeb
    Kmlhmhcddfoeilc

    Sorting on column
    15

    Lcekoeddhoffbmg
    Lkcmggjcdhhglif
    Cgldjhcekjigcdd
    Cgldjhcekjigcdn
    Bffmdbkcenlafjk
    Fggdijijegfblln
    Jjlncnimjldfedj
    Amliglfohajcdmm
    Balgfcaelhfkgeb
    Kmlhmhcddfoeilc

    it should be sorting according to column 15 and look like this
    Cgldjhcekjigcdn
    Fggdijijegfblln
    Amliglfohajcdmm
    Bffmdbkcenlafjk
    Jjlncnimjldfedj
    Lcekoeddhoffbmg
    Lkcmggjcdhhglif
    Cgldjhcekjigcdd
    Kmlhmhcddfoeilc
    Balgfcaelhfkgeb

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You appear to be looking at indexes 15, 30, 45, 60, 75, etc. I think you want to be looking at 0*16+15, 1*16+15, 2*16+15, 3*16+15, etc.

    Actually, I'm kidding: you want to be looking at 0*16+14, 1*16+14, 2*16+14, etc. Otherwise all you'd be sorting on are the \n characters, since the 15th column is index 14.

    And really: xor swap? for integers? ewwww. (After all, there is std::swap()....)

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    25
    Thank you so much. It must really be late if i missed that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-31-2009, 12:34 PM
  2. two dimensional array sorting in C/C++
    By George2 in forum C Programming
    Replies: 16
    Last Post: 11-19-2006, 03:17 AM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM