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:40 PM
Moderator
 
Join Date: Nov 2007
Posts: 1,659
Java Tip will become famous soon enoughJava Tip will become famous soon enough
Simple version of quick sort
Code:
public class QuickSortSimpleVersion { private long[] data; private int len; public QuickSortSimpleVersion(int max) { data = new long[max]; len = 0; } public void insert(long value) { data[len] = value; len++; } public void display() { System.out.print("Data"); for (int j = 0; j < len; j++) System.out.print(data[j] + " "); System.out.println(""); } public void quickSort() { recQuickSort(0, len - 1); } public void recQuickSort(int left, int right) { if (right - left <= 0) // if size <= 1 already sorted return; else // size is 2 or larger { long pivot = data[right]; // rightmost item // partition range int partition = partitionData(left, right, pivot); recQuickSort(left, partition - 1); // sort left side recQuickSort(partition + 1, right); // sort right side } } public int partitionData(int left, int right, long pivot) { int leftPtr = left - 1; // left (after ++) int rightPtr = right; // right-1 (after --) while (true) { // find bigger item while (data[++leftPtr] < pivot) ; // find smaller item while (rightPtr > 0 && data[--rightPtr] > pivot) ; if (leftPtr >= rightPtr) // if pointers cross, partition done break; else swap(leftPtr, rightPtr); } swap(leftPtr, right); // restore pivot and return pivot location return leftPtr; } public void swap(int d1, int d2) { long temp = data[d1]; data[d1] = data[d2];data[d2] = temp; } public static void main(String[] args) { int maxSize = 16; // array size QuickSortSimpleVersion arr = new QuickSortSimpleVersion(maxSize); // create array for (int j = 0; j < maxSize; j++) // fill array with random numbers { long n = (int) (java.lang.Math.random() * 99); arr.insert(n); } arr.display(); arr.quickSort(); 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.


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
Quick sort with median-of-three partitioning Java Tip Algorithms 0 04-15-2008 09:40 PM
Quick Help Please! Can't Run Code!! VinceGuad Eclipse 4 01-16-2008 05:54 AM
Quick Job required in Java taxman Jobs Offered 0 01-02-2008 01:46 PM
Quick Question (Functions) ibanez270dx New To Java 2 11-16-2007 03:42 AM


All times are GMT +3. The time now is 11:59 AM.


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