• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to create array using loop and sort it  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a program which writes down all possible equation y=a+b+c values from min to max (in reality this equation would more difficult, but here is just short example).
The problem is that my sorting code can't get access to full array in loop.
Is there any way to pass array to sorting code, or somehow change sorting code?

 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please don't keep editing your post; it causes confusion.
I shall sort out the long line in line 79, and you can see the correct way to do it.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am afraid I don't think that code can be rescued. I think you will have to start again.
Start with a class to encapsulate your calculations. Then another class with a proper main method, 1 statement long:-I see you have indented your code, but it isn't consistent; line 6 ought to be a little to the right of line 5, for example. Incorrect indentation can cause confusing errors. I have my own suggestions about how to do indenting, which I showed here.
Give your variables names which mean something. You will read this code in three weeks and not know what aS means. And why are you using double arithmetic when all those numbers are integers? Use int arithmetic for whole numbers.
Never use doubles in loop variables. Always use integers. You can get subtle tiny errors with double arithmetic which will cause your loop to behave incorrectly.
Never write == false or == true. That is poor style and error‑prone. What if you write = by mistake?
Never if (b == true) ...
Always if (b) ...
Never if (b == false) ...
Always if (!b) ...
That method (as you will have read from the link) is much too long. You need separate methods which sort arrays. In fact I think you need a separate class. I wrote about that last week, so look in this thread for how to do it. If you write a simple bubble sort method, it will be shorter than your lines 50-75. I haven't read that code in detail, but seeing things like length + 1 and length * 2 worry me; I think you will go beyond the bounds of an array and suffer an Exception.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I seem so negative, but your code needs so much changing to make it into good quality code. And good quality code is what we like to see here
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you have a utility class you can do things like:-
MyUtilities.swapTwoElementsInArray(myArray, 0, 987654321);
and
MyUtilities.sortArray(myArray);
 
Justin Kaf
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I am newbie in programming. Basically that I want to do is to create array using loop and than sort it using bubble sort. Similar like in this code, but instead of int [] numArray = {0, 2, 4, 7, 3, 19}; I want to create array using loop. Is it possible?
>
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that algorithm is incorrect because you are not going to the very end of the array.
Also create a swap method and a sort method, so you can reuse those methods. Otherwise you will be writing swapping and sorting code repeatedly and you should remember
DRY=Don't Repeat Yourself.
You can fill an array in a loop; of course you can
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Earlier, I wrote:I believe that algorithm is incorrect because you are not going to the very end of the array. . . .
I may be mistaken however. Anybody like to check that?
 
Sheriff
Posts: 4931
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Campbell Ritchie already suggested, it is worth to start over. Solving by one task at a time.

1. Create a method to create an array of size (supplied as an argument) and automatically populate it with "pseudo random" numbers.
2. Create a method to sort array by supplying array as an argument.
3. Create a "run" method where you could call these created methods.
4. Call "run" method to start program.

Structure as an example could be:
Once you have your program structure, will be the way easier first to yourself also to your readers to understand in which part you're struggling in.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote: . . . create an array . . . and automatically populate it with "pseudo random" numbers. . . .
I have written about how to create such an array from a Stream at least twice in the last two weeks. If you find those posts, it will give you a hint how to create such an array elegantly. But Streams only work in Java8.
 
Liutauras Vilda
Sheriff
Posts: 4931
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I'm not allowed (maybe you're lucky that i'm not allowed, because it can be wrong) to provide you a Bubble sort algorithm in the way you want, because you have to find the solution yourself, I just wrote it in recursive way.
If I made mistake - guys hopefully will correct me instantly, so you could compare with yours and see if it does the same job.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would not give that sort method a return type like that. Unless you are cloning the array passed as a parameter, you are altering the state of the parameter, so you are using it as an output parameter. So you can give it void return.
In the swap method there is no need to initialise temp to 0. You can simply write int temp = arr[i];
 
Liutauras Vilda
Sheriff
Posts: 4931
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very good points Campbell Ritchie - thank you.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!