18.04.2013 Views

The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Lecture 4 - heapsort<br />

Listen To Part 4-8<br />

A simple sorting algorithm is selection sort.<br />

Selection Sort<br />

Sweep through all the elements to find the smallest item, then the smallest remaining item, etc. until the<br />

array is sorted.<br />

Selection-sort(A)<br />

swap(A[i],A[j])<br />

for i = 1 to n<br />

for j = i+1 to n<br />

if (A[j] < A[i]) then<br />

It is clear this algorithm must be correct from an inductive argument, since the ith element is in its correct<br />

position.<br />

It is clear that this algorithm takes time.<br />

It is clear that the analysis of this algorithm cannot be improved because there will be n/2 iterations<br />

which will require at least n/2 comparisons each, so at least comparisons will be made. More careful<br />

analysis doubles this.<br />

Thus selection sort runs in time.<br />

Listen To Part 4-9<br />

Binary Heaps<br />

A binary heap is defined to be a binary tree with a key in each node such that:<br />

file:///E|/LEC/LECTUR17/NODE4.HTM (5 of 14) [19/1/2003 1:34:31]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!