File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Game Development and the fly likes algorithm Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "algorithm" Watch "algorithm" New topic
Author

algorithm

Mark Moore
Greenhorn

Joined: Feb 21, 2006
Posts: 15
I am looking for help on algorithm:
for example, if there are 8 unique ordered elements: 1, 2, 3, 4, 5, 6, 7, 8
if someone re-orders them
1,5,8,2,4,3,7,6 (then I will reorder the divs in that order)
but if user enters values like:
1, 2,3,4,5,6,7,1,2 - it means they want to move last two elements to the front, so the order in the end is really:
3,4,5,6,7,8,1,2
or if they enter 1,2,3,4,5,1,1,2 => 4,5,6,7,8,1,2,3
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11229
    
  16

this is very confusing.

Your example:

1,2,3,4,5,1,1,2 => 4,5,6,7,8,1,2,3


What would this do?
1,2,3,4,5,1,1,1 => ?

The trick to coding an algorithm is to define it in words first. You need to be able to explain, in words, what should be done. just giving an example (or two, or eight, or even 90) doesn't do it.

If you can't put it into words, you can't code it.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Mark Moore
Greenhorn

Joined: Feb 21, 2006
Posts: 15
1,2,3,4,5,1,1,1 => ? means last 3 elements need to move up so order is 6,7,8,1,2,3,4,5
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Mark Moore wrote:1,2,3,4,5,1,1,1 => ? means last 3 elements need to move up so order is 6,7,8,1,2,3,4,5


Is it your intention that there be different ways for the user to achieve the same ordering? For example the bolded ordering might be achieved by the user entering 1,2,3,4,5,1,1,1 as you say, or by 1,2,3,4,5,1,2,3, or maybe just by entering 6,7,8,1,2,3,4,5 ?
Mark Moore
Greenhorn

Joined: Feb 21, 2006
Posts: 15
yes
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Well, I don't have a solution off the top of my head, but if it was me, I'd give some thought as to how many different ways an ordering can be requested, then tackle these ways one at a time. In your project is there such a thing as an invalid user entry, where the user is told "Sorry, no can do?" It seems a big part of your challenge lies in error checking the user's entry to determine if it is valid.
Mark Moore
Greenhorn

Joined: Feb 21, 2006
Posts: 15
there is no wrong entry for a number
IE:
You have n number of ordered elements:

1 -blue
2 -red
3 -green

if you want green to be on top you change the green number around:
1
2
1
then order becomes
3 - green
2 - red
1 -blue

if you changed last two:
1
1
1

order would be:
2 -red
3 -green
1 -blue


if you changed to:
1
2
1222

then it stays the same
1 -blue
2 -red
3 -green


Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
OK, so the user enters 8 numbers, or 3, or whatever, and it kind of looks like the program will process these numbers sequentially. For example if the user enters 1,1,1 then the program looks at the first 1 and says this element stays here for now, then it looks at the 2nd 1 and sats ok this element goes up to 1, and the element that is 1 gets bumped down to 2nd, then it looks at the third 1 and says this goes to 1, but first I need to bump 2 to 3 and 1 to 2,

So you are moving elements around in some kind of an array or ArrayList. There are some things that this algorithm has in common with bubble sorts, so read up up on the mechanics of bubble sort to get some ideas.

hope this helps

http://en.wikipedia.org/wiki/Bubble_sort
Mark Moore
Greenhorn

Joined: Feb 21, 2006
Posts: 15
thanks Fred
Kyle Richter
Greenhorn

Joined: Dec 10, 2009
Posts: 2
ArrayList. Manipulate the List based on what the user wanted to do with a quicksort or other sorting method. Or even do a search for the array index that was changed, then comparatively review the entered segment to the current configuration to determine the correct configuration.

I'm pretty sure you could even use a queue, and just add functionality for searches to it, couldn't you?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10246
    
    8

Kyle,
Welcome to the Ranch.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: algorithm