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

Info icon This 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) // bounds of the portion of the array to sort, & the array itself 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) {
Image of page 1

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

View Full Document Right 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); if (i < r) comps = comps + quickSort2 (i, r, sortArray);
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern