COSC 1P03 Data Structures and Abstraction 20.1
COSC 1P03 Data Structures and Abstraction 20.1
COSC 1P03 Data Structures and Abstraction 20.1
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<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