Thread: problems with greatest to least sorting

  1. #1
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003

    problems with greatest to least sorting

    I have this linked list class that stores int variables. Each int variable is placed in it's own node. I want to put the nodes in order, greatest to least. I thought of an idea where you have a loop that goes through each node and another loop inside of that that goes through each node also. The first loop chooses what node is being "scored". The node's score determines where it is moved to in the linked list. The second loop "scores" the node the first loop chose by comparing it's int's value against the node that second loop selected's int. If the first node's int is greater than the other one, it gets a point. After the second loop is done executing, you calculate what element number the node that was being scored gets by taking the opposite of the score and adding the length of the list to that. I hope I explained it well. Anyway, I'm having a lot of trouble with that idea. I have no idea why I get unhandled exception errors when I try it, so I'm asking for your help! If you know of a better way to sort stuff from greatest to least, I beg you to tell me. Before I show you my code, I'm going to tell a little about some of the functions my class uses. The important function that my sorting function (sortgtl) uses is the moveto function. It's arguments are the element numbers of two nodes. The nodes corresponding to these numbers switch places in the linked list. I haven't used the elementdel function as I thought I was going to, so I guess I'll stop explaining and show you the code:

    class intarr
    	class node
    		int data;
    		node * next;
    		node * prev;
    		node & operator=(int);
    		friend bool operator>(node & a, node & b) { return >; }
    		friend bool operator<(node & a, node & b) { return <; }
    		friend bool operator>=(node & a, node & b) { return >=; }
    		friend bool operator<=(node & a, node & b) { return <=; }
    	node * head;
    	node * current;
    	unsigned int len;
    	intarr() : head(0), current(head) {}
    	intarr(int *, unsigned int);
    	node & operator[](unsigned int);
    	void elementdel(unsigned int);
    	void moveto(unsigned int, unsigned int);
    	void sortgtl();
    intarr::node & intarr::node::operator=(int n)
    	this->data = n;
    	return *this;
    intarr::intarr(int * d, unsigned int l)
    	head = new node;
    	current = head;
    	for(unsigned int a = 0; a < l; a++)
    		current->data = d[a];
    		current->next = new node;
    		current->next->prev = current;
    		current = current->next;
    	len = l;
    intarr::node & intarr::operator[](unsigned int i)
    	current = head;
    	for (unsigned int a = 0; a < i; a++)
    		current = current->next;
    	return *current;
    void intarr::elementdel(unsigned int e)
    	node * deadman = &(operator[](e));
    	if(deadman != head)
    		deadman->prev->next = deadman->next;
    		deadman->next->prev = deadman->prev;
    		delete deadman;
    void intarr::moveto(unsigned int m, unsigned int l)
    	node * mover = &(operator[](m));
    	node * location = &(operator[](l));
    	node * temp = new node;
    	*temp = *mover;
    	mover->next = location->next;
    	mover->prev = location->prev;
    	location->next = temp->next;
    	location->prev = temp->prev;
    	delete temp;
    void intarr::sortgtl()
    	for (unsigned int a = 0; a < len; a++)
    		unsigned int points = 0;
    		for (unsigned int b = 0; b < len; b++)
    			if (operator[](a) > operator[](b))
    		moveto(a, -points + len);

  2. #2
    Registered User
    Join Date
    Jan 2004
    The exception errors are probably due to problems within your linked list. For a simple sorting algoritm, google for 'bubble sort'.
    “Focused, hard work is the real key to success. Keep your eyes on the goal, and just keep taking the next step towards completing it. " -John Carmack

  3. #3
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Bubble Sorts lol. Not the best sorting algorithm. Check out QuickSort or do a search for sorting algorithms.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting problems.
    By Sedvan in forum C++ Programming
    Replies: 2
    Last Post: 02-14-2008, 01:13 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  5. Sorting structures
    By RedRum in forum C++ Programming
    Replies: 2
    Last Post: 05-23-2002, 12:19 PM