diff --git a/Foundations/overview/README.md b/Foundations/overview/README.md index d26341e..4f86c83 100644 --- a/Foundations/overview/README.md +++ b/Foundations/overview/README.md @@ -115,10 +115,21 @@ appear in A. [unittest](insertion_sort_unittest.py) ### 2.1-4 -Consider the problem of adding two n-bit binary integers, stored in two n-element arrays A and B. The sum of the two integers should be stored in binary form in an (n+1)-element array `C`. State the problem formally and write pseudocode for adding the two integers. +> Consider the problem of adding two n-bit binary integers, stored in two n-element arrays A and B. The sum of the two integers should be stored in binary form in an (n+1)-element array `C`. State the problem formally and write pseudocode for adding the two integers. **Input**: Two n-bit binary integers, `A=` and `B=`. **Output**: An (n+1)-element array, `C=` which is the sum of the two input integers. [pseudocode](add-binary.pdf) | [code](insertion_sort.py) | [unittest](insertion_sort_unittest.py) + +### 2.2-1 +> Express the function `n^3/1000 - 100n^2 + 100n + 3` in terms of O-notation. + +![image](https://cloud.githubusercontent.com/assets/1147451/7592456/dd19381e-f905-11e4-8089-8ec444fca706.png) + +### 2.2-2 +> Consider sorting `n` numbers stored in array `A` by first finding the smallest element of `A` and exchanging it with the element in `A[1]`. Then find the second smallest element of `A`, and exchange it with `A[2]`􏰃. Continue in this manner for the first `n-1` elements of `A`. Write pseudocode for this algorithm, which is known as **selection sort**. What loop invariant does this algorithm maintain? Why does it need to run for only the first `n-1` elements, rather than for all `n` elements? Give the best-case and worst-case running times of selection sort in O-notation. + +[pseudocode](selection-sort.pdf) | [code](insertion_sort.py) | +[unittest](insertion_sort_unittest.py) diff --git a/Foundations/overview/insertion_sort.py b/Foundations/overview/insertion_sort.py index 8aff7d5..3f594b6 100644 --- a/Foundations/overview/insertion_sort.py +++ b/Foundations/overview/insertion_sort.py @@ -38,3 +38,13 @@ def add_binary(a_lst, b_lst): carry = (a_lst[j] + b_lst[j] + carry) // 2 c_lst[0] = carry return c_lst + + +def selection_sort(lst): + for j in range(0, len(lst)-1): + smallest = j + for i in range(j+1, len(lst)): + if lst[i] < lst[smallest]: + smallest = i + lst[j], lst[smallest] = lst[smallest], lst[j] + return lst diff --git a/Foundations/overview/insertion_sort_unittest.py b/Foundations/overview/insertion_sort_unittest.py index 8f8251d..1d9897e 100644 --- a/Foundations/overview/insertion_sort_unittest.py +++ b/Foundations/overview/insertion_sort_unittest.py @@ -24,5 +24,11 @@ class InsertionSortTest(unittest.TestCase): self.assertEqual(add_binary([0, 0, 1], [1, 1, 1]), [1, 0, 0, 0]) self.assertEqual(add_binary([1, 1, 1], [1, 1, 1]), [1, 1, 1, 0]) + def test_selection_sort(self): + self.assertEqual(selection_sort([5, 2, 4, 6, 1, 3]), [1, 2, 3, 4, 5, 6]) + self.assertEqual(selection_sort([5]), [5]) + self.assertEqual(selection_sort([]), []) + self.assertEqual(selection_sort([1, 5, 2, 1, 6]), [1, 1, 2, 5, 6]) + if __name__ == '__main__': unittest.main() diff --git a/Foundations/overview/selection-sort.pdf b/Foundations/overview/selection-sort.pdf new file mode 100644 index 0000000..f106e8f Binary files /dev/null and b/Foundations/overview/selection-sort.pdf differ diff --git a/Foundations/overview/selection-sort.tex b/Foundations/overview/selection-sort.tex new file mode 100644 index 0000000..899f0b4 --- /dev/null +++ b/Foundations/overview/selection-sort.tex @@ -0,0 +1,20 @@ +\documentclass{article} +\usepackage{clrscode3e} +\begin{document} + \title{2.2-2} + \author{pezy} + \maketitle + \begin{codebox} + \Procname{$\proc{Selection-Sort}(A)$} + \li \For $j \gets 1$ \To $\attrib{A}{length} - 1$ + \li \Do $min \gets j$ + \li \For $i \gets j + 1$ \To $\attrib{A}{length}$ + \li \Do \If $A[i] < A[min]$ + \li \Do $min \gets i$ + \End + \End + \li \func{swap}($A[min], A[j]$) + \End + \li \Return $A$ + \end{codebox} +\end{document} \ No newline at end of file