*This preview shows
pages
1–4. Sign up
to
view the full content.*

This
** preview**
has intentionally

**sections.**

*blurred***to view the full version.**

*Sign up*This
** preview**
has intentionally

**sections.**

*blurred***to view the full version.**

*Sign up*
**Unformatted text preview: **Introduction to Computer Programming October 2, 2008 CSC180 Fall 2008, University of Toronto Quicksort The quicksort algorithm was developed by Hoare in 1962. Use a partition element to divide an unsorted array into two subsets. All elements in subset one are less than the partition element. All elements in subset two are greater than or equal to the partition element. The following code uses the middle element as the partition element. An implementation of the quicksort algorithm using recursion: CSC180 Fall 2008, University of Toronto 1 /* quicksort.c */ /* interchange v[i] and v[j] */ void swap(int v, int i, int j) { int temp; temp = v[i]; v[i] = v[j]; v[j] = temp; } /* sort v[left]...v[right] into increasing order */ void qsort(int v, int left, int right) { int i, last; if (left >= right) /* do nothing if array contains */ return; /* fewer than two elements */ swap(v, left, (left + right)/2); /* move partition elem */ last = left; /* to v[0] */ for (i = left+1; i <= right; i++)...

View
Full
Document