23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

R-11.21<br />

Give an example <strong>in</strong>put list that requires merge-sort <strong>and</strong> heap-sort to take<br />

O(nlogn) time to sort, but <strong>in</strong>sertion-sort runs <strong>in</strong> O(n) time. What if you reverse<br />

this list?<br />

R-11.22<br />

Describe, <strong>in</strong> pseudo-code, how to perform path compression on a path of length<br />

h <strong>in</strong> O(h) time <strong>in</strong> a tree-based partition union/f<strong>in</strong>d structure.<br />

R-11.23<br />

George claims he has a fast way to do path compression <strong>in</strong> a partition structure,<br />

start<strong>in</strong>g at a node v. He puts v <strong>in</strong>to a list L, <strong>and</strong> starts follow<strong>in</strong>g parent po<strong>in</strong>ters.<br />

Each time he encounters a new node, u, he adds u to L <strong>and</strong> updates the parent<br />

po<strong>in</strong>ter of each node <strong>in</strong> L to po<strong>in</strong>t to u'S parent. Show that George's algorithm<br />

runs <strong>in</strong> (h 2 ) time on a path of length h.<br />

R-11.24<br />

Describe an <strong>in</strong>-place version of the quick-select algorithm <strong>in</strong> pseudo-code.<br />

R-11.25<br />

Show that the worst-case runn<strong>in</strong>g time of quick-select on an n-element sequence<br />

is (n 2 ).<br />

Creativity<br />

C-11.1<br />

L<strong>in</strong>da claims to have an algorithm that takes an <strong>in</strong>put sequence S <strong>and</strong> produces<br />

an output sequence T that is a sort<strong>in</strong>g of the n elements <strong>in</strong> S.<br />

a.<br />

Give an algorithm, isSorted, for test<strong>in</strong>g <strong>in</strong> O(n) time if T is sorted.<br />

b.<br />

Expla<strong>in</strong> why the algorithm isSorted is not sufficient to prove a particular<br />

output T to L<strong>in</strong>da's algorithm is a sort<strong>in</strong>g of S.<br />

c.<br />

732

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

Saved successfully!

Ooh no, something went wrong!