Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 680
  • Last Modified:

Java QuickSort and Selection Sort

I need to build two sorting methods:  a selection sort, and a quicksort.  I found sample code online at http://www.roseindia.net

I built a class called Sort, and included the quickSort and the selectionSort.  Then I built a tester called SortTester, and I included the testing code for both methods.

However, I can't get the code running.  I'm not sure what I'm doing wrong.

Thanks,
Lynn
import java.util.Date;
public class SortTester {

	public static void main(String a[]){
		// selectionSort testing
	    int i;
	    int array[] = {12,9,4,99,120,1,3,10};
	    System.out.println("\n\n       RoseIndia\n\n");
	    System.out.println("       Selection Sort\n\n");   
	    System.out.println("Values Before the sort:\n");    
	    for(i = 0; i < array.length; i++)
	      System.out.print( array[i]+"  ");
	    System.out.println();
	    selectionSort(array, array.length);        
	    System.out.print("Values after the sort:\n");    
	    for(i = 0; i <array.length; i++)
	      System.out.print(array[i]+"  ");
	    System.out.println();
	    System.out.println("PAUSE");
	  }


		// quickSort testing
	    int k;
	    int array1[] = {12,9,4,99,120,1,3,10,13};
	    System.out.println("\n\n" + "       RoseIndia" + "\n\n");
	    System.out.println("       Quick Sort\n\n");
	    System.out.println("Values Before the sort:\n");
	    for(k = 0; k < array1.length; i++)
	      System.out.print( array1[k]+"  ");
	    System.out.println();
	    quickSort(array1, 0, array1.length - 1);
	    System.out.print("Values after the sort:\n");
	    for(k = 0; k <array1.length; i++)
	      System.out.print(array1[k]+"  ");
	    System.out.println();
	    System.out.println("PAUSE");
	  }

}

Open in new window

public class Sort{

	/**
	 * Method: public Sort(int array[], int n)
	 * 		
	 * Parameters:
	 * 		array[] - the capacity of the array
	 * 		n - the minimum value in the population
	 * Returns:
	 * 		an instance of sort which is an array of sorted items 
	 * 				
	 * Postcondition:
	 * 		an instance of a sorted array
	 */
	 
	// code from Rose India Technologies 
	// http://www.roseindia.net/java/beginners/arrayexamples/SelectionSort.shtml
	
	  public static void selectionSort(int array[], int n){
	    for(int x=0; x<n; x++){
	      int index_of_min = x;
	      for(int y=x; y<n; y++){
	        if(array[index_of_min]<array[y]){
	          index_of_min = y;
	        }
	      }
	      int temp = array[x];
	      array[x] = array[index_of_min];
	      array[index_of_min] = temp;
	    }
	  }
	/**
	 * Constructor Method: quickSort(int array[],int low, int n)
	 * 		
	 * Parameters:
	 * 		array[] - the capacity of the array
	 * 		low - the minimum value in the population
	 * 		n - length of array
	 * Returns:
	 * 		an instance of sort which is an array of sorted items 
	 * 			
	 * Postcondition:
	 * 		an instance of a sorted array
	 */
	  
	// code credited to C. A. R. Hoare from Rose India Technologies 
	// http://www.roseindia.net/java/beginners/arrayexamples/SelectionSort.shtml	  
	  
	public static void quickSort(int arrayIn[],int low, int n){
		    int lo = low;
		    int hi = n;
		    if (lo >= n) {
		      return;
		    }
		    int mid = arrayIn[(lo + hi) / 2];
		    while (lo < hi) {
		      while (lo<hi && arrayIn[lo] < mid) {
		        lo++;
		      }
		      while (lo<hi && arrayIn[hi] > mid) {
		        hi--;
		      }
		      if (lo < hi) {
		        int T = arrayIn[lo];
		        arrayIn[lo] = arrayIn[hi];
		        arrayIn[hi] = T;
		      }
		    }
		    if (hi < lo) {
		      int T = hi;
		      hi = lo;
		      lo = T;
		    }
		    quickSort(arrayIn, low, lo);
		    quickSort(arrayIn, lo == low ? lo+1 : lo, n);
		  }
		}

Open in new window

0
Lambel
Asked:
Lambel
  • 2
  • 2
1 Solution
 
objectsCommented:
you need to specify class name when calling static methods
plus you seem to have an extra brace

try this

public class SortTester {

	public static void main(String a[]) {
		// selectionSort testing
		int i;
		int array[] = { 12, 9, 4, 99, 120, 1, 3, 10 };
		System.out.println("\n\n       RoseIndia\n\n");
		System.out.println("       Selection Sort\n\n");
		System.out.println("Values Before the sort:\n");
		for (i = 0; i < array.length; i++)
			System.out.print(array[i] + "  ");
		System.out.println();
		Sort.selectionSort(array, array.length);
		System.out.print("Values after the sort:\n");
		for (i = 0; i < array.length; i++)
			System.out.print(array[i] + "  ");
		System.out.println();
		System.out.println("PAUSE");

		// quickSort testing
		int k;
		int array1[] = { 12, 9, 4, 99, 120, 1, 3, 10, 13 };
		System.out.println("\n\n" + "       RoseIndia" + "\n\n");
		System.out.println("       Quick Sort\n\n");
		System.out.println("Values Before the sort:\n");
		for (k = 0; k < array1.length; i++)
			System.out.print(array1[k] + "  ");
		System.out.println();
		Sort.quickSort(array1, 0, array1.length - 1);
		System.out.print("Values after the sort:\n");
		for (k = 0; k < array1.length; i++)
			System.out.print(array1[k] + "  ");
		System.out.println();
		System.out.println("PAUSE");
	}

}

