您的位置:首页 > 其它

算法导论习题答案(第二章)--自己整理

2011-02-01 16:32 393 查看
2.1-1

1.{31 41 59 26 41 58}

2.{31 41 59 26 41 58}

3.{31 41 59 26 41 58}

4.{26 31 41 59 41 58}

5.{26 31 41 41 59 58}

6.{26 31 41 41 58 59}

2.1-2

把”A[I]>key”中的大于号改为小于号。

#include <iostream>

#include <time.h>

using namespace std;

void InsertSort(int a[],int n)

{

int i,j;

int key;

for(j=2 ; j<=n ; j++)

{

key = a[j];

i = j-1;

while(i>0 && a[i]<key)

{

a[i+1] = a[i];

i--;

}

a[i+1] = key;

}

}

int main()

{

int a[99];

a[0] = -1;

srand(time(0));

for(int i=1 ; i<99 ; i++)

{

a[i] = rand()%999;

}

InsertSort(a,98);

for(int i=1 ; i<99 ; i++)

{

cout << a[i] << ' ';

}

return 0;

}

2.1-4

#include <iostream>

using namespace std;

void BinaryAdd(int a[],int b[],int sum[],int bits)

{

int i = bits;

int c = 0;//进位

int s;

while(i>0)

{

s=a[i]+b[i]+c;

if(s >= 2)

{

s -= 2;

c = 1;

}

sum[i] = s;

i--;

}

if(c == 1)

{

sum[0] = 1;

}

else

{

sum[0] = 0;

}

}

int main()

{

int a[6]={0,1,0,1,0,1},

b[6]={0,1,1,1,1,1};

int sum[6];

BinaryAdd(a,b,sum,5);

for(int i=0 ; i<6 ; i++)

{

cout << sum[i] << ' ';

}

return 0;

}

2.2-1

Θ(n3)

2,2-2

都是Θ(n2)

2.2-3

都是Θ(n)

2.2-4

输入数组为最好情况

2.3-2

#include <iostream>

#include <time.h>

#define N 100

using namespace std;

void Merge(int array[],int begin,int mid,int end)

{

int length1 = mid-begin+1,

length2 = end-mid;

int *tmpArray1 = new int[length1+1];

int *tmpArray2 = new int[length2+1];

int i,j;

for(i=0 ; i<length1 ; i++)

{

tmpArray1[i] = array[begin+i];

}

for(j=0 ; j<length2 ; j++)

{

tmpArray2[j] = array[mid+1+j];//

}

//tmpArray1[i] = tmpArray2[j] = 99999;

i = 0;

j = 0;

int k;

for(k=begin ; k<=end && i<length1 && j<length2; k++)

{

if(tmpArray1[i] < tmpArray2[j])

{

array[k] = tmpArray1[i];

i++;

}

else

{

array[k] = tmpArray2[j];

j++;

}

}

if(j>=length2)

{

for(;i<length1;i++,k++)

{

array[k] = tmpArray1[i];

}

}

else

{

for(;j<length2;j++,k++)

{

array[k] = tmpArray2[j];

}

}

}

void MergeSort(int array[],int begin,int end)

{

int mid;

if(begin < end)

{

mid = (begin+end)/2;

MergeSort(array,begin,mid);

MergeSort(array,mid+1,end);

Merge(array,begin,mid,end);

}

}

int main()

{

int a
;

srand( time(0) );

for(int i=0 ; i<N ; i++)

{

a[i] = rand()%100;

}

MergeSort(a,0,N-1);

for(int i=0 ; i<N ; i++)

{

cout << a[i] << ' ';

}

return 0;

}

2.3-3

T(2)=2T(n)+2n=2nlgn+2n=2n(lgn+1)=2nlg2n

2.3-4

#include <iostream>

#include <time.h>

using namespace std;

void InsertSort(int a[],int n)

{

if(n>1)

{

InsertSort(a,n-1);

int i,j;

j=n-1;

int key;

key = a[j];

i = j-1;

while(i>=0 && a[i]>key)

{

a[i+1] = a[i];

i--;

}

a[i+1] = key;

}

}

int main()

{

int a[5];

srand( time(0) );

for(int i=0 ; i<5 ; i++)

{

a[i] = rand()%100;

}

InsertSort(a,5);

for(int i=0 ; i<5 ; i++)

{

cout << a[i] << ' ';

}

return 0;

}

2.3-5

#include <iostream>

#include <time.h>

using namespace std;

void InsertSort(int a[],int n)

{

int i,j;

int key;

for(j=2 ; j<=n ; j++)

{

key = a[j];

i = j-1;

while(i>0 && a[i]>key)

{

a[i+1] = a[i];

i--;

}

a[i+1] = key;

}

}

int BinarySearch(int a[],int x,int begin,int end)

{

int mid;

while(begin<=end)

{

mid = (begin+end)/2;

if(a[mid] > x)

{

end = mid-1;

continue;

}

else if(a[mid] < x)

{

begin = mid+1;

continue;

}

else

{

return mid;

}

}

return 0;

}

int main()

{

int a[10];

srand( time(0) );

for(int i=1 ; i<10 ; i++)

{

a[i] = rand()%11;

}

for(int i=1 ; i<10 ; i++)

{

cout << a[i] << ' ';

}

InsertSort(a,9);

cout << endl;

for(int i=1 ; i<10 ; i++)

{

cout << a[i] << ' ';

}

cout << BinarySearch(a,5,1,9);

return 0;

}

2.3-6

不能,二分查找是针对已排好序的数组的。

2.3-7

#include <iostream>

#include <time.h>

using namespace std;

void InsertSort(int a[],int n)

{

int i,j;

int key;

for(j=2 ; j<=n ; j++)

{

key = a[j];

i = j-1;

while(i>0 && a[i]>key)

{

a[i+1] = a[i];

i--;

}

a[i+1] = key;

}

}

int BinarySearch(int a[],int x,int begin,int end)

{

int mid;

while(begin<=end)

{

mid = (begin+end)/2;

if(a[mid] > x)

{

end = mid-1;

continue;

}

else if(a[mid] < x)

{

begin = mid+1;

continue;

}

else

{

return mid;

}

}

return 0;

}

int main()

{

int a[10];

srand( time(0) );

for(int i=1 ; i<10 ; i++)

{

a[i] = rand()%11;

}

for(int i=1 ; i<10 ; i++)

{

cout << a[i] << ' ';

}

InsertSort(a,9);

cout << endl;

for(int i=1 ; i<10 ; i++)

{

cout << a[i] << ' ';

}

int x;

cout << "input x:";

cin >> x;

bool f;

for(int i=1 ; i<10 ; i++)

{

if(BinarySearch(a,x-a[i],i+1,9))

{

f=true;

break;

}

else

{

f=false;

}

}

if(f == true)

{

cout<<"Exist.";

}

else

{

cout << "Not exist.";

}

return 0;

}

Problems2-1

a.n/k *Θ(k2) = (nk)

b.树的高度是lg(n/k),每一级需要cn,所以cn+cnlg(n/k)=Θ(nlg(n/k))。

