• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

algoritem writing problem

 
Yuri Paliy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to Java for 4 months, unfortunately we had a 2 month strike at school and now we need to catch up on "simple" things on our own. the coding seems to be right but I may be wrong but its the algorithm the one i find difficult for me. a few ground rules on how to write action (function) in an algorithm would be very helpful



any correction to the code would be welcomed too, thank you.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It worked nicely when I tried it, but I can't read Hebrew and you haven't told us what it ought to do, so I don't know whether it is working.

Your putToOrder method ought only to take one array at a time; you are doing the same thing twice. It would be better to have a method which does whatever it does (I do think I know what!) to one array, and call it twice.
 
Yuri Paliy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yes i have forgot to say that it does, so here it is:

the program creates two arrays of 20 and 15 and randomly selects numbers from 1 to 100 and puts them in the arrays.

1. arrange the arrays from smallest to biggest.
2. create an array of all the numbers within the two random arrays (no double numbers are allowed to appear) and print it.
3. crate an array of the common numbers between the groups and print it.
4. arrange the arrays from 2,3 (remove the zeros between the numbers).

thanks for the advice i understand how its better to send both arrays in separate.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I had guessed that the first method represents a bubble sort.

The fullBucket method counts whether a particular number appears in an array, and sets its value to 1 (it starts with 0 then ++). Then if the same number appears in the other array, it sets the number to 2. Then if it isn't 0 you print it. That sounds the same as a sort of set union, which appears correctly to implement what you want.

The emptyBucket method seems to print out those members in both arrays similarly, a sort of set intersection.

And I think the arrange method does actually get rid of the 0s. Only you never print it out.

I would have preferred to see the arrays set up as fields in the class and the methods not static. Better object-oriented style.
I would suggest you would get a better mark as a beginner by always putting {} round the bodies of your for-loops and if blocks. Apart from that it is good.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . but your bubble sort method will have to be static if you call it twice.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic