Results 1 to 2 of 2
  1. #1
    trill is offline Member
    Join Date
    Jul 2007
    Rep Power

    Default Help with code "perfect shuffle"

    Hello, I am trying to write java code for "perfect shuffle".
    Perfect shuffle is :given Ncards,you cut the cards at say ICards.
    Take the bottom card from the top portion first and then bottom card from bottom portion and so on.Need to calculate how many such shuffles are needed to restore it the original order.

    Can somebody give me an idea of what data structures to use.
    How to proceed?

  2. #2
    zoe is offline Member
    Join Date
    Jul 2007
    Rep Power


    The best class to use to model a pack of cards in this way is the Stack class. It's a bit like a Vector or ArrayList in that you can bung any old object into a Stack, but you're only allowed to add or remove objects from the start of the list with the push() and pop() methods.

    The way I'd tackle your problem is to split your pack of cards into two Stacks and then run through a loop to add cards to a third Stack which will be your recombined deck. If the loop counter is even then add from the first stack else add from the second until both piles are empty (or 52 insertions, they should be the same). At the end of this loop you can compare the combined Stack with your original deck to see if they match - if not then repeat the shuffle.

    If you're splitting the deck exactly in half each time, then I'm told it should be back in it's original state after eight shuffles although that seems a bit low to me.

Similar Threads

  1. Hwlp with "Open", "Save", "Save as..."
    By trill in forum New To Java
    Replies: 3
    Last Post: 11-02-2010, 09:26 AM
  2. Replies: 1
    Last Post: 03-14-2008, 02:51 PM
  3. Replies: 1
    Last Post: 07-27-2007, 10:59 PM
  4. Replies: 1
    Last Post: 07-25-2007, 10:55 PM
  5. Replies: 1
    Last Post: 07-16-2007, 06:32 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