Open in new window

0
 
for_yanCommented:
This at least compiles:
Try it.

import java.util.Date;
public class SortTester {

	public static void main(String a[]){
		// selectionSort testing
	    int i;
	    int array[] = {12,9,4,99,120,1,3,10};
	    System.out.println("\n\n       RoseIndia\n\n");
	    System.out.println("       Selection Sort\n\n");   
	    System.out.println("Values Before the sort:\n");    
	    for(i = 0; i < array.length; i++)
	      System.out.print( array[i]+"  ");
	    System.out.println();
	    Sort.selectionSort(array, array.length);        
	    System.out.print("Values after the sort:\n");    
	    for(i = 0; i <array.length; i++)
	      System.out.print(array[i]+"  ");
	    System.out.println();
	    System.out.println("PAUSE");
	  


		// quickSort testing
	    int k;
	    int array1[] = {12,9,4,99,120,1,3,10,13};
	    System.out.println("\n\n" + "       RoseIndia" + "\n\n");
	    System.out.println("       Quick Sort\n\n");
	    System.out.println("Values Before the sort:\n");
	    for(k = 0; k < array1.length; i++)
	      System.out.print( array1[k]+"  ");
	    System.out.println();
	    Sort.quickSort(array1, 0, array1.length - 1);
	    System.out.print("Values after the sort:\n");
	    for(k = 0; k <array1.length; i++)
	      System.out.print(array1[k]+"  ");
	    System.out.println();
	    System.out.println("PAUSE");
	  }

}

Open in new window

0
 
objectsCommented:
you've also got a problem in the var names in your loops

public class SortTester {

	public static void main(String a[]) {
		// selectionSort testing
		int i;
		int array[] = { 12, 9, 4, 99, 120, 1, 3, 10 };
		System.out.println("\n\n       RoseIndia\n\n");
		System.out.println("       Selection Sort\n\n");
		System.out.println("Values Before the sort:\n");
		for (i = 0; i < array.length; i++)
			System.out.print(array[i] + "  ");
		System.out.println();
		Sort.selectionSort(array, array.length);
		System.out.print("Values after the sort:\n");
		for (i = 0; i < array.length; i++)
			System.out.print(array[i] + "  ");
		System.out.println();
		System.out.println("PAUSE");

		// quickSort testing
		int k;
		int array1[] = { 12, 9, 4, 99, 120, 1, 3, 10, 13 };
		System.out.println("\n\n" + "       RoseIndia" + "\n\n");
		System.out.println("       Quick Sort\n\n");
		System.out.println("Values Before the sort:\n");
		for (k = 0; k < array1.length; k++)
			System.out.print(array1[k] + "  ");
		System.out.println();
		Sort.quickSort(array1, 0, array1.length - 1);
		System.out.print("Values after the sort:\n");
		for (k = 0; k < array1.length; k++)
			System.out.print(array1[k] + "  ");
		System.out.println();
		System.out.println("PAUSE");
	}

}

Open in new window

0
 
for_yanCommented:

The first part was correct but in second part you left i++ instaed of k++ in the loop

import java.util.Date;
public class SortTester {

	public static void main(String a[]){
		// selectionSort testing
	    int i;
	    int array[] = {12,9,4,99,120,1,3,10};
	    System.out.println("\n\n       RoseIndia\n\n");
	    System.out.println("       Selection Sort\n\n");
	    System.out.println("Values Before the sort:\n");
	    for(i = 0; i < array.length; i++)
	      System.out.print( array[i]+"  ");
	    System.out.println();
	    Sort.selectionSort(array, array.length);
	    System.out.print("Values after the sort:\n");
	    for(i = 0; i <array.length; i++)
	      System.out.print(array[i]+"  ");
	    System.out.println();
	    System.out.println("PAUSE");

            

		// quickSort testing
	    int k;
	    int array1[] = {12,9,4,99,120,1,3,10,13};
	    System.out.println("\n\n" + "       RoseIndia" + "\n\n");
	    System.out.println("       Quick Sort\n\n");
	    System.out.println("Values Before the sort:\n");
	    for(k = 0; k < array1.length; k++)
	      System.out.print( array1[k]+"  ");
	    System.out.println();
	    Sort.quickSort(array1, 0, array1.length - 1);
	    System.out.print("Values after the sort:\n");
	    for(k = 0; k <array1.length; k++)
	      System.out.print(array1[k]+"  ");
	    System.out.println();
	    System.out.println("PAUSE");
	  }

}

Open in new window

0
 
LambelAuthor Commented:
Objects,
Thanks, you've saved me once again.  You are some kind of genius! I've really appreciated all your quick responses this last year or so.  I think this should be the last project I have to do working in Java (thank heavens!). How's you C#??  I'm starting to learn C# this summer.  
Lynn
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now