LEC20081002

LEC20081002 - Introduction to Computer Programming October...

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

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

View Full Document

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

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

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

{[ snackBarMessage ]}

Page1 / 11

LEC20081002 - Introduction to Computer Programming October...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online