Results 1 to 5 of 5

Thread: Insertion sort

  1. #1
    Johnspsu is offline Member
    Join Date
    Jan 2014
    Posts
    2
    Rep Power
    0

    Default Insertion sort

    i have to write this program

    1) A “MyNum” class that will generate a random integer number from 1 to 200.
    2) A “Time” class that will return the system time with sufficient resolution to show the time between insertions. I suggest System.nanoTime().
    3) A “Node” class that will encapsulate the integer number and the Time it was generated.
    4) And a “Storage class” class that will contain the all the SORTED Nodes using an insertion sort.
    a. This class should have a “add” method to add in each node.
    b. And method(s) so the driver class may display the sorted list of nodes
    5) The “main” or driver class.

    So far i have written MyNum class and Time i am stuck on the Node class. When i run it, it only gives me the random number it does not give me the time it took for it to generate.

    XML Code:
    class Node {
    
    	private long time; 
    	private int num;
    		
    	     
    	public Node (){
    		num=MyNum.genRandom();
    		time = Time.time();
    		
    	}
    		
     
    	public String toString (){
    		
    		
    	return "Number" + num + "Time" + time;
     }
    	
    
    }
    Time CLASS
    XML Code:
    public class Time {
    
    	public static long time(){
    		return System.nanoTime();
    	}
    }


    Random Number
    XML Code:
    import java.util.Random;
    
    
    public class MyNum {
    	
    
    		static Random number = new Random();
    
    
    		public static void main(String[] args) {
    
    		    for (int i =0; i < 1 ; i++)
    		    {
    		        System.out.println( genRandom());
    		    }
    
    		}
    		public static int genRandom(){
    		    return number.nextInt(200);
    		}
    
    		}

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,652
    Rep Power
    5

    Default Re: Insertion sort

    To time an event you need a snapshot of the time before the event. Then subtract that from a snapshot after the event has completed.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: Insertion sort

    Crossposted: Insertion sort
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Johnspsu is offline Member
    Join Date
    Jan 2014
    Posts
    2
    Rep Power
    0

    Default Re: Insertion sort

    Okay im still i little confused since i have never coded in Java so what i need to do in the time class is subtract the starting time from the end time right ?

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,652
    Rep Power
    5

    Default Re: Insertion sort

    Correct!

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Insertion Sort
    By apiwowar in forum New To Java
    Replies: 2
    Last Post: 08-31-2011, 07:20 AM
  2. Help with insertion sort
    By daendoonge in forum Java Applets
    Replies: 0
    Last Post: 01-29-2011, 11:28 PM
  3. problem with insertion sort???
    By blueduiker in forum New To Java
    Replies: 2
    Last Post: 03-22-2010, 01:17 PM
  4. Insertion Sort in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 07:41 PM
  5. Insertion sort algorithm
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 06-28-2007, 08:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •