1 Replies - 432 Views - Last Post: 06 October 2012 - 10:40 PM Rate Topic: -----

#1 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

sorting a linked list issue

Posted 06 October 2012 - 09:59 PM

It seems as though I am having some issues sorting a linked list...
It doesn't sort it, all it does is print out the order inwhich things are entered, i'm trying to use a simple bubble sort since the information being sorted is not very large.
void circularList::sortByKey(string key)
{
	bool swap = true;
	int keyCount = 0;

	if(islower(key[keyCount]))
		toupper(key[keyCount]);	// Make sure the first letter of the name is capitalized
	
	while(keyCount != key.size() && key[keyCount] != '\0')
	{
		keyCount++;
		if(key[keyCount] == ' ' && key[keyCount] != '\0')
			key[keyCount] = toupper(key[keyCount + 1]);		// Ensure the last name is also capitalized
	}
	
	dataNode* temp = head;
	dataNode* link = head;
	dataNode* sorted = head;
	do
	{
		swap = false;
		while(link->next != NULL)
		{
			// A-Z bubble sort
			if(temp->next->customer > key)	// If customer > key, both string type
			{
				sorted = temp;
				temp = temp->next;
				sorted->next = temp;
				swap = true;
			}
			link = link->next;
		}
	}while(swap);
	
	cout << "\n\nSorted by key [" << key << "]" << endl;
	while( sorted != NULL)
	{
		cout << sorted->customer << " "
			<< sorted->magician << " "
			<< sorted->holiday << endl;
		sorted = sorted->next;
	}
}



I need some help as to where I am going wrong with this.

Is This A Good Question/Topic? 0
  • +

Replies To: sorting a linked list issue

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3652
  • View blog
  • Posts: 11,421
  • Joined: 05-May 12

Re: sorting a linked list issue

Posted 06 October 2012 - 10:40 PM

Shouldn't a sort be sorting against values within the container, not against a value that is passed in? Your line 25 is only comparing against a key value that is passed in.

Additionally, your swap on lines 27-29 don't seem to actually be doing a swap.

I recommend simulating your sort algorithm on paper or the whiteboard and see what is happening. This will help you figure out what changes you need to make.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1