Added 2.2-1, 2.2-2

1. added 2.2-1
2. part of 2.2-2(pseudocode and code)
This commit is contained in:
pezy_mbp
2015-05-13 00:55:34 +08:00
parent 8b472fe019
commit b070883ceb
5 changed files with 48 additions and 1 deletions

View File

@@ -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=<a1,...,an>` and `B=<b1,...,bn>`.
**Output**: An (n+1)-element array, `C=<c1,...,cn>` 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)

View File

@@ -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

View File

@@ -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()

Binary file not shown.

View File

@@ -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}