1 / 4

QUICKSORT: METHOD

QUICKSORT: METHOD. public static void quickSort (int left, int right, int [ ] arr) { // use middle value of current partition to split it int i = left, j = right, pVal = arr [(left + right) / 2]; do { // move index vals towards middle while values conform to partition

Download Presentation

QUICKSORT: METHOD

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. QUICKSORT: METHOD public static void quickSort (int left, int right, int [ ] arr) { // use middle value of current partition to split it int i = left, j = right, pVal = arr [(left + right) / 2]; do { // move index vals towards middle while values conform to partition while (arr [i] < pVal) i++; while (arr [j] > pVal) j--; // swap "out of partition" elements if (i <= j) { if (i < j) swap (i, j, arr); i++; j--; } // end if } while (i <= j); // invoke quicksort on resulting partitions if (left < j) quickSort (left, j, arr); if (i < right) quickSort (i, right, arr); } // end quickSort method CISC-101 SPRING 2003

  2. QUICKSORT: ILLUSTRATIONS • next 2 pages show quicksort method in operation • first, listing array contents as they change • underlining marks chosen partitioning value: 21 • bolding marks values selected for swap: 2418 • bold vertical borders mark partitions of array: | • second, graphical representation of recursive calls • red & green arrows mark recursion on left & right partitions • dashed blue arrows mark base case, recursion ending CISC-101 SPRING 2003

  3. QUICKSORT: TRACE CISC-101 SPRING 2003

  4. QUICKSORT: CALL TRACE • Quick Sort execution tree (for the above example) • RED: recurse on left partition • GREEN: recurse on right partition • BLUE: recursion ends • test for (left < j) fails • or test for (i < right) fails CISC-101 SPRING 2003

More Related