c.要使Θ(nk+nlg(n/k)=Θ(nlgn),k一定不大于lgn,否则等式不成立。将k=lgn带入左式,得到(n lg n + n lg n −n lg lg n) = (2n lg n −n lg lg n)=Θ(nlgn)。

d.k只要小于插入排序优于归并排序的临界值即可。

Problems 2-2

a.元素不变

b. Loop invariant: At the start of each iteration of the for loop of lines 2.4,

A[ j ] = min {A[k] : j ≤ k ≤ n} and the subarray A[ j . . n] is a permutation

of the values that were in A[ j . . n] at the time that the loop started.

Initialization: Initially, j = n, and the subarray A[ j . . n] consists of single

element A[n]. The loop invariant trivially holds.

Maintenance: Consider an iteration for a given value of j . By the loop invariant,

A[ j ] is the smallest value in A[ j . . n]. Lines 3.4 exchange A[ j ]

and A[ j − 1] if A[ j ] is less than A[ j − 1], and so A[ j − 1] will be the

smallest value in A[ j − 1 . . n] afterward. Since the only change to the subarray

A[ j − 1 . . n] is this possible exchange, and the subarray A[ j . . n] is

a permutation of the values that were in A[ j . . n] at the time that the loop

started, we see that A[ j − 1 . . n] is a permutation of the values that were in

A[ j − 1 . . n] at the time that the loop started. Decrementing j for the next

iteration maintains the invariant.

Termination: The loop terminates when j reaches i . By the statement of the

loop invariant, A[i ] = min{A[k] : i ≤ k ≤ n} and A[i . . n] is a permutation

of the values that were in A[i . . n] at the time that the loop started.

c. Loop invariant: At the start of each iteration of the for loop of lines 1.4,

the subarray A[1 . . i −1] consists of the i −1 smallest values originally in

A[1 . . n], in sorted order, and A[i . . n] consists of the n −i +1 remaining

values originally in A[1 . . n].

Initialization: Before the Þrst iteration of the loop, i = 1. The subarray

A[1 . . i − 1] is empty, and so the loop invariant vacuously holds.

Maintenance: Consider an iteration for a given value of i . By the loop invariant,

A[1 . . i −1] consists of the i smallest values in A[1 . . n], in sorted order.

Part (b) showed that after executing the for loop of lines 2.4, A[i ] is the

smallest value in A[i . . n], and so A[1 . . i ] is now the i smallest values originally

in A[1 . . n], in sorted order. Moreover, since the for loop of lines 2.4

permutes A[i . . n], the subarray A[i +1 . . n] consists of the n −i remaining

values originally in A[1 . . n].

Termination: The for loop of lines 1.4 terminates when i = n + 1, so that

i − 1 = n. By the statement of the loop invariant, A[1 . . i − 1] is the entire

array A[1 . . n], and it consists of the original array A[1 . . n], in sorted order.

Note: We have received requests to change the upper bound of the outer for

loop of lines 1.4 to length[A] − 1. That change would also result in a correct

algorithm. The loop would terminate when i = n, so that according to the loop

invariant, A[1 . . n − 1] would consist of the n − 1 smallest values originally

in A[1 . . n], in sorted order, and A[n] would contain the remaining element,

which must be the largest in A[1 . . n]. Therefore, A[1 . . n] would be sorted.

In the original pseudocode, the last iteration of the outer for loop results in no

iterations of the inner for loop of lines 1.4. With the upper bound for i set to

length[A]−1, the last iteration of outer loop would result in one iteration of the

inner loop. Either bound, length[A] or length[A]−1, yields a correct algorithm.

d.(n-1)+(n-2)+…+2+1=n(n-1)/2=Θ(n2),与InsertSort的复杂度是相等的。

(b,c是拷贝的标准答案。)

Problems 2-3

a. Θ(n)

b. 1+2+3+…+n=(n+1)/2=Θ(n2),乘法运算次数明显多于Horner’s rule。

Problems 2-4

a.(2,1) (3,1) (8,6) (8,1) (3,1)

b.(n-1)*n/2

c,inversions越多,花费时间越多。

d. COUNT-INVERSIONS(A, p, r )

inversions ← 0

if p < r

then q ← (p + r)/2

inversions ← inversions +COUNT-INVERSIONS(A, p, q)

inversions ← inversions +COUNT-INVERSIONS(A, q + 1, r )

inversions ← inversions +MERGE-INVERSIONS(A, p, q, r )

return inversions

MERGE-INVERSIONS(A, p, q, r )

n1 ← q − p + 1

n2 ←r − q

create arrays L[1 . . n1 + 1] and R[1 . . n2 + 1]

for i ← 1 to n1

do L[i ] ← A[p + i − 1]

for j ← 1 to n2

do R[ j ] ← A[q + j ]

L[n1 + 1]←∞

R[n2 + 1]←∞

i ← 1

j ← 1

inversions ← 0

counted ← FALSE

for k ← p to r

do if counted = FALSE and R[ j ] < L[i ]

then inversions ← inversions +n1 − i + 1

counted ← TRUE

if L[i ] ≤ R[ j ]

then A[k] ← L[i ]

i ← i + 1

else A[k] ← R[ j ]

j ← j + 1

counted ← FALSE

return inversions
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: