bubble sort timer problem
hi guys my aim is to generate a bubblesort, which is done no problem but one thing i cant figure out is generating how long it takes on each part of the array any input would be great!! thanks
Code :
import java.util.*;
public class bubbleSort
{
public static void main(String a[])
{
StopWatch s = new StopWatch();
int sort;
int array[] = {60000,20000,40000,10000,30000,50000};
System.out.print(" n\tTime(ms)\n");
System.out.println("-------|--------");
s.start();
bub_srt(array, array.length);
for(sort = 0; sort <array.length; sort++)
System.out.println(array[sort]);
System.out.println();
s.stop();
System.out.println(s.getElapsedTime());
}
public static void bub_srt(int a[],int n)
{
int i, j,t=0;
for(i = 0; i < n; i++)
{
for(j = 1; j < (n-i); j++)
{
if(a[j-1] > a[j])
{
t = a[j-1];
a[j-1]=a[j];
a[j]=t;
}
}
}
}
}
Re: bubble sort timer problem
Quote:
generating how long it takes on each part of the array
Not really sure what you mean by this.
Only surround the stuff you want to time with your timer start/stop. Also, I'm not familiar with the StopWatch class (it's not a standard Java API class). A simple way to time something is by using the System.currentTimeMillis() (not completely positive on that spelling), which returns the number of milliseconds that have gone by since ~1960 sometime.
Code :
long start = System.currentTimeMillis();
bub_srt(array, array.length);
long end = System.currentTimeMillis();
for(sort = 0; sort <array.length; sort++)
System.out.println(array[sort]);
System.out.println();
System.out.println("Elapsed time: " + (end-start) + " ms");