Calling methods in a main

Posted 19 April 2010 - 11:19 AM

I am trying to create a random list in the main, use the quickSort algorithm method on it, print how long it took, use the same random list generated, use the quickInsertionSort algorithm method on it, and print how long it took to do that one. I am a little confused on how to actually the random list with the method I wrote and then use the list with the two algorithm sorting methods I wrote (quicksort and quickInsertionSort). Help please! This is due fairly quickly, like little over two hours

Re: Calling methods in a main

Posted 21 April 2010 - 12:42 PM

Okay so I need to create an instance of randomList, quickSort, and quickInsertionSort. I can do that. But I don't quite know what I should pass into those as parameters. Yea the date has passed but I just want to know for me now.

Re: Calling methods in a main

Posted 21 April 2010 - 02:19 PM

Ok I got a few questions.

I added parameters to the spots needed but it said "Cannot make a static reference to a non-static method randomList() from the type Test". It said that for randomList(), quicksort(), and quickInsertionSort(). Should I just change the signatures of the three methods to static to fix this or should I solve this in a different way?

I added a "test line of code in the main, System.out.println(list), and it only printed the memory address and not the integers stored in it.

Seems like there was one more but I can't seem to remember it so I'll stay with this for now.

Error #1: The print line only prints out the memory location of list. How can I get it to print out the list?

Error #2: "No enclosing instance of type Test is accessible. Must qualify the allocation with an enclosing instance of type Test." What does this mean? I thought it was because you had put created the instance of Test after created the instance of ExecutionTimer but that wasn't it.

Re: Calling methods in a main

Posted 21 April 2010 - 03:44 PM

Thanks a lot Dogstopper. That was really strange with the creating the instance of the ExecutionTimer. Never seen nothing like that before. I got it compiling and printing stuff out but not quite performing the way it should.

Error #1: It prints out the list before the sort and afterwards, but the afterwards printout is the same as the first. So, in other words, it is not sorting the list. I can't seem to figure out why. I understand the algorithm pretty decently even though I did get the code for this particular quicksort from google.

Error #2: I put 100 million ints in the list and had it sort it (even though it didn't sort it) and it took well over a minute to finish whatever it was doing. The timer printout at the end always says 0.0 seconds

Re: Calling methods in a main

Posted 21 April 2010 - 04:17 PM

#1: Oops, I thought you WANTED it that way... Well, objects passed to methods are never changed as explained HERE. The workaround is to have each of those methods return the sorted array and receive it like so:

Re: Calling methods in a main

Posted 21 April 2010 - 04:36 PM

I tried that but it still isn't sorting the list because both printouts are the same. Maybe I will go search for another quicksort online and see if theirs work. But if anyone catches why it is not sorting/printing the right thing, feel free to fix my current code.

As for the double/long thing. That was just an overlook by me so I changed the durationInSeconds() method to a long as well. Timer still not working either.

Thanks for the help Dogstopper. You have gotten me far. Its compiling at least and is practically finished now. Just one little thing messed up someowhere, I can feel it!

Re: Calling methods in a main

Posted 21 April 2010 - 11:16 PM

I just worked on it about an hour with fresh eyes and fixed a couple things. I got the quickInsertionSort to work completely. It sorts any list thrown at it. The quicksort, for some reason, does not work. It shuffles a couple of the values around in the list to be sorted, but it doesn't finish it. For example, the list:

[20, 57, 43, 58, 10, 95, 99, 15, 16, 38]

gets "sorted" halfwayish to this:

[20, 43, 57, 58, 10, 95, 99, 15, 16, 38]

Not sure what is wrong with it.

And still, the timers are always saying 0 seconds. I put the list to 10 million ints and it takes almost a whole second to execute. I have it just printing the starting and ending times, and they are equal for some reason. For example, one test print this:

Start: 1271917526554
End: 1271917526554

EDIT:: The quickInsertionSort falls to pieces and does not work when the list is over 30. In the code I have it to go directly to the insertionSort if the list was under 30 and if not, to go to quicksort. This means that my insertionSort works perfectly and, still, the quicksort does not work.

Re: Calling methods in a main

Posted 22 April 2010 - 06:49 PM

Dogstopper is a mod on this forum, and is on all of the time. As he has been going through all of this post with you, which is quite long, might be better to ask for further help when he is online. Otherwise, some other member is going to have to go through the whole page reading whats been done already (Although, this is a nice community and someone might just do that!)