Jump to content

sorting lists

- - - - -

  • Please log in to reply
2 replies to this topic

#1
ghostrider_

ghostrider_

    Newbie

  • Members
  • PipPip
  • 13 posts
i want to sort a linked list in c..

i have understood the algorithm but it seems i am having problems with the code..

i am trying mergesort..any help?

#2
julmuri

julmuri

    Programmer

  • Members
  • PipPipPipPip
  • 139 posts
You should try bubble sort.
Merge sort works best when you have random access.
std::string s("oberq zhpu?");std::for_each(s.begin(),s.end(),[&](char&c){c=~c;c=~c-0x01/(~(c|0x20)/0x0D*0x02-0x0B)*0x0D;});std::cout<<s;

#3
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
When sorting lists its best to swap the data in the nodes, not the nodes themselves because its just easier, especially if you put all data in a structure then have the structure in the node. For example
struct data
{
    char last_name[20];
    char first_name[20];
    char street_address[40];
    char city[20];
};

struct node
{
   struct node *next;
   struct node *previous;
   struct data d;
};

Now in your program when a swap is needed all you have to do is swap the data structures instead of the next and previous pointers.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users