954,551 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Rebuild array versus linked list traversal

I have been working on a number of card games. Important, of course, is shuffling the deck. I have come up with a few different ways to do this. Currently I do not shuffle, but to deal a card, I lay out the deck in order, selecting a random number between 1 and the number of cards remaining and selecting that card. I then remove that card from the deck so that another card can be picked.

There are two main ways to do this, and I am trying to figure out which one is optimal.

The first is to have an array of cards. When one card is dealt, a new array is formed with that card missing. I kind of like keeping the array size exactly the size of the deck, but perhaps this is not efficient. So, the first array has length n and the new array after the card is dealt has length n-1. VB.Net makes it simple to change array sizes around like this.

The second method is to use a linked list for the deck of cards. This makes removing a card fairly swift, aside from the list traversal to get to the place where the card is to be removed.

Is the time taken to traverse a linked list comparable to the time taken to recreate an array or does one greatly outweigh the other?

arkanoid

arkanoid
Newbie Poster
4 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

I have been working on a number of card games. Important, of course, is shuffling the deck. I have come up with a few different ways to do this. Currently I do not shuffle, but to deal a card, I lay out the deck in order, selecting a random number between 1 and the number of cards remaining and selecting that card. I then remove that card from the deck so that another card can be picked.

There are two main ways to do this, and I am trying to figure out which one is optimal.

The first is to have an array of cards. When one card is dealt, a new array is formed with that card missing. I kind of like keeping the array size exactly the size of the deck, but perhaps this is not efficient. So, the first array has length n and the new array after the card is dealt has length n-1. VB.Net makes it simple to change array sizes around like this.

The second method is to use a linked list for the deck of cards. This makes removing a card fairly swift, aside from the list traversal to get to the place where the card is to be removed.

Is the time taken to traverse a linked list comparable to the time taken to recreate an array or does one greatly outweigh the other?

arkanoid


It seems that Insertion and Removal are order O(1) with linked lists, so these are probably ideal. I just can't get over the idea that it has to travel down the list to get to the place to do the work.

The other method seems to be O(n^2/2)

arkanoid
Newbie Poster
4 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: