This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Help counting the number of comparisons in sorting a list. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help counting the number of comparisons in sorting a list." Watch "Help counting the number of comparisons in sorting a list." New topic
Author

Help counting the number of comparisons in sorting a list.

Edward Strife
Greenhorn

Joined: Mar 15, 2012
Posts: 14
Hello,

My program needs to generate a list of random integers, the size being entered by the user. Then it needs to sort the list using the Selection Sort algorithm, keeping track of the comparisons made.

I think I have done well so far but for some reason the number of comparisons required are not printing to the screen.

Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 563
    
    7

just where are you calculating the number of compares?
Edward Strife
Greenhorn

Joined: Mar 15, 2012
Posts: 14
process() keeps track of the number of comparisons (numComparisons) as it sorts the list. This value is returned and used in the statement values = process(size);
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

And perhaps more to the point: where is the code which you think should be displaying the number of comparisons on the screen?
Edward Strife
Greenhorn

Joined: Mar 15, 2012
Posts: 14
values is supposed to be output in the applet in bottomLabel JLabel
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Ah, yes. And are you setting the value of that JLabel before or after you count the comparisons?
Edward Strife
Greenhorn

Joined: Mar 15, 2012
Posts: 14
Is there a straightforward way for me to reset the text on that JLabel every time I hit the Enter button?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help counting the number of comparisons in sorting a list.