Results 1 to 9 of 9

Thread: C# Sorting

  1. #1
    larry_d1990 is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default C# Sorting

    I know this really should only be about Java but I have a small issue involving the sorting of linked lists. C# is quite similar to Java(as i understand) and I was wondering how one could go about sorting a linked list. I've created a blank list(to copy to), and started an algorithm, how can <T> list be sorted??

    private LinkGen<T> list;

    public void Sort()
    {
    LinkListGen<T> newList = new LinkListGen<T>();//new blank list
    while (list != null)
    {
    if (list == null)
    {
    newList.AppendItem(list.HeadList);
    }
    else
    {

    }
    list = newList.list;
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,882
    Rep Power
    8

    Default

    By using a static method in the Collections class. How that translates to C# is probably your homework assignment.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by larry_d1990 View Post
    I've created a blank list(to copy to),
    Why?

    The whole point of sorting is to reorder items in the same list.

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Well, arrays have indexes, so you just swap the elements by index when sorting. How does a linked list work? Each element is represented by its value and a pointer to the next element (if it's a singly linked list). So how would you swap the two elements?
    Ever seen a dog chase its tail? Now that's an infinite loop.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by m00nchile View Post
    Well, arrays have indexes, so you just swap the elements by index when sorting. How does a linked list work? Each element is represented by its value and a pointer to the next element (if it's a singly linked list). So how would you swap the two elements?
    If you have three pointers/references P, A and B (in this order) and you want to swap elements A and B just a bit of pointer fiddling will do.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Hey Jos, I was asking the OP how he would tackle the problem instead of just posting code. Just clarifying :)
    Ever seen a dog chase its tail? Now that's an infinite loop.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by m00nchile View Post
    Hey Jos, I was asking the OP how he would tackle the problem instead of just posting code. Just clarifying :)
    Oops, sorry about the intrusion; I'll keep my mouth shut ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Nah, I should probably have quoted the OP to clarify I was talking to him. Please don't keep your mouth shut as want more stuff like the scizophrenia recursion examples from you :D
    Ever seen a dog chase its tail? Now that's an infinite loop.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by m00nchile View Post
    Nah, I should probably have quoted the OP to clarify I was talking to him. Please don't keep your mouth shut as want more stuff like the scizophrenia recursion examples from you :D
    Scizophrenia recursion examples? From me? Noooo, not from me, my bad alter ego maybe, but not me ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Sorting
    By Freakzoyd in forum New To Java
    Replies: 3
    Last Post: 01-21-2011, 02:02 AM
  2. little help with sorting
    By drgnfire25 in forum New To Java
    Replies: 3
    Last Post: 01-15-2011, 09:23 PM
  3. Help in sorting
    By nn12 in forum New To Java
    Replies: 3
    Last Post: 01-08-2011, 06:15 PM
  4. sorting
    By jot321 in forum New To Java
    Replies: 18
    Last Post: 10-02-2008, 10:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •