I was trying to work on the logic before I made it look pretty) Is my logic still valid
Liutauras Vilda wrote:I'm looking to the code, well, not looking for particular mistake now, but seeing lots of same methods. Why have you created each method multiple times? Probably you know that purpose of methods ar partially that, not to repeat yourself, but rather re-use methods for the same kind of task.
Feels like you can throw away about 1/3 of the code and have same functionality.
Now there are other insteresting things. I'm not trying to look for potential mistakes, just looking for weird things now. countM() method, why you have loop starting like that? "for (int j = 999990..." Can't wrap my head.
Let's do that way - try to put some print statements on quite a few places by writing i.e.: "i'm in a for loop, my i is: + i"... values after swap are ... and you'll likely see unexpected things.
Jacob Sousie wrote:So, what I was supposed to do was create 3 randomly generated arrays. one with 10 values, one with 1000, and one with 1,000,000. and then list ONLY the first ten elements of each, and then sort ONLY the first 10 digits of the whole array.
Jacob Sousie wrote:Why I used 999,990 is because I only need the first 10 out of the 1,000,000(that's what my teacher told me to do) so I start the value at 999,990 because 1,000,000 - 999,990 = 10
Carey Brown wrote:
Jacob Sousie wrote:So, what I was supposed to do was create 3 randomly generated arrays. one with 10 values, one with 1000, and one with 1,000,000. and then list ONLY the first ten elements of each, and then sort ONLY the first 10 digits of the whole array.
I see attempts to sort and print the entire array, your instructions say to do this for ONLY the first 10 [numbers] of the whole array.
Liutauras Vilda wrote:Can you show us those instructions?
Carey Brown wrote:
Carey Brown wrote:
Jacob Sousie wrote:So, what I was supposed to do was create 3 randomly generated arrays. one with 10 values, one with 1000, and one with 1,000,000. and then list ONLY the first ten elements of each, and then sort ONLY the first 10 digits of the whole array.
I see attempts to sort and print the entire array, your instructions say to do this for ONLY the first 10 [numbers] of the whole array.
Doesn't make sense to fill an array with a million numbers and then only deal with the first 10. Are you sure about those instructions? Are you paraphrasing?
Jacob Sousie wrote:Create an array of ten random integers, and array of 1,000 random integers, and an array of a million (1,000,000) random integers.
For each array:
Display the first ten elements of the array.
Call selection sort for the array.
Display the first ten elements of the sorted array.
Carey Brown wrote:
Jacob Sousie wrote:Create an array of ten random integers, and array of 1,000 random integers, and an array of a million (1,000,000) random integers.
For each array:
Display the first ten elements of the array.
Call selection sort for the array.
Display the first ten elements of the sorted array.
You don't need different sort methods for each array length, one will do the job.
You need a method: displayFirstTen( int[] array )
You can reuse that display method.
Jacob Sousie wrote:I just cut them out
now I just have one selectionSort(int[] array) (required to use that method name)
why does it work with 10 and 1000, but not 1,000,000? I tried debugging but it just skips over the code.
Norm Radder wrote:
I was trying to work on the logic before I made it look pretty) Is my logic still valid
I don't see any of the logic expressed in comments in the code so its hard to comment on the logic without seeing what it is.
Carey Brown wrote:
You don't want to be printing the whole array here. Remove lines 34-37. Every method should only do one thing.
You don't want to repeat your code. Move your functionality into reusable methods. For instance, your main() might look something like this:
Jacob Sousie wrote:When I get rid of the other methods, it prints 1000 numbers. that's why I made different methods for each case. Because when I use the one that was meant for the number 10 it doesn't count them right.
And when I try using the run(10) methods I get an error.
Carey Brown wrote:
You don't want to be printing the whole array here. Remove lines 34-37. Every method should only do one thing.
You don't want to repeat your code. Move your functionality into reusable methods. For instance, your main() might look something like this:
Jacob Sousie wrote:Which looks right so far. But I don't know why 1,000,000 wont print it's 10 sorted values!
I don't know how to use that run method you showed me. :/ I don't know what would go inside of it for the random numbers. Maybe if I use that it will help xD
Carey Brown wrote:
Jacob Sousie wrote:Which looks right so far. But I don't know why 1,000,000 wont print it's 10 sorted values!
You may not be waiting long enough for the sort to finish. Depending on the speed of your machine it may take anywhere from 5 to 15 minutes.
I don't know how to use that run method you showed me. :/ I don't know what would go inside of it for the random numbers. Maybe if I use that it will help xD
So far so good. You've eliminated a bunch of redundancy, these 4 lines are the only redundant ones left. Put them inside run().
Carey Brown wrote:
On line 16 you are generating random numbers from 0 through 9 inclusive. This was not part of your requirements. Additionally if you have a million of these and sort them your sorted list will probably come out as: 0 0 0 0 0 0 0 0 0 0.
I think you want random integers. Using Math.random() is now considered obsolete and has been replaced with the Random class. Then you can use its nextInt() to get a random integer.
Have you compared that with Arrays#sort? Selection sort runs in On² complexity and, the last time I timed it, I found it only slightly faster than bubble sort. But it was still much faster than bogosortCarey Brown wrote:On my (fast) machine, sorting a million took 3.5 minutes.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |