• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

JAVA QUESTION

santana9

Banned
Hi, I am trying to calculate the amount of time it takes to complete an ascending or descending bubblesort. I have both a worker class and an application class as such:

Worker class:
public void abubbleSort()
{

int out, in;

for(out=nElems-1; out>1; out--) // outer loop (backward)
for(in=0; in<out; in++) // inner loop (forward)
if( a[in] > a[in+1] ) // out of order?
swap(in, in+1); // swap them

} // end bubbleSort()

Application Class:
Date start = new Date();
if (s.charAt(0) == 'A')
arr.abubbleSort();
else
arr.dbubbleSort();
Date stop = new Date ();
long elapsed = stop.getTime() - start.getTime();
arr.display(numberToPrint); // display items

Is there anyway I can streamline this?? All suggestions would be appreciated. Thanks.

 
What are trying to optimize? The bubbleSort or the timing of the sort?

The standard optimization to bubble sort is to count the number of swaps made in the inner loop. If no swaps were made, then use a break statement to break out of the outer loop. Of course, you should be using quick sort.
 
Back
Top