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 4 of 4

Thread: Insertion sort

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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.

    Node Class
    HTML 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
    HTML Code:
    public class Time {
     
        public static long time(){
            return System.nanoTime();
        }
    }
    Random Number
    HTML 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
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Insertion sort

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/84970-insertion-sort.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    johnspsu (January 23rd, 2014)

  4. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Insertion sort

    lol i dint notice ma bad , thanks i just need some to point me to the right direction to get this thing working.

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Insertion sort

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for newcomers, including "house rules" that you'd try to follow when visiting someplace new.

Similar Threads

  1. Problem with a binary insertion sort method
    By kert666 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 29th, 2013, 12:54 PM
  2. Insertion Sort - memory usage
    By lovon in forum Algorithms & Recursion
    Replies: 2
    Last Post: November 6th, 2011, 04:00 PM
  3. Insertion Sort on a Doubly Linked List
    By Kaisshau in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 7th, 2011, 11:18 PM
  4. Using a Generic Insertion Sort method
    By dubois.ford in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 6th, 2011, 06:08 PM
  5. Insertion Sort
    By Kimimaru in forum Algorithms & Recursion
    Replies: 2
    Last Post: December 6th, 2010, 06:26 AM