lecture12

lecture12 - Monday,May15 SortingAlgorithms,partII:...

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

View Full Document Right Arrow Icon
Monday, May 15 th Sorting Algorithms, part II: Quicksort Mergesort Introduction to Trees No midterms yet!
Background image of page 1

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

View Full DocumentRight Arrow Icon
Divide and Conquer  Sorting Any time you see “ divide and conquer ,” you should think  recursion ...   EEK! These sorting schemes generally work as follows: 1. Divide  the elements to be sorted into two groups of roughly  equal size. 2. Sort  each of these smaller groups of elements (recursively). 3. Combine  the two sorted groups into one large sorted list.
Background image of page 2
D&Q Sorts: Quicksort QuickSort is a  classic  divide and conquer sort. It’s very much like the  recursive card sort we learned  earlier. 1. Compute the  median number   in the pile 1. Put cards with values  less  than the median  in a pile on  the  left 3. Put cards with values  greater  than or equal to the median   in a pile on the  right 4. Repeat steps 1-4 on each sub- pile
Background image of page 3

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

View Full DocumentRight Arrow Icon
  1. Compute the  median number   in the pile 1. Put cards with values  less  than the median  in a pile on  the  left 3. Put cards with values  greater  than or equal to the median   in a pile on the  right 4. Repeat steps 1-4 on each sub- pile 9 3 2 7 4 1 5 Median! 5 9 3 2 7 4 1 Median! 3 2 1 Median! 5 9 4 1 Median! 3 2 4 5 7 7 9 Median! By applying the same algorithm to simpler and  simpler subproblems, we can quickly  solve  complex problems ! D&Q Sorts: Quicksort
Background image of page 4
The Quicksort Algorithm 1. Select an arbitrary element  P  from the array. 1. If the array contains only 0 or 1 element,  return . 1. Move all elements that are  less than or equal  to  P  to the  left of the array   and all elements  greater than P  to the  right  (this is called  partitioning ). Divide 4. Recursively repeat this process on the left sub-array and then the right  sub-array. Conquer
Background image of page 5

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

View Full DocumentRight Arrow Icon
D&Q Sorts: Quicksort And here’s an actual Quicksort C++ function: void QuickSort(int Array[],int First,int Last) { if (Last – First >= 1 ) { int PivotIndex; PivotIndex = Partition (Array,First,Last); QuickSort (Array,First,PivotIndex-1); // left QuickSort (Array,PivotIndex+1,Last); // right } } The  Partition  function  divides  the array into two sub-arrays (small items  to the left and large to the right). The first call to  QuickSort  recursively sorts the left sub-array, and the second  call sorts the right sub-array.
Background image of page 6
QuickSort in Action void QuickSort(int Array[],int First,int Last) { if (Last – First >= 1 ) { int PivotIndex; PivotIndex = Partition (Array,First,Last); QuickSort (Array, First , PivotIndex-1 ); // left QuickSort (Array, PivotIndex+1 , Last ); // right } } First Last PivotIndex 30 1 77 0               7 13 21 3 void QuickSort(int Array[],int First,int Last) { if (Last – First >= 1 ) { int PivotIndex; PivotIndex = Partition (Array,First,Last); QuickSort (Array, First , PivotIndex-1 ); // left QuickSort (Array, PivotIndex+1 , Last ); // right } } First Last PivotIndex
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 34

lecture12 - Monday,May15 SortingAlgorithms,partII:...

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

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