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 offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    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 - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

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

    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
    11

    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
    14,422
    Blog Entries
    7
    Rep Power
    28

    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
    Build a wall around Donald Trump; I'll pay for it.

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

    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
    14,422
    Blog Entries
    7
    Rep Power
    28

    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
    Build a wall around Donald Trump; I'll pay for it.

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

    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
    14,422
    Blog Entries
    7
    Rep Power
    28

    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
    Build a wall around Donald Trump; I'll pay for it.

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
  •