C Board  

Go Back   C Board > Community Boards > General Discussions

Reply
 
LinkBack Thread Tools Display Modes
Old 05-12-2008, 10:18 PM   #1
Registered User
 
Join Date: Dec 2006
Location: Canada
Posts: 2,007
manually sorting cards

Not exactly programming but...

I have a stack of 100 cards numbered 1-100. Distribution is random. With a memory restraint of the size of a desk, what is the most efficient way to sort it manually?

I did a bucket sort (into piles of 10) and then insertion sorts on the individual piles. Is there a more efficient way?

Thanks =)
cyberfish is offline   Reply With Quote
Old 05-12-2008, 10:38 PM   #2
MENTAL DETECTOR
 
whiteflags's Avatar
 
Join Date: Apr 2006
Location: United States
Posts: 3,292
Try mergesort.
whiteflags is offline   Reply With Quote
Old 05-12-2008, 10:43 PM   #3
Registered User
 
Join Date: Dec 2006
Location: Canada
Posts: 2,007
Is mergesort more efficient? Since it's a comparison sort, it has a lower bound of O(nlogn) where bucket sort would do it in O(n) avg if I understand it correctly (according to wikipedia). And in this case, I do have the information required for bucket sort.
cyberfish is offline   Reply With Quote
Old 05-12-2008, 10:50 PM   #4
MENTAL DETECTOR
 
whiteflags's Avatar
 
Join Date: Apr 2006
Location: United States
Posts: 3,292
No, I wasn't being helpful. I just wanted to see if you could do it. But mergesort is quite intiutive to real life, and you can definitely make minimal use of your desk. The only thing you need is a sorted pile and an unsorted pile. The idea is to build the sorted pile by inserting cards yourself. I think you can do it pretty fast. Maybe I'm too quick to judge on the helpfulness thing.
whiteflags is offline   Reply With Quote
Old 05-12-2008, 11:01 PM   #5
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,338
Quote:
But mergesort is quite intiutive to real life, and you can definitely make minimal use of your desk. The only thing you need is a sorted pile and an unsorted pile. The idea is to build the sorted pile by inserting cards yourself.
I think you are thinking of insertion sort rather than merge sort.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 05-12-2008, 11:03 PM   #6
Registered User
 
Join Date: Dec 2006
Location: Canada
Posts: 2,007
Quote:
I think you are thinking of insertion sort rather than merge sort.
That is what I thought, too.
cyberfish is offline   Reply With Quote
Old 05-13-2008, 01:02 AM   #7
Deathray Engineer
 
MacGyver's Avatar
 
Join Date: Mar 2007
Posts: 3,211
Wow. We need lives.
__________________
MacGyver is offline   Reply With Quote
Old 05-13-2008, 05:05 AM   #8
the hat of redundancy hat
 
nvoigt's Avatar
 
Join Date: Aug 2001
Location: Hannover, Germany
Posts: 2,769
Quote:
Is there a more efficient way?
Hire 10 Indians or Ukrainians to do it for you ?
__________________
hth
-nv

She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

When in doubt, read the FAQ.
Then ask a smart question.
nvoigt is offline   Reply With Quote
Old 05-13-2008, 11:55 AM   #9
The superheterodyne.
 
twomers's Avatar
 
Join Date: Dec 2005
Location: Ireland
Posts: 2,215
Buy a new set.
__________________
I blag!
twomers is offline   Reply With Quote
Old 05-13-2008, 12:11 PM   #10
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,763
Direct placement sort. Draw 100 squares on the ground, labeled 1-100. For each card, place it on the matching square. Now pick them up in order. (This is a special case of rank sort)

Even more efficient, don't sort the cards at all, but merely imagine that they are sorted.

Last edited by brewbuck; 05-13-2008 at 12:13 PM.
brewbuck is offline   Reply With Quote
Old 05-13-2008, 12:31 PM   #11
Internet Superhero
 
Join Date: Sep 2006
Location: Denmark
Posts: 468
Quote:
Originally Posted by brewbuck View Post
Direct placement sort. Draw 100 squares on the ground, labeled 1-100. For each card, place it on the matching square. Now pick them up in order. (This is a special case of rank sort)

Even more efficient, don't sort the cards at all, but merely imagine that they are sorted.
Hmm

Code:
int* DPSort(int *Data)
{
	int Table[100];
	int SortedData[100];
	unsigned int i = 0;
	for(; i < 100; i++)
	{
		Table[i] = i+1;
	}
	
	for(i = 0; i < 100; i++)
	{
		for(unsigned int j = 0; j < 100; j++)
		{
			if(Data[i] == Table[j])
			{
				SortedData[j] = Data[i];
				break;
			}
		}
	}
	
	return SortedData;
}
Something tells me this will be very inefficient?

Edit: Ready to flame the first one to comment my poor code!
__________________
How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

Last edited by Neo1; 05-13-2008 at 12:33 PM.
Neo1 is offline   Reply With Quote
Old 05-13-2008, 12:52 PM   #12
Registered User
 
Join Date: Jan 2005
Posts: 7,252
That doesn't model brewbuck's idea. It relies on the fact that Table[j] always equals j+1. So remove your j loop and your if and simply set SortedData[Data[i]+1] = Data[i].

I would make either 10 or 20 piles and sort by the first digit in the number on the card first, then sort each individual pile as I picked them up.

The most efficient algorithm depends on how fast your mind can determine the proper location and deal the cards, not so much what the most efficient algorithm in code would be.
Daved is offline   Reply With Quote
Old 05-13-2008, 12:52 PM   #13
Registered User
 
PING's Avatar
 
Join Date: Nov 2004
Location: india
Posts: 493
Quote:
Hire 10 Indians or Ukrainians to do it for you ?
Or maybe 100 Germans
__________________
Go Petr !!
My Blog
PING is offline   Reply With Quote
Old 05-13-2008, 01:15 PM   #14
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,492
I would indeed go for bucket sort. Have ten stacks. Put 1-9 into the first, 10-19 into the second, and so on. (The first stack will have 9 cards, and the last 11. That's fine. It's worth the faster recognition.)

Then sort the individual stacks and put them together.
__________________
All the buzzt!
CornedBee

"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
CornedBee is offline   Reply With Quote
Old 05-13-2008, 01:22 PM   #15
& the hat of GPL slaying
 
Thantos's Avatar
 
Join Date: Sep 2001
Posts: 5,732
It kinda matters what type of cards these are also and how you want to sort them. For example if you were trying to sort two playing decks (104 cards) then I would just make a pile for each type of card (2s, 3s, Js, Qs, etc). If you wanted to do them by suit and rank then I'd probably just make 8 rows each with a suit and then put the card in the right spot (insertion sort) as I got to it.
Thantos is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with linked list sorting function Jaggid1x C Programming 6 06-02-2009 02:14 AM
Help it won't compile!!!!! esbo C Programming 58 01-04-2009 03:22 PM
Help!For poker game simulation tx1988 C++ Programming 24 05-25-2007 09:59 PM
Cribbage Game PJYelton Game Programming 14 04-07-2003 10:00 AM
playing ... cards?? GiraffeMan C Programming 16 02-06-2002 03:51 PM


All times are GMT -6. The time now is 03:39 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22