Thread: sort function help

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    75

    sort function help

    hi, in this sort function i need some help.....is this sort function sorting by swapping the values in the nodes, or does a swap by swapping the nodes itself , after comparing the values?

    Code:
    void dList<Type>::sort()
    {
    	for(nodeType<Type>* temp=first;temp != NULL; temp=temp->next)
    	{
    		nodeType<Type>* min = temp;
    
    		for(nodeType<Type>* current = min; current != NULL; current=current->next)
    		{
    			if(current->info < min->info)min=current;
    		}
    
    		Type temp_var=temp->info;
    		temp->info=min->info;
    		min->info=temp_var;
    	}
    }

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    It swaps the values.Modify this sort function so it swaps the nodes insteed (improves performance).

    P.S.
    change this line to
    Code:
    for(nodeType<Type>* current = min; current != NULL; current=current->next)
    Code:
    for(nodeType<Type>* current = min->next; current != NULL; current=current->next)
    because you are comparing two identical values each time you enter this for-loop.
    Last edited by ripper079; 09-16-2003 at 04:59 AM.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things don´t come easy in life!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Compile problem: sort() cannot be used as a function!?
    By ac251404 in forum C++ Programming
    Replies: 2
    Last Post: 06-01-2006, 12:58 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Replies: 10
    Last Post: 09-15-2004, 01:00 PM