8
Assignment#1 Computer Science Q2. The origami is totally done and will be handled and discussed with the TA. Q3.

Assignment # 1 Computer Science_Mahmoud Khedr_201301069

Embed Size (px)

DESCRIPTION

g

Citation preview

Page 1: Assignment # 1 Computer Science_Mahmoud Khedr_201301069

Assignment#1 Computer Science

Q2. The origami is totally done and will be handled and discussed with the TA.

Q3.

Page 2: Assignment # 1 Computer Science_Mahmoud Khedr_201301069
Page 3: Assignment # 1 Computer Science_Mahmoud Khedr_201301069

Q4.

Page 4: Assignment # 1 Computer Science_Mahmoud Khedr_201301069
Page 5: Assignment # 1 Computer Science_Mahmoud Khedr_201301069

Q5.

def Fib (n)

if (n == 0)

return 0;

else if (n == 1)

return 1;

else

return fib(n - 1) + fib(n - 2);

Page 6: Assignment # 1 Computer Science_Mahmoud Khedr_201301069
Page 7: Assignment # 1 Computer Science_Mahmoud Khedr_201301069
Page 8: Assignment # 1 Computer Science_Mahmoud Khedr_201301069

Q6. " Bonus – Best case analysis in insertion sort "

As shown in Q3. A psoudocode for insertion sort

The difference in the best case analysis is that the program will NOT go through the while loop not at all as the array will be already sorted so the condition of ( A[wait -1]>original ) always fail .

So, T(n) would be = (c1 + c3 )*(n-1) which is time complexity of O(n).

Q7.

No.5 in the lab sheet:

Psoudocode:

Sum(n^2)

{

For i ← 1 to n

A[i] ← i * i ;

m ← length(A) ;

sum ← 0;

for i ← 1 to m

sum ← sum + A[i];

return sum;

}

No.8 in the lab sheet:

Psoudocode:

i ← 2 ;

while i > = 2

{ If i’s prime devisors are 2 OR 3 OR both

return i ;

}

** the code is not in the strict sense as it’s not terminating

Name : Mahmoud Mohamed Bahaa El Din Khedr

ID no. 201301069