slide errors
parent
e72dc5d23a
commit
943d34964c
|
@ -5,6 +5,10 @@ date: Sept 21, 2022
|
|||
textbook: Ch. 15
|
||||
---
|
||||
|
||||
<section>
|
||||
<p>Warm up...</p>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<h4 class="slide_title">Fibonacci</h4>
|
||||
|
||||
|
@ -24,7 +28,7 @@ textbook: Ch. 15
|
|||
T(n-1) + T(n-2) + \Theta(1) & \textbf{otherwise}
|
||||
\end{cases}$$
|
||||
|
||||
<p>Solve for $T(n)$</p>
|
||||
<p>Test Hypothesis: $T(n) \in O(2^n)$</p>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
|
@ -60,6 +64,40 @@ textbook: Ch. 15
|
|||
<p><b>Output:</b> An array with elements in sorted order.</p>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<h4 class="slide_title">Merge Sort</h4>
|
||||
|
||||
<svg data-src="graphics/11b/merge_example.svg"/>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<h4 class="slide_title">Merge Sort</h4>
|
||||
|
||||
<p style="font-size: 90%;"><b>Observation:</b> Merging two sorted arrays can be done in $O(n)$.</p>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<h4 class="slide_title">Merge Sort</h4>
|
||||
|
||||
<pre><code class="scala">
|
||||
def merge[A: Ordering](left: Seq[A], right: Seq[A]): Seq[A] = {
|
||||
val output = ArrayBuffer[A]()
|
||||
|
||||
val leftItems = left.iterator.buffered
|
||||
val rightItems = right.iterator.buffered
|
||||
|
||||
while(leftItems.hasNext || rightItems.hasNext) {
|
||||
if(!left.hasNext) { output.append(right.next) }
|
||||
else if(!right.hasNext) { output.append(left.next) }
|
||||
else if(Ordering[A].lt( left.head, right.head ))
|
||||
{ output.append(left.next) }
|
||||
else { output.append(right.next) }
|
||||
}
|
||||
output.toSeq
|
||||
}
|
||||
</code></pre>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<h4 class="slide_title">Merge Sort</h4>
|
||||
|
||||
|
@ -110,34 +148,6 @@ textbook: Ch. 15
|
|||
</ol>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<h4 class="slide_title">Merge Sort</h4>
|
||||
|
||||
<svg data-src="graphics/11b/merge_example.svg"/>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<h4 class="slide_title">Merge Sort</h4>
|
||||
|
||||
<pre><code class="scala">
|
||||
def merge[A: Ordering](left: Seq[A], right: Seq[A]): Seq[A] = {
|
||||
val output = ArrayBuffer[A]()
|
||||
|
||||
val leftItems = left.iterator.buffered
|
||||
val rightItems = right.iterator.buffered
|
||||
|
||||
while(leftItems.hasNext || rightItems.hasNext) {
|
||||
if(!left.hasNext) { output.append(right.next) }
|
||||
else if(!right.hasNext) { output.append(left.next) }
|
||||
else if(Ordering[A].lt( left.head, right.head ))
|
||||
{ output.append(left.next) }
|
||||
else { output.append(right.next) }
|
||||
}
|
||||
output.toSeq
|
||||
}
|
||||
</code></pre>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<h4 class="slide_title">Merge Sort</h4>
|
||||
|
||||
|
|
Loading…
Reference in New Issue