-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Louis1992
committed
Jun 14, 2015
1 parent
a2b0aff
commit 83d8be0
Showing
18 changed files
with
346 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ | |
*.tex | ||
|
||
images/ | ||
Tools/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
### Exercises 2.2-1 | ||
*** | ||
Express the function ![](http://latex.codecogs.com/gif.latex?n^3/1000 - 100n^2 - 100n+3) | ||
in terms of Θ-notation. | ||
|
||
### `Answer` | ||
Θ(n3) | ||
|
||
|
||
### Exercises 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 Θ- notation. | ||
|
||
### `Answer` | ||
![pic](./repo/s2/1.png) | ||
|
||
时间都是Θ(![](http://latex.codecogs.com/gif.latex?n^2)) | ||
|
||
|
||
### Exercises 2.2-3 | ||
*** | ||
Consider linear search again (see Exercise 2.1-3). How many elements of the input sequence need to be checked on the average, assuming that the element being searched for is equally likely to be any element in the array? How about in the worst case? What are the average-case and worst-case running times of linear search in Θ-notation? Justify your answers. | ||
|
||
### `Answer` | ||
* 平均情况应该要查找n/2个元素 | ||
* 最坏情况是n个 | ||
|
||
所以都是Θ(n) | ||
|
||
|
||
### Exercises 2.2-4 | ||
*** | ||
How can we modify almost any algorithm to have a good best-case running time? | ||
|
||
### `Answer` | ||
投机取巧,往最好情况去想 | ||
|
||
|
||
|
||
*** | ||
Follow [@louis1992](https://github.com/gzc) on github to help finish this task. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/usr/bin/env python | ||
# coding=utf-8 | ||
|
||
def binarysearch(items, v): | ||
low = 0 | ||
high = len(items)-1; | ||
while low <= high: | ||
mid = (low+high)/2; | ||
if(items[mid] == v): | ||
return mid | ||
elif(items[mid] < v): | ||
low += 1 | ||
else: | ||
high -= 1 | ||
return None | ||
|
||
items = [1,2,3,4,5] | ||
print binarysearch(items, 3) | ||
print binarysearch(items, 6) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#!/usr/bin/env python | ||
# coding=utf-8 | ||
|
||
def merge(items, p, q, r): | ||
L = items[p:q+1] | ||
R = items[q+1:r+1] | ||
i = j = 0 | ||
k = p | ||
inversions = 0; | ||
while i < len(L) and j < len(R): | ||
if(L[i] < R[j]): | ||
items[k] = L[i] | ||
i += 1 | ||
else: | ||
items[k] = R[j] | ||
j += 1 | ||
inversions += (len(L) - i) | ||
k += 1 | ||
if(j == len(R)): | ||
items[k:r+1] = L[i:] | ||
return inversions | ||
|
||
|
||
|
||
def mergesort(items, p, r): | ||
inversions = 0; | ||
if(p < r): | ||
q = (p+r)/2 | ||
inversions += mergesort(items, p, q) | ||
inversions += mergesort(items, q+1, r) | ||
inversions += merge(items, p, q, r) | ||
return inversions | ||
|
||
|
||
items = [4,3,2,1,17] | ||
inversions = mergesort(items, 0, len(items)-1) | ||
print items,inversions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#!/usr/bin/env python | ||
# coding=utf-8 | ||
|
||
def merge(items, p, q, r): | ||
L = items[p:q+1] | ||
R = items[q+1:r+1] | ||
i = j = 0 | ||
k = p | ||
while i < len(L) and j < len(R): | ||
if(L[i] < R[j]): | ||
items[k] = L[i] | ||
i += 1 | ||
else: | ||
items[k] = R[j] | ||
j += 1 | ||
k += 1 | ||
if(j == len(R)): | ||
items[k:r+1] = L[i:] | ||
|
||
|
||
|
||
def mergesort(items, p, r): | ||
if(p < r): | ||
q = (p+r)/2 | ||
mergesort(items, p, q) | ||
mergesort(items, q+1, r) | ||
merge(items, p, q, r) | ||
|
||
|
||
items = [4,3,2,1,17] | ||
mergesort(items, 0, len(items)-1) | ||
print items |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters