QUICKSORT: METHOD - PowerPoint PPT Presentation

About This Presentation
Title:

QUICKSORT: METHOD

Description:

use middle value of current partition to split it ... bolding marks values selected for swap: 24 18. bold vertical borders mark partitions of array: ... – PowerPoint PPT presentation

Number of Views:68
Avg rating:3.0/5.0
Slides: 5
Provided by: jamesr59
Category:

less

Transcript and Presenter's Notes

Title: QUICKSORT: METHOD


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 lt pVal) i
  • while (arr j gt pVal) j--
  • // swap "out of partition" elements
  • if (i lt j)
  • if (i lt j) swap (i, j, arr)
  • i
  • j--
  • // end if
  • while (i lt j)
  • // invoke quicksort on resulting partitions
  • if (left lt j) quickSort (left, j, arr)
  • if (i lt right) quickSort (i, right, arr)
  • // end quickSort method

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 24 18
  • 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

3
QUICKSORT TRACE

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 lt j) fails
  • or test for (i lt right) fails
Write a Comment
User Comments (0)
About PowerShow.com