{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture12 - quicksort, merge sort

lecture12 - quicksort, merge sort - Monday Feb 23 rd...

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

View Full Document Right Arrow Icon
Monday, Feb 23 rd     Sorting Algorithms, part II: Quicksort Mergesort Introduction to Trees Review the partition fu Its complicated and eas
Image of page 1

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

View Full Document Right 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.
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
Image of page 3

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

View Full Document Right 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
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
Image of page 5

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

View Full Document Right 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.
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 0               7 13 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
Image of page 7

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

View Full Document Right Arrow Icon
Image of page 8
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