Performing MergeSort on a singly linked list

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 9
Reputation: seanl1 is an unknown quantity at this point 
Solved Threads: 0
seanl1 seanl1 is offline Offline
Newbie Poster

Performing MergeSort on a singly linked list

 
0
  #1
Feb 7th, 2009
Alright guys, I'm in a Data Structures I class which is proving to be impossible at some junctures. I suppose it's a right of passage to earn the degree. I know how mergesort works on Arrays, but our professor has purposely used the linked list data structure to up the difficulty. Through Google, I think I've found a great resource to help me out a bit.

http://www.chiark.greenend.org.uk/~s.../listsort.html

If you guys could help explain it in terms of C# it'd go a long way in helping me solve this problem. Thanks.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 32
Reputation: subtercosm is an unknown quantity at this point 
Solved Threads: 1
subtercosm subtercosm is offline Offline
Light Poster

Re: Performing MergeSort on a singly linked list

 
0
  #2
Feb 7th, 2009
Originally Posted by seanl1 View Post
I suppose it's a right of passage to earn the degree.
You mean a rite of passage. Also, it's the most important or second-most important class you'll take.

What version of C# are you familiar with? If you answer in the next few minutes, I'll tailor my answer to Math.Max(2.0, version) where version is the version you reply with.

Also, if it is important, specify whether you want to work with doubly-linked linked lists or singly-linked. If you have no preference, I will go with singly-linked.

Here is a partial answer:

The one feature of a linked list merge sort is that you can sort the list in place -- the only pieces of the nodes that you modify are the references between nodes. The basic algorithm for merge sort is the same: cut the list in two, sort the parts, and merge them back together. It's hard to understand the source of your confusion -- one might be confusion with linked lists in general, another might be confusion about the algorithm I just described.
Last edited by subtercosm; Feb 7th, 2009 at 12:51 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 9
Reputation: seanl1 is an unknown quantity at this point 
Solved Threads: 0
seanl1 seanl1 is offline Offline
Newbie Poster

Re: Performing MergeSort on a singly linked list

 
0
  #3
Feb 7th, 2009
Excuse my grammar, I did mean "rite" of passage. As far as what version I'm using, I know I'm using Microsoft C# Express 2008. Also, we're working with Singly Linked lists.
Last edited by seanl1; Feb 7th, 2009 at 1:10 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 32
Reputation: subtercosm is an unknown quantity at this point 
Solved Threads: 1
subtercosm subtercosm is offline Offline
Light Poster

Re: Performing MergeSort on a singly linked list

 
0
  #4
Feb 7th, 2009
Good. Visual Studio 2008 uses C# 3.0, the latest version.

So suppose you have a linked list type defined by Node:
  1. class Node<T> {
  2. public T Value { get; set; }
  3. public Node<T> Tail { get; set; }
  4. }

The question is: How would we implement a naive, in-place merge sort?

You'd probably want separate functions that implement parts of the merge sort algorithm:
  1. // cuts a list into equal halves
  2. static void CutInHalf(Node<T> list, out Node<T> headOfLastHalf);
  3.  
  4. // merges two sorted lists into a single sorted list, returning the
  5. // reference of the head of the sorted list (which will either be
  6. // the head of a or the head of b)
  7. static Node<T> Merge(Node<T> a, Node<T> b);

So implement those functions and combine them into the traditional merge sort algorithm.

Any questions?

How much experience do you have with linked lists? Can you do something simple, like reversing a linked list in place?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 9
Reputation: seanl1 is an unknown quantity at this point 
Solved Threads: 0
seanl1 seanl1 is offline Offline
Newbie Poster

Re: Performing MergeSort on a singly linked list

 
0
  #5
Feb 7th, 2009
Originally Posted by subtercosm View Post
Good. Visual Studio 2008 uses C# 3.0, the latest version.

So suppose you have a linked list type defined by Node:
  1. class Node<T> {
  2. public T Value { get; set; }
  3. public Node<T> Tail { get; set; }
  4. }

The question is: How would we implement a naive, in-place merge sort?

You'd probably want separate functions that implement parts of the merge sort algorithm:
  1. // cuts a list into equal halves
  2. static void CutInHalf(Node<T> list, out Node<T> headOfLastHalf);
  3.  
  4. // merges two sorted lists into a single sorted list, returning the
  5. // reference of the head of the sorted list (which will either be
  6. // the head of a or the head of b)
  7. static Node<T> Merge(Node<T> a, Node<T> b);

So implement those functions and combine them into the traditional merge sort algorithm.

Any questions?

How much experience do you have with linked lists? Can you do something simple, like reversing a linked list in place?
That's an excellent explanation. Thank you very much. I have as much experience with linked lists and one would expect a month into Data Structures I. However, I can reverse linked lists, iterate through them, I understand the concept of pointers, nodes, etc. Certainly enough to understand your code snippet.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC