import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; /** * Programming Assignement 1 Solution * This class uses quicksort to sort an n-element list of integers * and then displays the kth quantiles. * @author Luis Marrero * @version 1.0, 15 Feb 1999 * @since JDK1.1.x */ public class QSort { private int count; /** This method implements the QuickSort algorithm. * This will handle arrays that are already * sorted, and arrays with duplicate keys.
* * If you think of a one dimensional array as going from * the lowest index on the left to the highest index on the right * then the parameters to this function are lowest index or * left and highest index or right. The first time you call * this function it will be with the parameters 0, a.length - 1. * * @param a an integer array * @param lo0 left boundary of array partition * @param hi0 right boundary of array partition */ private void QuickSort(int a[], int lo0, int hi0) throws Exception { int lo = lo0; int hi = hi0; int mid; if ( hi0 > lo0) { /* Arbitrarily establishing partition element as the midpoint of * the array. */ mid = a[ ( lo0 + hi0 ) / 2 ]; // loop through the array until indices cross while( lo <= hi ) { /* find the first element that is greater than or equal to * the partition element starting from the left Index. */ while( ( lo < hi0 ) && ( a[lo] < mid )) { count++; ++lo; } /* find an element that is smaller than or equal to * the partition element starting from the right Index. */ while( ( hi > lo0 ) && ( a[hi] > mid )) { count++; --hi; } // if the indexes have not crossed, swap if( lo <= hi ) { swap(a, lo, hi); ++lo; --hi; } } /* If the right index has not reached the left side of array * must now sort the left partition. */ if( lo0 < hi ) QuickSort( a, lo0, hi ); /* If the left index has not reached the right side of array * must now sort the right partition. */ if( lo < hi0 ) QuickSort( a, lo, hi0 ); } } /** This method handle the actual swapping of two elements * * @param a an integer array * @param i destination index within the array * @param j source index within the array */ private void swap(int a[], int i, int j) { int T; T = a[i]; a[i] = a[j]; a[j] = T; } /** This method is called by the main program * used to sort an array of integers * @param a an integer array */ public int sort(int a[]) throws Exception { count = 0; QuickSort(a, 0, a.length - 1); return count; } public static void main( String args[] ) { int a[] = new int[5000]; int from = 1; int to = 5000; int k_value = 0; int num = 0; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); // read input and populate array to be sorted for(int i = from-1; i < to; ) { //int tmp = i+1; //System.out.print("input integer " + tmp + ": "); try{ String line = in.readLine(); if(line != null && line.length() != 0) { a[i] = (new Integer(line.trim())).intValue(); i++; } }catch(IOException x){ System.out.print("IOException occured: " + x); } } //now read k value while(k_value <= 0) try{ //System.out.print("input k-value: "); String line = in.readLine(); if(line != null && line.length() != 0) { k_value = (new Integer(line.trim())).intValue(); } }catch(IOException x){ System.out.print("IOException occured: " + x); } try{ // sort the array num = (new QSort()).sort(a); // now display k-1 quantiles int size = to/k_value; for(int i = size; i < to; i+=(size+1)) { System.out.println(a[i]); } } catch(Exception x) { System.out.println("Exception occured: " + x); return; } System.out.println("Num of comparisons = " + num); } }