JAVA QUESTION

santana9

Banned
Jan 19, 2003
154
0
0
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.

 

ThreeEagles

Member
Dec 9, 1999
38
0
0
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.