• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

algorithm

 
Mark Moore
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Moore
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 684
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes
 
Fred Hamilton
Ranch Hand
Posts: 684
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 684
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Fred
 
Kyle Richter
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10971
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kyle,
Welcome to the Ranch.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic