Mergesort on a Singly Linked List C++ -


i looking kind of simple way can learn , understand merge sort on these. have looked on web , has been found merge sort singly linked lists cant understand how it. websites found: wikipedia merge sort , specifically linked lists

i not sure code give you. have in header file , new this basic. thank in advance :)

class node { public:     int data;     node* next;     node()     {         next = null;         data = 0;     } };  class sllintstorage { public:     node* head;     node* current;     node* tail;      void read(istream&);     void write(ostream&);     void setreadsort(bool);     void sortown();     void print();      bool _sortread;     int numberofints;      sllintstorage(const sllintstorage& copying)     {      }      sllintstorage(void);     ~sllintstorage(void); }; 

if @ paragraph wikipedia

conceptually, merge sort works follows

  1. if list of length 0 or 1, sorted. otherwise:
  2. divide unsorted list 2 sublists of half size.
  3. sort each sublist recursively re-applying merge sort.
  4. merge 2 sublists 1 sorted list.

this tells pretty succinctly need , kind of operations need. sentences 2 , 4 operations need able split() , merge(). split implemented on node class

// split: removes half of list , returns pointer node* node::split() { }  

similarly merge implemented

// merge: insert elements source in order node::merge(node* source) { } 

as whole 1,2,3,4 describe need perform actual sorting implement these steps in order in sorting function using list operations merge , split.

this 1 way merge , split like, how implement them depend on style, requirements, knowledge of c++ , various other factors. (i sure see other solutions in answers). need node::append(node* val) or similar basic operator. not need node::remove(node* val) might not hurt implement too.


Comments