lecture-14

# Lecture 14

• 19

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 ﬁrst, int last) { if(ﬁrst < last) { int pivotIndex = Partition(theArray, ﬁrst, last); // quicksort ﬁrst half Quicksort(theArray, ﬁrst, pivotIndex – 1); // quicksort second half Quicksort(theArray, pivotIndex + 1, last); } } CPSC 223  ­ ­ Fall 2010 3 Quicksort (based on textbook) int Partition(Entry theArray, int ﬁrst, int last) { Entry pivot = theArray[ﬁrst]; // pivot value int lastP1 = ﬁrst; // last index of ﬁrst partition for(int i = ﬁrst + 1; i <= last; i++) { if(theArray[i] < pivot) { lastP1++; Swap(theArray[i], theArray[lastP1]); } } Swap(theArray[ﬁrst], 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

• 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.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern