Hi friends, I am a very new user of this forum and want a simplest program on insertion sort. Can anybody provide me this?

Recommended Answers

All 10 Replies

You want us to do your homework for you?

Member Avatar for iamthwee

Hi friends, I am a very new user of this forum and want a simplest program on insertion sort. Can anybody provide me this?

This was found using google in two minutes. So are you saying you can't even use google? :rolleyes:

// insertSort.java
// demonstrates insertion sort
// to run this program: C>java InsertSortApp
//--------------------------------------------------------------
class ArrayIns
  {
  private long[] a;         // ref to array a
  private int nElems;        // number of data items
//--------------------------------------------------------------
  public ArrayIns(int max)     // constructor
   {
   a = new long[max];         // create the array
   nElems = 0;            // no items yet
   }
//--------------------------------------------------------------
  public void insert(long value)  // put element into array
   {
   a[nElems] = value;       // insert it
   nElems++;           // increment size
   }
//--------------------------------------------------------------
  public void display()       // displays array contents
   {
   for(int j=0; j<nElems; j++)    // for each element,
     System.out.print(a[j] + " "); // display it
   System.out.println("");
   }
//--------------------------------------------------------------
  public void insertionSort()
   {
   int in, out;

   for(out=1; out<nElems; out++)   // out is dividing line
     {
     long temp = a[out];      // remove marked item
     in = out;           // start shifts at out
     while(in>0 && a[in-1] >= temp) // until one is smaller,
      {
      a[in] = a[in-1];      // shift item to right
      --in;            // go left one position
      }
     a[in] = temp;         // insert marked item
     } // end for
   } // end insertionSort()
//--------------------------------------------------------------
  } // end class ArrayIns
////////////////////////////////////////////////////////////////
class insertSort
  {
  public static void main(String[] args)
   {
   int maxSize = 100;      // array size
   ArrayIns arr;         // reference to array
   arr = new ArrayIns(maxSize); // create the array

   arr.insert(77);        // insert 10 items
   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();        // display items

   arr.insertionSort();     // insertion-sort them

   arr.display();        // display them again
   } // end main()
  } // end class InsertSortApp
////////////////////////////////////////////////////////////////Here's the output from the insertSort.java program; it's the same

I'm half tempted to just provided a bubble sort and let him submit that to his teacher not knowing any better..

I'm half tempted to just provided a bubble sort and let him submit that to his teacher not knowing any better..

Muuuwhahhahhahahah. That would have been hilarious.

Thanks, I tried it and working nice.

this is one is more simple and better!!

import java.io.*;
class insertion
{
public static void main() throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br= new BufferedReader(isr);
System.out.println("Please Enter the number of values");
int a=Integer.parseInt(br.readLine());
int age[]=new int[a];
int minelem=0, temp;
System.out.println("Enter the VALUES");
for(int x=0;x<a;x++)
{
age[x]=Integer.parseInt(br.readLine());
}
System.out.println("The number in ascending order are: ");
    int min;
   for(int x=0;x<a-1;x++)
   {
       min=age[x];
       for(int y=x+1;y<a;y++)
       {
           if(min>age[y])
           {
               min=age[y];
               minelem=y;
                            
            }
        }
        System.out.println("Minimum Value: " + min);
        temp=age[minelem];
        age[minelem] = age[x];
        age[x]=temp;
    }
    for(int w=0;w<a;w++)
    {
        System.out.println(age[w]);
    }
    }
}

2006? This is really silly javaflasher.

hey dude!!
i know no one cares about this thread
but people who search on google about insertion sorting
it might be helpful to them!!
its for them not for this thread!!
GOT IT

hey dude!!
i know no one cares about this thread
but people who search on google about insertion sorting
it might be helpful to them!!
its for them not for this thread!!
GOT IT

hey "dude",

JamesCherrill is right. If you have found thé way to do this task, no one's stopping you from creating your own thread, and put a title like:

THÉ ULTIMATE WAY TO ... insertion sorting, or whatever, and posting your code, this way, every one googeling the topic will find it too, and you don't have to make us read through all the dates, just to figure out which one might be a current question.
also, you're writing a lot of code, while even manually sorting the array would even take less code, not to mention the sort functions that are standard available in the java libraries. I have no doubt that you want to help other developers, but then why not by answering questions that are currently being asked?

hey dude!!
i know no one cares about this thread
but people who search on google about insertion sorting
it might be helpful to them!!
its for them not for this thread!!
GOT IT

My personal opinion for anyone possibly reading this: Above presented code may pass you for your assignment, but code quality and standards are very low so it is not best example to use.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.