18.10.2014 Views

COSC 1P03 Data Structures and Abstraction 20.1

COSC 1P03 Data Structures and Abstraction 20.1

COSC 1P03 Data Structures and Abstraction 20.1

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.

<strong>COSC</strong> <strong>1P03</strong><br />

<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong><br />

<strong>COSC</strong> <strong>1P03</strong><br />

Ch 20<br />

Searching & Sorting<br />

<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong> <strong>20.1</strong><br />

<strong>COSC</strong> <strong>1P03</strong><br />

Searching<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

Locating one record (object) from a collection<br />

Search key<br />

unique vs duplicate<br />

Outcomes<br />

found/not found<br />

Simple vs exhaustive search<br />

Internal vs external<br />

Order<br />

significant step<br />

probe (checking key)<br />

simple O(n)<br />

best O(lg n)<br />

Key comparison search<br />

<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong> 20.2<br />

loop<br />

determine point of next probe<br />

if no more choices then exit not found<br />

access item<br />

if search key matches key on item then exit found<br />

end<br />

Figure <strong>20.1</strong> Algorithm for key comparison search<br />

<strong>20.1</strong>


<strong>COSC</strong> <strong>1P03</strong><br />

<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong><br />

<strong>COSC</strong> <strong>1P03</strong><br />

Sequential (Linear) Search<br />

<br />

<br />

<br />

<br />

<br />

Principle<br />

probe in order<br />

Algorithm<br />

as variation of basic search<br />

Simple vs exhaustive<br />

Analysis<br />

depends on where element is found<br />

average performance<br />

each probe eliminates 1 entry<br />

O(n)<br />

E.g. view student<br />

<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong> 20.4<br />

start at first item;<br />

for ( ; ; ) {<br />

if ( no more items ) { not found; break; };<br />

access item<br />

if ( search key matches key on item ) { found; break; };<br />

on to next item;<br />

};<br />

Figure 20.2 Sequential search algorithm<br />

minimum O(1)<br />

maximum O(n)<br />

average<br />

O(n)<br />

Table <strong>20.1</strong> Order of sequential search<br />

20.2


<strong>COSC</strong> <strong>1P03</strong><br />

<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong><br />

<strong>COSC</strong> <strong>1P03</strong><br />

Binary Search<br />

<br />

<br />

<br />

<br />

<br />

<br />

Large collections<br />

e.g. telephone book<br />

Requires - sorted by key<br />

Principle<br />

distribution of keys?<br />

probe at midpoint<br />

Algorithm<br />

probing<br />

bounds<br />

termination<br />

Analysis<br />

worst case<br />

each probe eliminates 1/2 remaining entries<br />

O(log n)<br />

E.g. view students<br />

<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong> 20.7<br />

start with bounds being first <strong>and</strong> last items;<br />

while ( true ) {<br />

if ( bounds empty ) { not found; break; };<br />

access middle item between bounds<br />

if ( search key matches key on item ) { found; break; };<br />

if ( search key greater than key on item ) {<br />

set lower bound to next item<br />

}<br />

else {<br />

set upper bound to prior item<br />

};<br />

};<br />

Figure 20.4 Binary search algorithm<br />

lower<br />

bound<br />

new upper<br />

bound<br />

upper<br />

bound<br />

probe<br />

Figure 20.5 Probe in binary search<br />

20.3


<strong>COSC</strong> <strong>1P03</strong><br />

<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong><br />

minimum O(1)<br />

maximum O(lg n)<br />

average O(lg n)<br />

Table 20.2 Order of Binary search<br />

<strong>COSC</strong> <strong>1P03</strong><br />

Comparison<br />

<br />

<br />

<br />

<br />

<br />

Orders<br />

O(n) vs O(lg n)<br />

Requirements<br />

unsorted vs sorted<br />

Complexity of code<br />

programmer time<br />

maintenance<br />

Size of n<br />

n32<br />

Comparison statistics<br />

<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong> <strong>20.1</strong>1<br />

Order Time (ms)<br />

