Assign8 - /Michelle Tao 463 1035 The"Assign8 class Program...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
//Michelle Tao 463 1035 / // The "Assign8" class /* Program information here */ import apsc142.*; // access Fmt class for formatted output public class Assign8 { // some useful constants public static final int NUM_SORTS = 4, QUICK_SORT = 0, SELECTION_SORT = 1, INSERTION_SORT = 2, BUBBLE_SORT = 3; public static final int NUM_SAMPLES = 10, NUM_IN_SAMPLE = 1024; public static void generateData (double [] [] dataArray) { for (int i = 0 ; i < NUM_SAMPLES ; i++) { for (int j = 0 ; j < NUM_IN_SAMPLE ; j++) { dataArray [i] [j] = Math.random (); } } /* //print to check / for (int i = 0 ; i < NUM_SAMPLES ; i++) { for (int j = 0 ; j < NUM_IN_SAMPLE ; j++) { Fmt.printf ("%3.2f", dataArray [i] [j]); } System.out.println (""); } */ * } // end generateData // a recursive method implementing quicksort // parameters are the left (lower) and right (upper) public static void quickSort (int l, int r, double [] sortArray) { int i = l, j = r; double partitionVal; // use middle value of current partition to split it partitionVal = sortArray [(l + r) / 2]; do { while (sortArray [i] < partitionVal) i++; while (sortArray [j] > partitionVal) j--; if (i <= j) {
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
swap (sortArray, i, j); i++; j--; } // end if } while (i <= j); if (l < j) quickSort (l, j, sortArray); if (i < r) quickSort (i, r, sortArray); } // end quickSort method // a recursive method implementing quicksort // parameters are the left (lower) and right (upper) // bounds of the portion of the array to sort // and the array itself public static int quickSort2 (int l, int r, double [] sortArray) { int i = l, j = r; double partitionVal; int comps = 0; i // use middle value of current partition to split it partitionVal = sortArray [(l + r) / 2]; do { while (sortArray [i] < partitionVal) { comps++; i++; } comps++; while (sortArray [j] > partitionVal) { comps++; j--; } comps++; if (i <= j) { swap (sortArray, i, j); i++; j--; } // end if } while (i <= j); if (l < j) comps = comps + quickSort2 (l, j, sortArray);
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/01/2010 for the course APPLIED SC 142 taught by Professor Professorstansimmons during the Winter '10 term at Queens University.

Page1 / 7

Assign8 - /Michelle Tao 463 1035 The"Assign8 class Program...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online