lecture-12

With only one partition this partition is of size n 1

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

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: n recursive calls to Quicksort! CPSC 223  ­ ­ Fall 2010 17 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 18 9 10/12/10 Quicksort Partitioning step (where the work is done) •  Worst case –  When pivot is smallest (largest) element –  We end up with only one partition –  This partition is of size n – 1 •  In general, the partition step has O(n) comparisons and swaps •  We do this partitioning n – 1 times in the worst case •  Therefore quicksort is O(n2) in the worst case CPSC 223  ­ ­ Fall 2010 19 Quicksort Quicksort is much better in practice –  Each recursive step on avg. partitions array into equal halves –  So we have approx. log2n rec...
View Full Document

This document was uploaded on 03/18/2014 for the course CPSC 223 at Gonzaga.

Ask a homework question - tutors are online