Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
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, 08:43 PM
Moderator
 
Join Date: Nov 2007
Posts: 1,415
Java Tip will become famous soon enoughJava Tip will become famous soon enough
Merge Sort in Java
Code:
public class MergeSortArray { private long[] theArray; private int nElems; public MergeSortArray(int max) { theArray = new long[max]; nElems = 0; } public void insert(long value) { theArray[nElems] = value; // insert it nElems++; // increment size } public void display() { for (int j = 0; j < nElems; j++) System.out.print(theArray[j] + " "); System.out.println(""); } public void mergeSort() { long[] workSpace = new long[nElems]; recMergeSort(workSpace, 0, nElems - 1); } private void recMergeSort(long[] workSpace, int lowerBound, int upperBound) { if (lowerBound == upperBound) // if range is 1, return; // no use sorting else { // find midpoint int mid = (lowerBound + upperBound) / 2; // sort low half recMergeSort(workSpace, lowerBound, mid); // sort high half recMergeSort(workSpace, mid + 1, upperBound); // merge them merge(workSpace, lowerBound, mid + 1, upperBound); } } private void merge(long[] workSpace, int lowPtr, int highPtr, int upperBound) { int j = 0; // workspace index int lowerBound = lowPtr; int mid = highPtr - 1; int n = upperBound - lowerBound + 1; // # of items while (lowPtr <= mid && highPtr <= upperBound) if (theArray[lowPtr] < theArray[highPtr]) workSpace[j++] = theArray[lowPtr++]; else workSpace[j++] = theArray[highPtr++]; while (lowPtr <= mid) workSpace[j++] = theArray[lowPtr++]; while (highPtr <= upperBound) workSpace[j++] = theArray[highPtr++]; for (j = 0; j < n; j++) theArray[lowerBound + j] = workSpace[j]; } public static void main(String[] args) { int maxSize = 100; // array size MergeSortArray arr = new MergeSortArray(maxSize); // create the array arr.insert(14); arr.insert(21); arr.insert(43); arr.insert(50); arr.insert(62); arr.insert(75); arr.insert(14); arr.insert(2); arr.insert(39); arr.insert(5); arr.insert(608); arr.insert(36); arr.display(); arr.mergeSort(); arr.display(); } }
__________________
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.
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 09:04 PM
Heap Sort in Java Java Tip Algorithms 0 04-16-2008 11:27 PM
Bubble Sort in Java Java Tip Algorithms 0 04-15-2008 08:42 PM
Insertion Sort in Java Java Tip Algorithms 0 04-15-2008 08:41 PM
Merge Sort Help Hollywood New To Java 5 01-30-2008 04:26 AM


All times are GMT +3. The time now is 03:27 AM.


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