第二章練習
2.1-2
View Code
1 publicstaticvoid insertionSort(int[] A) {
2 int j;
3 int key;
4 int i;
5 for (j =1; j < A.length; ++j) {
6 key = A[j];
7 i = j -1;
8 while (i >=0&& A[i] < key) {
9 A[i +1] = A[i];
10 i = i -1;
11 }
12 A[i +1] = key;
13 }
14 }
2.1-3
View Code
1 publicstaticint find(int[] A, int v) {
2 for (int i =0; i < A.length; ++i) {
3 if (v == A[i]) {
4 return i;
5 }
6 }
7 return-1;
8 }
2.1-4
View Code
1 publicstaticboolean[] BiAdd(boolean[] A, boolean[] B) {
2 boolean[] mResult =null;
3 int i;
4 boolean mAcc =false;
5 if (A.length != B.length) {
6 returnnull;
7 }
8 mResult =newboolean[A.length +1];
9 for (i = mResult.length -1; i >0; --i) {
10 mResult[i] = B[i -1] ^ mAcc;
11 if (A[i -1]) {
12 mResult[i] =!mResult[i];
13 mAcc = B[i -1] | mAcc;
14 } else {
15 mAcc = B[i -1] & mAcc;
16 }
17 }
18 mResult[0] = mAcc;
19 return mResult;
20 }
2.2-1
Θ(n^3)
2.2-2
View Code
1 publicstaticvoid selectionSort(int[] A) {
2 int i;
3 int j;
4 int mCurrMin;
5 int mCurrMinIndex;
6 int mLastIndex;
7 int mLength;
8 int mAj;
9
10 mLength = A.length;
11 mLastIndex = mLength -1;
12
13 for (i =0; i < mLastIndex; ++i) {
14 mCurrMin = Integer.MAX_VALUE;
15 mCurrMinIndex =-1;
16 for (j = i; j < A.length; ++j) {
17 mAj = A[j];
18 if (mAj < mCurrMin) {
19 mCurrMin = mAj;
20 mCurrMinIndex = j;
21 }
22 }
23 A[mCurrMinIndex] = A[i];
24 A[i] = mCurrMin;
25 }
26 }
Loop invariant: min(A[i .. n-1])=A[i] ⋀ {a|a∈A}|begin-of-loop={a|a∈A'}|end-of-loop
Why only the first n - 1 elements: i=n-1: min(A[i .. n-1])=min(A[n-1 .. n-1])=min(A[n-1])=A[n-1]
Best-case: Θ(n^2); Worst-case: Θ(n^2)
2.2-3
How many elements to be checked on the average: n/2
How many for worst-case: n
Average-case: Θ(n); Worst-case: Θ(n)
2.3-2
View Code
publicstaticvoid merge(int[] A, int Left, int Middle, int Right) {
int mLeftLength;
int mRightLength;
int i;
int mCurrLeft;
int mCurrRight;
int[] mLeft;
int[] mRight;
int mLeftData;
int mRightData;
mLeftLength = Middle - Left +1;
mRightLength = Right - Middle;
mLeft =newint[mLeftLength];
mRight =newint[mRightLength];
for (i =0; i < mLeftLength; ++i) {
mLeft[i] = A[Left + i];
}
for (i =0; i < mRightLength; ++i) {
mRight[i] = A[Middle + i +1];
}
mCurrLeft =0;
mCurrRight =0;
i = Left;
do {
mLeftData = mLeft[mCurrLeft];
mRightData = mRight[mCurrRight];
if (mLeftData < mRightData) {
A[i] = mLeftData;
++mCurrLeft;
} else {
A[i] = mRightData;
++mCurrRight;
}
++i;
} while (mCurrLeft < mLeftLength && mCurrRight < mRightLength);
if (mCurrLeft < mLeftLength) {
while (mCurrLeft < mLeftLength) {
A[i] = mLeft[mCurrLeft];
++i;
++mCurrLeft;
}
} else {
while (mCurrRight < mRightLength) {
A[i] = mRight[mCurrRight];
++i;
++mCurrRight;
}
}
}
publicstaticvoid mergeSort(int[] A, int Left, int Right) {
int mMiddle;
if (Left < Right) {
mMiddle = (Left + Right) /2;
mergeSort(A, Left, mMiddle);
mergeSort(A, mMiddle +1, Right);
merge(A, Left, mMiddle, Right);
}
}
2.3-3
(k=1: n=2^k=2^1=2 => T(n)=T(2)=2*lg(2)=2) ⋀
((k=t: n=2^k=2^t => T(n)=T(2^t)=2^t*lg(2^t)=t*2^t)
=> (k=t+1: n=2^k=2^(t+1) => 2*T(2^(t+1)/2)+2^(t+1)=(t+1)*2^(t+1)=T(2^(t+1))=T(n))) => (T(n)=n*lg(n))