​Reference Document: HW1.pdf:

Question 1

  • Merge Sort =

  • Insertion Sort =

The values of n which can beat merge sort should satisfy the following inequality:

That is all values of n < 43.5

Question 2

  • Algo 1 =

  • Algo 2 =

The smallest value of n that satisfies the following equation would be our answer:

That is the smallest value of 14.4

Question 3

(a)

int s, i ,n; 			---- 1
cin >> n; 				---- 1
s = 0; 					---- 1
for(i=n; i>=1; i--)		---- n + 1
	s++;  				---- n * 1

Total = 1 + 1 + 1 + (n + 1) + n
T(n) = 2n + 4
O(n)

  • T(n) c f(n)

      • Let n = 1
      • 2 + 4 c
      • 6 c, therefore, c needs to be greater than or equal to 6 for every n >= 1
      • That is for n >= n0 where n0 = 1
      • This condition satisfies for all values of n >= 1
    • Hence, we have proved that T(n) is O(n)

(b)

int sum, i, j, n;        	------- 1 
sum = 0;					------- 1
cin >> n;					------- 1
for(i=1; i<n; i=i*2)		------- log2(n)
	for(j=1; j<n; j=j*2)	------- log2(n)-1 * log2(n)
		sum++;				-------	log2(n)-1 * log2(n)-1 * 1

Total = 1 + 1 + 1 + log(n) + log(n) * (log(n) - 1) + (log(n) - 1) * (log(n) - 1) * 1
= 3 + log(n) + log(n)2 - log(n) + (log(n) - 1)2
= 3 + log(n)2 + log(n)2 + 1 - 2log(n)
T(n) = 4 + 2*log(n)2 - 2log(n)
O(log(n)2)

  • T(n) c f(n)

    • 4 + 2log2(n) -2log(n) c log2(n)

      • Let n = 2
      • Therefore for n >= 2, c should be >= 4
      • For every n >= n0 where n0 = 2

      • Therefore, 4+2log2(n) is O(log2(n))

(c)

int x = 0, j=n;   ----- 1
while(j>0) {	  ----- log4(n) + 1
	x += j*3;	  ----- log4(n) * 1
	j /= 4;		  ----- log4(n) * 1
}

Total = 1 + log4(n) + 1 + log4(n) + log4(n)
T(n) = 2 + 3*log4(n)
O(log4(n))

  • T(n) c f(n)

    • 2 + 3log4(n) c log4(n)

      • Let n = 4

      • Therefore c should be at least >= 5 for every n >= 4

          • For every n >= n0 where n0 = 4
    • Hence, 2 + 3log4(n) is Big Oh of log4n

Question 4

PseudoCode

for(int i=0; i<n-1; i++){
	int x = i
	for(int j=i+1; j<n; j++){
		if (A[x] > A[j]){
			x = j
		}
	}
	if (i != x){
		int temp = A[i]
		A[i] = A[x]
		A[x] = temp
	}
}

Why is it running for only n-1 times?

The outer loop iterator is being used to set the values of indexes respectively by swapping with the smaller elements. The inner loop needs to run n​ times to compare with each value but as for the outer loop, since the time, it had reached nth value, that nth value must already be in correct position by the swapping done in earlier iterations. Hence, there should not be a need to iterate over the nth time.

Best CaseWorst Case
ø(n2)ø(n2)

Question 5

Prove that is Θ

We need to prove that:

Let n = 5

c1 should have a value equal or less than for n >= 5
Also, c2 should have a value greater than for n >= 5.
Since will eventually get large and the value will get positive thus we will keep a positive value as c2 for our upper bound

Thus let c1 = and c2 =

For every n >= n0 where n0 = 5

Hence ** ** is Θ****

Question 6

Function Mystery (n)
{
	If (n > 1)
	{
		Print “hello”
		Mystery(n/5)
		For (i=1 .... n)
			Print “world”
		Mystery(2n/5)
	}
}

The equation for this recursive function would be:

Recursion Tree Q5

We can observe that at each level there are n count of instructions being executed
where k is the level count

The following series can be observed:

Where is the height of the right most branch of the tree
(the longest branch, as being divided by each time)

To Solve This:

By applying summation formula:

Hence, we have as our runtime for this algorithm

Question 7

(a)

Q1

Solution

WhatsApp Image 2023-09-13 at 11.49.34 PM

(b)

Q2

Solution

2

(c)

Q3

Solution

3

(d)

Q4

Solution

4

(e)

WhatsApp Image 2023-09-13 at 11.17.21 PM

Solution

5

Question 8

Inversion Pairs Algorithm

WhatsApp Image 2023-09-13 at 9.07.20 PM

{} Curly Braces represent the return value, which is an array in our case, of each recursive call
The value followed by it is the count of the split inversion pairs

The given array has a total count of inversion pairs of 30