sequential search O(n) 5,320<br />

binary search O(lg n) 50<br />

Table 20.3 Comparison of search algorithms<br />

20.4


<strong>COSC</strong> <strong>1P03</strong><br />

<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong><br />

<strong>COSC</strong> <strong>1P03</strong><br />

Sorting<br />

<br />

<br />

<br />

<br />

<br />

<br />

Reports<br />

in name or id number order<br />

Ordering by key<br />

subsorts<br />

eg e.g. by course within department<br />

Sort key<br />

duplicate keys<br />

Internal vs external sorting<br />

In situ<br />

Stable<br />

depends on implementation<br />

required for subsorts<br />

<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong> <strong>20.1</strong>3<br />

<strong>COSC</strong> <strong>1P03</strong><br />

Analysis<br />

<br />

<br />

<br />

Order<br />

key comparison<br />

simple O(n 2 )<br />

best O(n lg n)<br />

Simple<br />

pass<br />

sorted & unsorted segments<br />

Better<br />

divide-<strong>and</strong>-conquer<br />

<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong> <strong>20.1</strong>4<br />

sorted<br />

unsorted<br />

pass<br />

Figure 20.7 Pass in sorting<br />

20.5


<strong>COSC</strong> <strong>1P03</strong><br />

<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong><br />

<strong>COSC</strong> <strong>1P03</strong><br />

Selection Sort<br />

<br />

<br />

<br />

<br />

<br />

<br />

Principle<br />

select smallest (of remaining) <strong>and</strong> put it next<br />

Behavior<br />

last pass<br />

Algorithm<br />

find minimum (maximum)<br />

exchange<br />

Analysis<br />

O(n) comparisons each pass<br />

O(n) passes<br />

O(n 2 )<br />

E.g. mark list by grade<br />

In situ, stable<br />

<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong> <strong>20.1</strong>6<br />

pass 1<br />

3 5 2 7 1 6 8 4<br />

pass 5<br />

1 2 3 4 5 6 8 7<br />

pass 2<br />

1 5 2 7 3 6 8 4<br />

pass 6<br />

1 2 3 4 5 6 8 7<br />

pass 3<br />

1 2 5 7 3 6 8 4<br />

pass 7<br />

1 2 3 4 5 6 8 7<br />

pass 4 1 2 3 7 5 6 8 4<br />

Figure 20.8 Selection sort<br />

pass 8<br />

1 2 3 4 5 6 7 8<br />

<strong>COSC</strong> <strong>1P03</strong><br />

Insertion Sort<br />

<br />

<br />

<br />

<br />

<br />

Principle<br />

insert next entry (from unsorted) at correct point in sorted<br />

segment<br />

Behaviour<br />

first pass<br />

Analysis<br />

O(n) passes<br />

ave O(n) comparisons per pass<br />

O(n 2 )<br />

E.g. mark list by grade<br />

shifting elements<br />

In situ, stable<br />

<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong> <strong>20.1</strong>8<br />

20.6


<strong>COSC</strong> <strong>1P03</strong><br />

<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong><br />

pass 1<br />

3 5 2 7 1 6 8 4<br />

pass 5<br />

3 5 2 7 1 4 6 8<br />

pass 2<br />

3 5 2 7 1 6 8 4<br />

pass 6<br />

3 5 2 1 4 6 7 8<br />

pass 3<br />

3 5 2 7 1 6 4 8<br />

pass 7<br />

3 5 1 2 4 6 7 8<br />

pass 4 3 5 2 7 1 4 6 8<br />

Figure <strong>20.1</strong>1 Insertion sort<br />

pass 8<br />

3 1 2 4 5 6 7 8<br />

<strong>COSC</strong> <strong>1P03</strong><br />

<br />

<br />

<br />

<br />

<br />

Exchange (Bubble) Sort<br />

Principle<br />

exchange pairs if out of order<br />

works?<br />

quit?<br />

Pehaviour<br />

largest “bubbles” to end<br />

next pass doesn’t have to go to end<br />

Analysis<br />

each pass moves 1 item to correct spot<br />

O(n) passes<br />

ave O(n) pairs<br />

O(n 2 )<br />

early termination<br />

no exchanges<br />

O(n) if sorted<br />

E.g. mark list in array<br />

In situ, stable<br />

<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong> 20.20<br />

pass 1<br />

3 5 2 7 1 6 8 4<br />

3 5 2 7 1 6 8 4<br />

pass 3<br />

2 3 1 5 6 4 7 8<br />

2 3 1 5 6 4 7 8<br />

3 2 5 7 1 6 8 4<br />

2 1 3 5 6 4 7 8<br />

3 2 5 7 1 6 8 4<br />

2 1 3 5 6 4 7 8<br />

3 2 5 1 7 6 8 4<br />

2 1 3 5 6 4 7 8<br />

3 2 5 1 6 7 8 4<br />

3 2 5 1 6 7 8 4<br />

pass 4<br />

2 1 3 5 4 6 7 8<br />

1 2 3 5 4 6 7 8<br />

pass 2<br />

3 2 5 1 6 7 4 8<br />

1 2 3 5 4 6 7 8<br />

2 3 5 1 6 7 4 8<br />

1 2 3 5 4 6 7 8<br />

2 3 5 1 6 7 4 8<br />

2 3 1 5 6 7 4 8<br />

2 3 1 5 6 7 4 8<br />

2 3 1 5 6 7 4 8<br />

pass 5<br />

pass 6<br />

1 2 3 4 5 6 7 8<br />

1 2 3 4 5 6 7 8<br />

1 2 3 4 5 6 7 8<br />

1 2 3 4 5 6 7 8<br />

1 2 3 4 5 6 7 8<br />

Figure <strong>20.1</strong>4 Exchange sort<br />

pass 7<br />

1 2 3 4 5 6 7 8<br />

20.7


<strong>COSC</strong> <strong>1P03</strong><br />

<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong><br />

<strong>COSC</strong> <strong>1P03</strong><br />

Merge Sort<br />

<br />

<br />

<br />

<br />

Divide-<strong>and</strong>-conquer<br />

principle<br />

partition into two sets, sort & combine<br />

Merge sort<br />

partition<br />

break into two halves<br />

combine<br />

merge sorted partitions<br />

Behavior<br />

Analysis<br />

passes: O(lg n)<br />

partition: O(n)<br />

merge: O(n)<br />

complete: O(n log n)<br />

<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong> 20.22<br />

pass 1<br />

3 5 2 7 1 6 8 4<br />

pass 2 3 5 2 7<br />

1 6 8 4<br />

2 7 8 4<br />

pass 4<br />

3<br />

pass 3 3 5 1 6 4<br />

5 2 7 1<br />

6<br />

8<br />

4<br />

pass 3<br />

3 5<br />

2 7 1 6 4 8<br />

pass 2 2 3 5 7 1 4 6 8<br />

pass 1<br />

1 2 3 4 5 6 7 8<br />

<strong>COSC</strong> <strong>1P03</strong><br />

Implementation<br />

<br />

<br />

<br />

<br />

<br />

<br />

Recursive<br />

trivial case: n=0, n=1<br />

reduction: n to 2 @ n/2<br />

Partition<br />

create two new arrays: left <strong>and</strong> left<br />

copy first half to left <strong>and</strong> second half to right<br />

Sort<br />

recursive<br />

Combine<br />

merge from left <strong>and</strong> right into original array<br />

special case, one side used up<br />

E.g. mark list by grade<br />

Stable, not in situ<br />

<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong> 20.24<br />

20.8


<strong>COSC</strong> <strong>1P03</strong><br />

<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong><br />

Order<br />

Time<br />

(ms)<br />

selection sort O(n 2 ) 4,610<br />

insertion sort O(n 2 ) 2,480<br />

exchange sort O(n 2 ) 9,120<br />

merge sort O(n lg n)

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

Saved successfully!

Ooh no, something went wrong!