Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: bubble sort timer problem

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation 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

    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;
                            }
                    }
                }
            }
    }
    Last edited by JavaNoob82; March 12th, 2010 at 06:28 AM. Reason: code wrong


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: bubble sort timer problem

    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.

            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");

Similar Threads

  1. How to Use Timer in Java
    By neo_2010 in forum Java SE API Tutorials
    Replies: 2
    Last Post: August 6th, 2013, 09:49 AM
  2. Timer Class help
    By Deadbob in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 22nd, 2010, 11:18 PM
  3. need help with Timer and sound
    By amahara in forum AWT / Java Swing
    Replies: 4
    Last Post: February 18th, 2010, 11:22 AM
  4. Timer?
    By TimW in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 27th, 2009, 07:43 AM
  5. Help - Swing Timer, 2 KeyEvents
    By Gheta in forum AWT / Java Swing
    Replies: 2
    Last Post: July 29th, 2009, 02:46 PM