You want us to do your homework for you?
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
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
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
I'm half tempted to just provided a bubble sort and let him submit that to his teacher not knowing any better..
Phaelax
Practically a Posting Shark
858 posts since Mar 2004
Reputation Points: 92
Solved Threads: 51
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.
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
2006? This is really silly javaflasher.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
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?
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
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.
peter_budo
Code tags enforcer
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902