Ordered Generic List

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gallantalex
    New Member
    • May 2010
    • 2

    Ordered Generic List

    Hi I am creating a game for fun using c#. I have a function that determines the shortest distance between two locations on a grid. I am using a form of dijkstra's algorithm but it still seems to be a little too slow for me. When I ask for a location from one corner to another in a grid of 140 by 140. It takes about 4 seconds.

    The list of possible paths gets pretty large and searching for the next shortest path involves an iteration through all available paths. So I wanted to use an ordered list of paths to greatly reduce the number of iterations.

    So my question is if there is a generic collection that I could use that automatically inserts a Comparable object into a sorted list and will be able to search for an object using the binary search algorithm? Right now I am just using List<T>. I was unable to find anything via google and do not want to create my own insert/sort methods. Plus I will be able to use this in other areas of the game. Thanks in advance and I you need more information let me know.
  • ThatThatGuy
    Recognized Expert Contributor
    • Jul 2009
    • 453

    #2
    Originally posted by gallantalex
    Hi I am creating a game for fun using c#. I have a function that determines the shortest distance between two locations on a grid. I am using a form of dijkstra's algorithm but it still seems to be a little too slow for me. When I ask for a location from one corner to another in a grid of 140 by 140. It takes about 4 seconds.

    The list of possible paths gets pretty large and searching for the next shortest path involves an iteration through all available paths. So I wanted to use an ordered list of paths to greatly reduce the number of iterations.

    So my question is if there is a generic collection that I could use that automatically inserts a Comparable object into a sorted list and will be able to search for an object using the binary search algorithm? Right now I am just using List<T>. I was unable to find anything via google and do not want to create my own insert/sort methods. Plus I will be able to use this in other areas of the game. Thanks in advance and I you need more information let me know.
    You can use List<T>.Sort()

    Comment

    • Christian Binder
      Recognized Expert New Member
      • Jan 2008
      • 218

      #3
      Hi!

      You could give System.Collecti ons.Generic.Sor tedDictionary or SortedList a try.

      Comment

      • gallantalex
        New Member
        • May 2010
        • 2

        #4
        After looking more closely into the members of List<T> I realized it has the BinarySearch method already which I didnt see before. I didnt need to sort my list because I was just going to insert accordingly. Instead the BinarySearch returns a bitwise complement of the location where my new item should get inserted if it isnt already in the list which solves my BinaryInsert problem as well.

        Comment

        Working...