//Group Members: David LaPorte & David Botelho //Programming Assignment 1, 2/16/99 import java.util.StringTokenizer; import java.util.Vector; import java.util.Random; import java.io.*; import java.lang.Math; class Quantiles { static int inputnum = 5000; static int quantile = 0; static int numbers[] = new int[inputnum]; static int compares = 0; //Quicksort Subroutine static int[] QuickSort(int a[], int lo0, int hi0) { //Lower Array Bound int lo = lo0; //Upper Array Bound int hi = hi0; int mid; if ( hi0 > lo0) { compares++; //Determines the mid value to use for partitioning the array mid = a[ ( lo0 + hi0 ) / 2 ]; while( lo <= hi ) { compares++; while( ( lo < hi0 ) && ( a[lo] < mid ) ) { compares++; ++lo; } while( ( hi > lo0 ) && ( a[hi] > mid ) ) { compares++; --hi; } if( lo <= hi ) { compares++; swap(a, lo, hi); ++lo; --hi; } } if( lo0 < hi ) { compares++; //Recursive call to Quicksort QuickSort( a, lo0, hi ); } if( lo < hi0 ) { compares++; QuickSort( a, lo, hi0 ); } } return(a); } //Swaps the low and high vales (called from within Quicksort) private static void swap(int a[], int i, int j) { int T; T = a[i]; a[i] = a[j]; a[j] = T; } public static void sort(int a[]) { QuickSort(a, 0, a.length - 1); } //Main subroutine public static void main(String arg[]) { int number = 0; Integer temp = new Integer(0); Quantiles q = new Quantiles(); //Read from standart input BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); for (int i = 0; i < inputnum; i++) { try { number = temp.parseInt(input.readLine()); } catch(IOException io) {} numbers[i] = number; } try { //Read quantiles from stdin quantile = temp.parseInt(input.readLine()); } catch(IOException io) {} int[] sorted = QuickSort(numbers, 0, numbers.length - 1); int counter = inputnum/quantile; for (int i = 0; i < quantile - 1; i++) { if (counter < inputnum - 1) { //Output quantile elements System.out.println(sorted[counter]); counter += inputnum/quantile; } } //Output Comparisons System.out.println("Comparisons: "+compares); } }