Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi,

All right, so first of all, this is not my homework, not a homework at all, I don't want any of you to solve it for me. I am just preparing for a programming competition, and there is this one I can't solve, and I would like some hint.
So the problem is that there are books in a library and I have to put them in order. But I have to find the the best way (definetely not the fastest!) to sort it.

So I have these numbers:
7, 10, 1, 3, 2, 8, 4, 9, 6, 5


I have to put them in order. I tried quicksort, but of course thats the fastest but not the best. Bubble sort is out as well, it has to do many steps.

So I have an answer, which is 7 (the count of the steps I have to do to sort it), but I can't find a way to figure out how to do it...

(I didn't know what tags to write, so since I try to write it in C# I just put that one there)

Thank you
Posted
Updated 7-Jan-13 8:50am
v3
Comments
PIEBALDconsult 7-Jan-13 15:36pm    
More specifics are required.

That reminds me of a job I applied for back in 2002. Among other things they asked me to submit an example of "a function to sort a list of numbers" in the language of my choice -- no other requirements. What sort of list? An array? On the command line? What?
At the time I was just learning C# and considered just using List.Sort but decided against that as it wasn't a C# job. After giving it some thought I decided to read the list from the command line and use a function I had written in C a few years earlier that added integers to an array in a sorted fashion. (I didn't get the job.)

You could also consider using a binary tree. :shrug:
velvet7 7-Jan-13 15:44pm    
Uhm, yeah so I have to give the minimum number of steps I have to do to sort it.
Array.sort doesn't return that to me. (and I think it does it in the fastest way, not the most efficient, im not sure though)
Philip Stuyck 7-Jan-13 15:47pm    
See the trick with bublesort and quicksort is, ... you keep using the same array.
Which is the whole point. Binary search is good enough if the numbers are being input one at a time, and the list is kept sorted in the process of inputting additional numbers and inserting them in the array or whatever containment you use.
velvet7 7-Jan-13 15:50pm    
Right. So the point of my application is not to be better.
Look at it this way. You have a friend, a librarian. He asked you to find a way how he could sort his books numbered from 1 to 10. (above) You have to tell him how many steps he has to do to sort them. :)
PIEBALDconsult 7-Jan-13 16:19pm    
Sure, but I don't think he would actually do swaps the way we do; a bookshelf doesn't work like an array. For instance in the first step he wouldn't put book 7 in the third spot, he'd put it in the seventh.

Here's another way -- put the 7 at the end, then put the 10 at the end, etc. -- I count five steps this way (for this set of data).

I suggest to take a look at Visualization and comparison of sorting algorithms in C# and experiment with the source code to compare different algorithms.

I took some minutes to test your numbers and I'm confindent that the constraint of 7 could be honored. ;)

Regards,
Daniele.
 
Share this answer
 
Comments
PIEBALDconsult 8-Jan-13 11:47am    
But this isn't about telling a computer how to sort them; it's about telling an actual person how to sort them -- there's a big difference; humans aren't as limited as computers.
velvet7 8-Jan-13 12:10pm    
I said that as an example to understand the concept better. The competition I am preparing for wrote the eaxct same thing. A librarian wants to know what are the minimum steps (swaps) to put them in order. That is the ONLY and ONE thing he cares about, nothing else. So I think this may help me, I'll check it out right away, thanks, Daniele.
PIEBALDconsult 8-Jan-13 17:14pm    
Then the librarian is an idiot -- he shouldn't use swaps at all.
He can do it in five operations, or even three if he's smart.
Daniele Rota Nodari 8-Jan-13 12:17pm    
Yes, You are right, but velvet7 was not asking for the whole solution: he was asking for a way to find the solution by himself.. so I think he has exactly to find how to sort them... in order to complete the process within 7 steps.
I think that article can help him walk this way.

And about limits of humans and computer: this is a topic we can discuss really long. ;)
If this is for programming competition, this is no less of cheating than if it is a homework. The competition should evaluate your real work and skills. Why should we help you more than your competitors? This is no good.

And, after all, you and your competitors can learn existing techniques on the Web, so what's the problem?

If I was the author of the problems for the competition, I would never pose a problem like sorting. This way, some knowledge of existing technique is evaluated, not in-depth knowledge and the ability to think. It is always possible to put forward an original problem, even though they are quite difficult to invent. Instead of offering some standard problems for a competition, it's much better not conduct any competitions at all.

So, if you are going for a real programming competition, knowledge of known algorithms won't really help. But if you find even some known algorithm by yourself, fully independently, it can greatly improve your skills and chances to win. But in this case, if we answer your question, we will fully take away that chance from you. As I don't want to take out this chance and hence hurt you, I'm not answering, sorry. Hope it can help you. :-)

—SA
 
Share this answer
 
Comments
velvet7 7-Jan-13 15:02pm    
I am sorry, I think you misunderstood me... I am practicing for this upcoming competition. The year before last this was the problem, so I was thinking of trying to solve it, just to be prepared. I would never ever cheat... I solved all of them, except this one. This one is giving me a headache.
velvet7 7-Jan-13 15:03pm    
Also, I was trying to get a HINT, not the answer. I stated that clearly in my quesiton. I know I have to find out the answer myself to get somewhere.
Sergey Alexandrovich Kryukov 7-Jan-13 15:09pm    
Good. :-)
—SA
velvet7 7-Jan-13 15:14pm    
Thanks, anyways :-)
Sergey Alexandrovich Kryukov 7-Jan-13 15:10pm    
If you get help, it will only reduce your chances to win, can you finally get it. It's unlikely that you get exactly this problem. You need to acquire skills, not the solution...
Instead trying to get the point, for your own sake, you are down-voting the post. Guess who is going to be a looser? A hint: not me. :-)
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900