insertion sort java code

E

Eric Sosman

Tim923 said:
I already earned my BA degree. This is just for personal interest.

Something rings false about the second sentence. If
you have a personal interest in insertion sorts, why ask
someone else to provide the code? If insertion sorts are
so interesting to you, why ask someone else to have all
the fun?

Tell you what, though: I'll offer some code for an
Even Better sorting method, one that will outperform an
insertion sort on virtually every measure in almost every
circumstance. Here ya go:

java.util.Arrays.sort(array_of_Comparable);

Hope this helps.
 
C

Caine

In said:
I already earned my BA degree. This is just for personal interest.

Looks like it's time to enroll again and get that Master's degree, your
Bachelor's has obviously not taught you to think for yourself...
 
V

Virgil Green

Caine said:
Looks like it's time to enroll again and get that Master's degree,
your Bachelor's has obviously not taught you to think for yourself...

Which, frankly concerns me... that without knowing how to think for one's
self, one could be accepted into and presumably complete the Master's
program.
 
T

Tim923

Something rings false about the second sentence. If
you have a personal interest in insertion sorts, why ask
someone else to provide the code?

Busting my butt for years to get my 4 year math degree at Binghamton
University entitles me to ask such questions about courses that don't
count for degrees. It's a well known policy.

http://www.jptc.org/cont_2.html
 
E

Eric Sosman

Tim923 said:
Busting my butt for years to get my 4 year math degree at Binghamton
University entitles me to ask such questions about courses that don't
count for degrees. It's a well known policy.

It's possible that you're being atrociously misjudged,
but you must admit that the request looks peculiar in the
extreme. A few of the red flags:

- You specify an insertion sort, and are presumably
unwilling to use an implementation of any other
sorting algorithm. Thus your goal is not to sort
something (I offered code for that), but to do an
insertion sort. I can think of only one context
that calls for one not-very-good sorting algorithm,
and that's homework.

- Well, no: Other contexts exist. Insertion sort has
poor asymptotic behavior but small constant factors,
so it can outperform "superior" algorithms on very
short files. Practical sorting implementations often
switch to insertion sort (or something like it) once
they've "almost sorted" the large file. You may be
implementing such a thing -- but if you were capable
of doing so, you would also be capable of writing the
six or eight lines of an insertion sort without needing
to ask for help.

- You say you want an insertion sort "just for personal
interest" -- and that still rings false to me. People
who are interested in a topic generally want to do
their own investigation/participation/discovery, not
ask other people to have the fun as proxies. Nothing
you've written thus far explains (or even attempts to
explain) this attitudinal oddity.

Perhaps if you would explain how you intend to use the
code (and why you're not up to the simple task of writing
it yourself), nasty suspicious people like me might become
more sympathetic.
 
B

Betty

Virgil Green said:
Which, frankly concerns me... that without knowing how to think for one's
self, one could be accepted into and presumably complete the Master's
program.
I still say lazy. Why? Because google produces a zillion examples
with java code.
 
G

googmeister

Tell you what, though: I'll offer some code for an
Even Better sorting method, one that will outperform an
insertion sort on virtually every measure in almost every
circumstance. Here ya go:

java.util.Arrays.sort(array_of _Comparable);

OK, I'm up for the challenge. Let's measure performance in terms of
memory.
Insertion definitely beats the system sort (mergesort) here. Insertion
sort
is a great algorithm if the files are small, and this explains why the
system
sort cuts off to insertion sort on small files. Also insertion sort
runs in linear
time if the input is sorted or nearly sorted, and these inputs tend to
arise
in practice.
 
T

Tim923

Perhaps if you would explain how you intend to use the
code (and why you're not up to the simple task of writing
it yourself), nasty suspicious people like me might become
more sympathetic.

Let this be the last post of the thread, dear friends.

Basically, I had just wanted a quick end to a project that I had been
working on for hours. It was 75% done, but I just needed the one last
"insertion sort" method to use for my object array. At that point I
was fatigued and didn't want to think anymore. I had found the code
on a website, and had to alter it to match my objects, methods,
classes, etc. Cheating? I don't know. I still had to know what I
was doing to get it to work and compile in my project.

Oh, the Java course is for work skills and not a degree. Maybe
"personal interest" was a bad description.
 
B

bugbear

Tim923 said:
Let this be the last post of the thread, dear friends.

Basically, I had just wanted a quick end to a project that I had been
working on for hours.

In that case Eric recommendation of the
provided-by-the-libray sort seems ideal.

BugBear
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top