The Naked Scientists
  • Login
  • Register
  • Podcasts
      • The Naked Scientists
      • eLife
      • Naked Genetics
      • Naked Astronomy
      • In short
      • Naked Neuroscience
      • Ask! The Naked Scientists
      • Question of the Week
      • Archive
      • Video
      • SUBSCRIBE to our Podcasts
  • Articles
      • Science News
      • Features
      • Interviews
      • Answers to Science Questions
  • Get Naked
      • Donate
      • Do an Experiment
      • Science Forum
      • Ask a Question
  • About
      • Meet the team
      • Our Sponsors
      • Site Map
      • Contact us

User menu

  • Login
  • Register
  • Home
  • Help
  • Search
  • Tags
  • Recent Topics
  • Login
  • Register
  1. Naked Science Forum
  2. Non Life Sciences
  3. Technology
  4. What's the most efficient way to sort a pile by size and colour?
« previous next »
  • Print
Pages: [1]   Go Down

What's the most efficient way to sort a pile by size and colour?

  • 6 Replies
  • 8647 Views
  • 0 Tags

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Paul Anderson

  • Guest
What's the most efficient way to sort a pile by size and colour?
« on: 08/10/2008 10:56:25 »
Paul Anderson  asked the Naked Scientists:

Hi Chris and team,
 
If you have a mixed pile of big and small, white and black buttons, and you are writing a computer program to sort them, I am of the opinion that you either sort by colour first and then sort by size, or vice versa? Is there a faster way which involves less computation?
 
Is it large?
If Y put in bag L otherwise put in bag S
 
And then sorting bag L
Is it white?
If Y put in bag LW otherwise put in bag LB
 
Then sorting bag S
Is it white?
If Y put in bag SW otherwise put in bag SB
 
If you were to say:
Is it black and large?
If Y put in bag LB

This now leaves you with B&S, W&S, W&L, so you still have to sort for 3 other possibilities, so you haven't achieved any economy by tackling it that way.
 
Maybe this doesn't really fit in with the Naked Scientist programme, but it's an idle thing I have considered in the past.
 
Regards
 
Paul
NZ

What do you think?
Logged
 



Offline LeeE

  • Naked Science Forum King!
  • ******
  • 3382
  • Activity:
    0%
  • Thanked: 3 times
    • Spatial
What's the most efficient way to sort a pile by size and colour?
« Reply #1 on: 08/10/2008 18:36:14 »
Where you know that you will only ever get large or small, black or white you could use:

IF large then {
  IF black then {
    put in large black bag;
  } ELSE {
    put in large white bag;
  }
} ELSE {
  IF black {
    put in small black bag;
  } ELSE {
    put in small white bag;
  }
}

If there's a chance of other colours or sizes of buttons you'd probably be better off doing something like:

CASE (large AND black) { put in large black bag }
CASE (large AND white) { put in large white bag }
CASE (small AND black) { put in small black bag }
CASE (small AND white) { put in small white bag }
CASE () {put in others bag}
Logged
...And its claws are as big as cups, and for some reason it's got a tremendous fear of stamps! And Mrs Doyle was telling me it's got magnets on its tail, so if you're made out of metal it can attach itself to you! And instead of a mouth it's got four arses!
 

lyner

  • Guest
What's the most efficient way to sort a pile by size and colour?
« Reply #2 on: 09/10/2008 17:53:51 »
The actual computation time will depend on how smart the compiler is; it may actually produce rather similar code for both the two sources.
Logged
 

Offline wolfekeeper

  • Naked Science Forum King!
  • ******
  • 1678
  • Activity:
    0%
  • Thanked: 79 times
What's the most efficient way to sort a pile by size and colour?
« Reply #3 on: 15/10/2008 05:57:52 »
The quickest way to do this kind of thing in general that I'm aware of is to use an index sort.

In other words, you convert the size into 0 or 1 (where 0 is small, 1 is big) and the colour into 0 or 1 as well.

you then just add the sock into an array something like:

sock[size][colour].add(sock)

As others have noted it may not be quicker, it will depend on the compiler and stuff. It probably would be quicker if there were more types of socks though than just 4.
Logged
 

Marked as best answer by on 10/09/2025 08:01:27

Offline LeeE

  • Naked Science Forum King!
  • ******
  • 3382
  • Activity:
    0%
  • Thanked: 3 times
    • Spatial
  • Undo Best Answer
  • What's the most efficient way to sort a pile by size and colour?
    « Reply #4 on: 16/10/2008 01:44:51 »
    Quote
    sock[size][colour].add(sock)

    Umm... where did the socks come from?  They should have been rejected by data validation, as we're dealing with buttons.

    But here you're just using a sock class that you assume has add, and presumably sort, methods without showing the corresponding algorithms for the methods.  This answer is like saying the best way to sort the buttons (or socks) is to use the sort method.
    Logged
    ...And its claws are as big as cups, and for some reason it's got a tremendous fear of stamps! And Mrs Doyle was telling me it's got magnets on its tail, so if you're made out of metal it can attach itself to you! And instead of a mouth it's got four arses!
     



    lyner

    • Guest
    What's the most efficient way to sort a pile by size and colour?
    « Reply #5 on: 16/10/2008 07:00:21 »
    those are essentially high level language instructions. Quick ways to tell the machine what you want done. I Assumed we were looking for a routine which was optimal for the particular operation. You'd need to specify it at a much lower level.
    Logged
     

    Offline erickejah

    • Sr. Member
    • ****
    • 346
    • Activity:
      0%
    • Parking? I make my own parking spot!!
    What's the most efficient way to sort a pile by size and colour?
    « Reply #6 on: 18/10/2008 17:59:15 »
    Quote from: sophiecentaur on 16/10/2008 07:00:21
    You'd need to specify it at a much lower level.

    I agree  [:)]
    Logged
     



    • Print
    Pages: [1]   Go Up
    « previous next »
    Tags:
     
    There was an error while thanking
    Thanking...
    • SMF 2.0.15 | SMF © 2017, Simple Machines
      Privacy Policy
      SMFAds for Free Forums
    • Naked Science Forum ©

    Page created in 0.439 seconds with 43 queries.

    • Podcasts
    • Articles
    • Get Naked
    • About
    • Contact us
    • Advertise
    • Privacy Policy
    • Subscribe to newsletter
    • We love feedback

    Follow us

    cambridge_logo_footer.png

    ©The Naked Scientists® 2000–2017 | The Naked Scientists® and Naked Science® are registered trademarks created by Dr Chris Smith. Information presented on this website is the opinion of the individual contributors and does not reflect the general views of the administrators, editors, moderators, sponsors, Cambridge University or the public at large.