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: Insert sort algorithm

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

    Default Insert sort algorithm

    I'm trying to understand the algorithm for insert sort for efficiency.

    Why does 2 + 2 x 2 + .... 2 X(n-1) = n squared - n?

    what's the relationship between the two?

    thanks.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Insert sort algorithm

    Hello Alysosh,

    Hopefully this link will answer your question:

    Insertion sort - Wikipedia, the free encyclopedia

    I found this good example here for you to take a look at:

    class ArrayIns {
        private long[] a;
        private int nElems;
     
        public ArrayIns(int max)
        {
            a = new long[max];
            nElems = 0;
        }
     
        public void insert(long value)
        {
            a[nElems] = value;
            nElems++;
        }
     
        public void display()
        {
            for (int j = 0; j < nElems; j++)
                System.out.print(a[j] + " ");
            System.out.println("");
        }
     
        public void insertionSort() {
            int in, out;
     
            for (out = 1; out < nElems; out++)
            {
                long temp = a[out];
                in = out;
                while (in > 0 && a[in - 1] >= temp)
                {
                    a[in] = a[in - 1];
                    --in; 
                }
                a[in] = temp;
            }
        }
    }

    class insertSort
    {
    public static void main(String[] args)
     {
     int maxSize = 100;      
     ArrayIns arr;        
     arr = new ArrayIns(maxSize); 
     
     arr.insert(77);       
     arr.insert(99);
     arr.insert(44);
     arr.insert(55);
     arr.insert(22);
     arr.insert(88);
     arr.insert(11);
     arr.insert(00);
     arr.insert(66);
     arr.insert(33);
     
     arr.display();        
     
     arr.insertionSort();    
     
     arr.display();     
     } 
    }

    Example output:

    77 99 44 55 22 88 11 0 66 33
    0 11 22 33 44 55 66 77 88 99
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. How to Sort an Array using the java.util.Arrays class
    By JavaPF in forum Java SE API Tutorials
    Replies: 2
    Last Post: May 17th, 2014, 01:16 AM