Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

QuickSort help

807606May 26 2007 — edited May 29 2007
// Exercise 16.10: QuickSort.java
// Class that creates an integer array filled with random
// values and can sort that array using the quicksort algorithm


// Your Name Here

public class QuickSort
{
    private int[] data; // array of values
   
    // create array of given size and fill with random integers
    public QuickSort( int[] newData )
    {
    	data = newData; // create array

    } // end QuickSort constructor

    // call recursive method quicksortHelper
    public void sort()
    {
       quicksortHelper( 0, data.length - 1 );      
    } // end method sort

	// recursive method to sort array using quicksort

	private void quicksortHelper( int left, int right )
	{
		int middle = (left + right) / 2;		
		swap(data[middle], right); 
		
		for(int i = 0; i < data.length - 1; i++){
			if(data[i] > right){
				swap(data[middle], data);
data[left] = left + 1;
}
swap(data[right], data[left]);
}

} // end method quicksortHelper

// helper method to swap values in two elements
private void swap( int first, int second )
{
int temporary = data[ first ]; // store first in temporary
data[ first ] = data[ second ]; // replace first with second
data[ second ] = temporary; // put temporary in second
} // end method swap

// method to output values in array
public String toString ()
{
StringBuffer temporary = new StringBuffer();

// iterate through array
for ( int element : data )
temporary.append( element + " " );

temporary.append( "\n" ); // add endline character
return temporary.toString();
} // end method toString

public static void main (String[] args) {



}


} // end class QuickSort



ok, this is the quicksort stuff that i wrote but I'm starting to doubt this will work so can anyone please please please tell me how i can check to see if it's running correctly using main method (i dont know what to write in there >_<) or tell me this works... if not, how to fix it... thx a lot
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 26 2007
Added on May 26 2007
15 comments
196 views