# CMPS 323 Design And Analysis Of Algorithms Assessment 1 Answer Pages: 4 Words: 890

## Question :

Q1. [10 pts] Designing an Algorithm – Smallest and Largest Numbers

Write an algorithm that finds both the smallest and largest numbers in a list of n numbers. Try to find a method that does at most 1.5n comparisons of array elements in total.

Q2. [20 pts] Designing and Implementing an Algorithm – Subsets of a set of n elements

1. [8 pts] Write an algorithm that prints all the subsets of three elements of a set of n elements (integers). The input to the algorithm is an array of integers (the elements of the set). The output is a list of all the subsets of three element. (Note that the efficiency of your algorithm will be considered in grading.)
2. [5 pts] Identify a basic operation for your algorithm and study the performance of your algorithm by giving its time complexity function. If it has an every-case time complexity, determine it. Otherwise, determine the worst-case time complexity.
3. [2 pts] Identify the complexity category to which the algorithm belongs.
4. [5 pts] Implement your algorithm using Java. Write a main method that asks the user to enter the size and elements of a set of integers, stores them in an array and then prints all the subsets of three-elements.

Q3. [20 pts] Designing and Implementing an Algorithm – Sum of three elements

1. [8 pts] Write an algorithm that determines whether there are three elements in a given array of integers that sums to exactly a given integer value k. As input, you are given an array of n integers and an integer value k. (Note that the efficiency of your algorithm will be considered in grading).

For example, given the array [1, 3, 7, 4] and k = 14, the answer is “yes”, given k = 6 the answer is “no.”

1. [5 pts] Identify a basic operation for your algorithm and study the performance of your algorithm by giving its time complexity function. If it has an every-case time complexity, determine it. Otherwise, determine the worst-case time complexity.
2. [2 pts] Identify the complexity category to which the algorithm belongs.
3. [5 pts] Implement your algorithm using Java. Write a main method that asks the user to enter the integer value k and the size n of an array of integers. The main method must generate the array elements randomly.

As output, the program must first print all elements of the array. Then, it should print “No” if the array does not contain three elements that sums to k or print “Yes” if there are three elements that sums to k. If the output is “Yes”, the program must print these three elements and their location index in the array.

Q4. [20 pts] Designing and Implementing an Algorithm – Longest increasing sequence

1. [8 pts] Write an algorithm to find a subsequence of a given sequence of integers in which the subsequence's elements are in sorted order, lowest to highest, and in which the subsequence is as long as possible. This subsequence is not necessarily contiguous, or unique. The algorithm should print one of the existing longest subsequences. (Note that the efficiency of your algorithm will be considered in grading.)

For example, in the following sequence of integer values: 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15

A longest increasing subsequence is 0, 2, 6, 9, 11, 15.

This subsequence has length six; the input sequence has no seven-member increasing subsequences. The longest increasing subsequence in this example is not the only solution: for instance 0, 4, 6, 9, 11, 15; 0, 2, 6, 9, 13, 15 ; and 0, 4, 6, 9, 13, 15 are other increasing subsequences of equal length in the same input sequence.

1. [5 pts] Identify a basic operation for your algorithm and study the performance of your algorithm by giving its time complexity function. If it has an every-case time complexity, determine it. Otherwise, determine the worst-case time complexity.
2. [2 pts] Identify the complexity category to which the algorithm belongs.
3. [5 pts] Implement your algorithm using Java. Write a main method that asks the user to enter the integer value k and the size n of an array of integers. The main method must generate the array elements randomly.

As output, the program must first print all elements of the original input sequence, then, it should print the length of the longest subsequence, and finally the elements of one of the longest subsequence.

Q5. [10 pts] Analysis of Algorithms

1. Consider the following algorithm:

for ( i = 1 ; i <= 1.5 n ; i++) print i ;

for ( i = n ; i >= 1 ; i - - print i ;

1. [1 pts] What is the output when n = 2, n = 4, and n = 6?
2. [4 pts] What is the time complexity of the above algorithm? You may assume that the input n is divisible by 2.
3. Consider the following algorithm: assume k <= n

for ( i = 1 ; i <= n ; i++)

for ( j = 1 ; j <= k ; j++ ) print i+j ;

1. [1 pts] What is the output when n = 2 and k = 1, and when n = 2 and k = 2?
2. [4 pts] What is the time complexity of the above algorithm.
3. Q6. [10 pts] Order of Growth

Using the definitions of O and Ω, show that

• [5 pts]6n2 + 20n Î O(n3),
• [5 pts]but: 6n2 + 20n Ï  (n3).

# Q7. [10 pts] Grouping Functions by Complexity Category

[6 pts] Group the following functions by complexity category and sort the identified categories in decreasing complexity order by giving enough justifications to your solution:

8n + 12, n3/2, n, 2n, n1/100, n5, 5n2 + 7n, log (n!), n!, n2log n, n2, 2n!, n2+ n2 log n, (log n)2

[2 pts] The function f(x) = (log n)2 + 2n + 4n + log n + 50 belongs in which of the following complexity categories: [2 pts] The function f(x) = f(x) = n + n2 + 2n + n4 belongs in which of the following complexity categories: 