Lecture 14

Info icon This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 10/19/10 CPSC 223 Algorithms & Data Abstract Structures Lecture 14: Quicksort exercise Binary Search Tree Traversal and Deletion Today … •  Quiz 6 •  Sorting Exercise (Quicksort) •  Tree traversals •  Binary Search Tree Deletion –  Ch 10: pp. 546-563 CPSC 223  ­ ­ Fall 2010 2 1 10/19/10 Quicksort (based on textbook) void Quicksort(Entry theArray, int first, int last) { if(first < last) { int pivotIndex = Partition(theArray, first, last); // quicksort first half Quicksort(theArray, first, pivotIndex – 1); // quicksort second half Quicksort(theArray, pivotIndex + 1, last); } } CPSC 223  ­ ­ Fall 2010 3 Quicksort (based on textbook) int Partition(Entry theArray, int first, int last) { Entry pivot = theArray[first]; // pivot value int lastP1 = first; // last index of first partition for(int i = first + 1; i <= last; i++) { if(theArray[i] < pivot) { lastP1++; Swap(theArray[i], theArray[lastP1]); } } Swap(theArray[first], theArray[lastP1]); return lastP1; } CPSC 223  ­ ­ Fall 2010 4 2 10/19/10 Binary Search Tree Traversal CPSC 223  ­ ­ Fall 2010 5 Traversing binary trees D B A G C E H F •  When traversing a tree, we typically “visit” every node •  As we visit nodes, apply operations (like pr...
View Full 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