Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-15-2008, 09:41 PM
Moderator
 
Join Date: Nov 2007
Posts: 1,659
Java Tip will become famous soon enoughJava Tip will become famous soon enough
Insertion Sort in Java
Code:
public class ObjectInsertSort { private Person[] a; private int nElems; public ObjectInsertSort(int max) { a = new Person[max]; nElems = 0; } // put person into array public void insert(String last, String first, int age) { a[nElems] = new Person(last, first, age); nElems++; } public void display() { for (int j = 0; j < nElems; j++) a[j].displayPerson(); } public void insertionSort() { int in, out; for (out = 1; out < nElems; out++) { Person temp = a[out]; // out is dividing line in = out; // start shifting at out while (in > 0 && // until smaller one found, a[in - 1].getLast().compareTo(temp.getLast()) > 0) { a[in] = a[in - 1]; // shift item to the right --in; // go left one position } a[in] = temp; // insert marked item } } public static void main(String[] args) { int maxSize = 100; // array size ObjectInsertSort arr; arr = new ObjectInsertSort(maxSize); // create the array arr.insert("Jo", "Yin", 24); arr.insert("Pengzhou", "Yin", 59); arr.insert("James", "Chen", 37); arr.insert("Chirs", "Paul", 37); arr.insert("Rob", "Tom", 43); arr.insert("Carlo", "Sato", 21); arr.insert("Al", "Henry", 29); arr.insert("Nancy", "Jose", 72); arr.insert("Vang", "Minh", 22); System.out.println("Before sorting:"); arr.display(); // display items arr.insertionSort(); // insertion-sort them System.out.println("After sorting:"); arr.display(); // display them again } } class Person { private String lastName; private String firstName; private int age; public Person(String last, String first, int a) { lastName = last; firstName = first; age = a; } public void displayPerson() { System.out.print(" Last name: " + lastName); System.out.print(", First name: " + firstName); System.out.println(", Age: " + age); } public String getLast() { return lastName; } }
__________________
Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums! (closes on July 27, 2008)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to sort a list using Bubble sort algorithm Java Tip Algorithms 3 04-29-2008 10:04 PM
Shell Sort in Java Java Tip Algorithms 0 04-15-2008 09:44 PM
Bubble Sort in Java Java Tip Algorithms 0 04-15-2008 09:42 PM
Selection sort in Java Java Tip Algorithms 0 04-15-2008 09:41 PM
Insertion sort algorithm Albert Advanced Java 2 06-28-2007 10:26 PM


All times are GMT +3. The time now is 12:24 PM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org