30.08.2014 Views

Introduction: Data Structures and ADTs - Student.cs.uwaterloo.ca ...

Introduction: Data Structures and ADTs - Student.cs.uwaterloo.ca ...

Introduction: Data Structures and ADTs - Student.cs.uwaterloo.ca ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Sorting<br />

CS 234: <strong>Data</strong> Types <strong>and</strong> <strong>Structures</strong><br />

Omer Beg<br />

David R. Cheriton School of Computer Science<br />

University of Waterloo<br />

Spring 2013<br />

Beg (CS, UW)<br />

CS234 - <strong>Data</strong> <strong>Structures</strong> 1


From Last Class …<br />

Binary Trees<br />

Beg (CS, UW)<br />

CS234 - <strong>Data</strong> <strong>Structures</strong> 2


Another implementation of<br />

ADT Priority Queue<br />

Implement an unbounded Priority Queue using<br />

a max-heap<br />

Store items with their priorities<br />

Use priorities to shape the heap<br />

Beg (CS, UW) CS234 - <strong>Data</strong> <strong>Structures</strong> 3


Heap-based Priority Queue<br />

Organization of data:<br />

_L – Python list to store items with priorities<br />

PriorityQueue()<br />

Set _L = []<br />

isEmpty()<br />

Does len(_L) == 0<br />

len()<br />

Return len(_L)<br />

Beg (CS, UW) CS234 - <strong>Data</strong> <strong>Structures</strong> 4


pq.enqueue(item, priority)<br />

Add item with priority to end of _L<br />

while item's priority > parent's priority:<br />

Swap item <strong>and</strong> parent<br />

Update indices for parent <strong>and</strong> child<br />

Beg (CS, UW) CS234 - <strong>Data</strong> <strong>Structures</strong> 5


Adding 90: What actually happens<br />

Beg (CS, UW) CS234 - <strong>Data</strong> <strong>Structures</strong> 6


And then …<br />

Beg (CS, UW) CS234 - <strong>Data</strong> <strong>Structures</strong> 7


Efficiency of enqueue<br />

Adding to end of heap:<br />

List/Array: amortized<br />

List/Array: worst <strong>ca</strong>se<br />

"Sifting" up –<br />

Maximum iterations<br />

Steps per iterations<br />

Total<br />

Overall: Worst <strong>ca</strong>se= _____ Amortized=_____<br />

Beg (CS, UW) CS234 - <strong>Data</strong> <strong>Structures</strong> 8


pq.dequeue() =><br />

item with maximum priority<br />

Move last entry into root<br />

While parent's priority < max children's priority:<br />

Swap parent <strong>and</strong> child with higher priority<br />

Update indices for parent <strong>and</strong> child<br />

Beg (CS, UW) CS234 - <strong>Data</strong> <strong>Structures</strong> 9


Efficiency of dequeue<br />

Removing/Swapping last <strong>and</strong> first of heap:<br />

List: amortized<br />

List: worst <strong>ca</strong>se<br />

Array:<br />

"Sifting" down –<br />

Maximum iterations<br />

Steps per iterations<br />

Total<br />

List Overall: Worst <strong>ca</strong>se= _____ Amortized=_____<br />

Array Overall: Worst <strong>ca</strong>se= _____ Amortized=_____<br />

Beg (CS, UW) CS234 - <strong>Data</strong> <strong>Structures</strong> 10


Another use of heaps<br />

Suppose we have a list L.<br />

Create an empty heap<br />

for each x in L:<br />

Add x to the heap<br />

for ind in range(n-1,-1,0):<br />

Remove max from heap, place in L[ind]<br />

We have a sorting algorithm!<br />

Beg (CS, UW) CS234 - <strong>Data</strong> <strong>Structures</strong> 11


Runtime of Heapsort<br />

Allo<strong>ca</strong>te array for heap to be same length as L<br />

Building the heap:<br />

Add is, at worst, O(log n)<br />

n elements added<br />

O(n log n)<br />

Refilling L:<br />

Remove is, at worst, O(log n)<br />

n elements removed<br />

O(n log n)<br />

Beg (CS, UW) CS234 - <strong>Data</strong> <strong>Structures</strong> 12


Improvements to Heapsort<br />

Algorithm requires extra array/list for heap<br />

Use the original array/list instead<br />

In-place algorithm<br />

Requires O(1) additional memory<br />

Have L share its space with the heap when<br />

Constructing the heap,<br />

Re-filling the array<br />

Heap: Uses first part of array/list<br />

Beg (CS, UW) CS234 - <strong>Data</strong> <strong>Structures</strong> 13


"in place" heapsort<br />

Insert first value into the heap:<br />

Insert 51 as new leaf, "sift up":<br />

Beg (CS, UW) CS234 - <strong>Data</strong> <strong>Structures</strong> 14


Beg (CS, UW) CS234 - <strong>Data</strong> <strong>Structures</strong> 15


Retrieving the sorted data<br />

Remove max from heap<br />

Place just past "end" of heap in array<br />

Repeat until heap "empty"<br />

Array is now in sorted order!<br />

Beg (CS, UW) CS234 - <strong>Data</strong> <strong>Structures</strong> 16


Beg (CS, UW) CS234 - <strong>Data</strong> <strong>Structures</strong> 17


Basi<strong>cs</strong> of Sorting<br />

L = [x 0 ,x 1 ,x 2 ,…,x n ]<br />

Collection of comparable data<br />

Comparable:<br />

Given two unequal values, <strong>ca</strong>n determine<br />

bigger <strong>and</strong> smaller<br />

For simplicity,<br />

Put into ascending order<br />

Beg (CS, UW) CS234 - <strong>Data</strong> <strong>Structures</strong> 18


Desirable Properties<br />

Comparisons of keys: O(n log n) worst <strong>ca</strong>se<br />

Swaps: O(n) worst <strong>ca</strong>se<br />

In place: Requires O(1) additional memory<br />

Stable: Equal keys are not reordered<br />

Special Case Performance ('adaptive'): Runtime<br />

approaches O(n) for nearly sorted data<br />

Beg (CS, UW) CS234 - <strong>Data</strong> <strong>Structures</strong> 19


A few basic sorts (to start)<br />

Bubble sort<br />

Selection sort<br />

Linear Insertion sort<br />

http://www.sorting-algorithms.com/<br />

http://www.<strong>cs</strong>.<strong>uwaterloo</strong>.<strong>ca</strong>/~bwbecker/sorting<br />

Demo/<br />

http://math.hws.edu/TMCM/java/xSortLab/<br />

Beg (CS, UW) CS234 - <strong>Data</strong> <strong>Structures</strong> 20

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

Saved successfully!

Ooh no, something went wrong!