Data Structures: Final Exam

¡Supera tus tareas y exámenes ahora con Quizwiz!

An algorithm is a _________ set of precise instructions for performing computation.

Finite Explanation: By the definition of algorithm.

If an optimal solution can be created for a problem by constructing optimal solutions for its subproblems, the problem possesses ____________ property.

Optimal substructure. Explanation: Optimal substructure is the property in which an optimal solution is found for the problem by constructing optimal solutions for the subproblems.

Which of the following is not true about the 2-3 tree?

post order traversal yields elements in sorted order. Explanation: In a 2-3 tree, leaves are at the same level. And 2-3 trees are perfectly balanced as every path from root node to the null link is of equal length. In 2-3 tree in-order traversal yields elements in sorted order.

Which algorithm is better for sorting between bubble sort and quicksort?

quick sort Explanation: Running time of quicksort is logarithmic whereas for bubble sort it is quadratic.

A Complexity of algorithm depends upon:

Both Time and Space Explanation: For Complexity we calculate both time and space consumed.

An algorithm which uses the past results and uses them to find the new results is

Dynamic programming algorithms Explanation: In Dynamic programming algorithms we utilizes previous results for new ones.

A greedy algorithm can be used to solve all the dynamic programming problems.

False Explanation: A greedy algorithm gives optimal solution for all subproblems, but when these locally optimal solutions are combined it may NOT result into a globally optimal solution. Hence, a greedy algorithm CANNOT be used to solve all the dynamic programming problems.

Consider a situation where swap operation is very costly. Which of the following sorting algorithms should be preferred so that the number of swap operations are minimized in general?

Selection Sort

A selection sort algorithm sorts the list by which method?

Finding the smallest element in the list and moving this to the beginning of the unsorted list

Which of the following problems is NOT solved using dynamic programming?

Fractional knapsack problem. Explanation: The fractional knapsack problem is solved using a greedy algorithm.

The big-omega notation for f(x, y) = x^5 y^3 + x^4 y^4 + x^3 y^5 is

x^3 y^3 Explanation: x^5y^3 , x^4y^4 and x^3y^5 is greater than or equal to x^3y^3.

Which search algorithm is best for a large list?

Binary search

A binary search algorithm can be best described as what?

A divide and conquer technique.

Merge sort uses

Divide-and-conquer

Which algorithm would work best to sort data as it arrives, one piece at a time, perhaps from a network?

Insertion sort

What is the disadvantage of selection sort?

It is not scalable. Explanation: As the input size increases, the performance of selection sort decreases.

What operation does the Insertion Sort use to move numbers from the unsorted section to the sorted section of the list?

Swapping

The purpose of the bubble sorting algorithm is what?

To speed up the search of an item in the list To sort the contents of the list ANSWER: Both choices above

Merge sort works on the principle of divide-and-conquer

True

For a recursive algorithm:

a base case is necessary and is solved without recursion. Explanation: Base case ends recursion and therefore it is necessary for finite recursion.

You are given a knapsack that can carry a maximum weight of 60. There are 4 items with weights {20, 30, 40, 70} and values {70, 80, 90, 200}. What is the maximum value of the items you can carry using the knapsack?

160 Explanation: The maximum value you can get is 160. This can be achieved by choosing the items 1 and 3 that have a total weight of 60.

When a top-down approach of dynamic programming is applied to a problem, it usually _____________.

Decreases the time complexity and increases the space complexity. Explanation: The top-down approach uses the memoization technique which stores the previously calculated values. Due to this, the time complexity is decreased but the space complexity is increased.

What is the advantage of bubble sort over other sorting techniques?

Detects whether the input is already sorted. Explanation: Bubble sort is one of the simplest sorting techniques and perhaps the only advantage it has over other techniques is that it can detect whether the input is already sorted.

An algorithm in which we divide the problem into subproblem and then we combine the sub solutions to form solution to the original problem is known as:

Divide and Conquer Explanation: In Divide and Conquer we divide the problem and then recombine the solution.

If a problem can be solved by combining optimal solutions to non-overlapping problems, the strategy is called _____________.

Divide and conquer. Explanation: In divide and conquer, the problem is divided into smaller non-overlapping subproblems and an optimal solution for each of the subproblems is found. The optimal solutions are then combined to get a global optimal solution. For example, merge sort uses divide and conquer strategy.

Time complexity of binary search algorithm is constant.

False Explanation: It is O(log 2 n), therefore complexity will be logarithmic.

The 0-1 Knapsack problem can be solved using Greedy algorithm.

False Explanation: The 0/1 Knapsack problem cannot be solved using the greedy algorithm, but the fractional Knapsack Problem can be solved using the greedy approach.

What is the best sorting algorithm to use for the elements in array are more than 1 million in general?

Quick sort.

Select the appropriate code that performs bubble sort.

for(int j=arr.length-1; j>=0; j--) { for(int k=0; k<j; k++) { if(arr[k] > arr[k+1]) { int temp = arr[k]; arr[k] = arr[k+1]; arr[k+1] = temp; } } } Explanation: The outer loop keeps count of number of iterations, and the inner loop checks to see if swapping is necessary.

The big-theta notation for f(n) = nlog(n^2 + 1) + n^2 logn is

n^2 logn Explanation: n^2 logn < n^3 , it follows that nlog(n^2 + 1) + n^2 logn is less than n^3 and greater than n^2 logn.

What are the correct intermediate steps of the following data set when it is being sorted with the Insertion sort? 15,20,10,18

15,20,10,18 -- 10,15,20,18 -- 10,15,18,20 -- 10,15,18,20

Which of the following is false about 2-3 Trees?

2-3 tree requires less storage than the BST. Explanation: Search is more efficient in the 2-3 tree than in BST. 2-3 tree is a balanced tree and performs efficient insertion and deletion and it is shallower than BST. But, 2-3 tree requires more storage than the BST.

Consider the following lists of partially sorted numbers. The double bars represent the sort marker. How many comparisons and swaps are needed to sort the next number using the insertion sort. [1 3 4 8 9 || 5

3 comparisons, 2 swaps

The given array is arr = {1,2,4,3}. Bubble sort is used to sort the array elements. How many iterations will be done to sort the array?

4 Explanation: Even though the first two elements are already sorted, bubble sort needs 4 iterations to sort the given array.

An Algorithm is:

A procedure for solving a problem. Explanation: An algorithm is stepwise solution to the problem.

Searching and sorting algorithms are best implemented with which data structure?

An array-based list A linked list ANSWER: Both Above

The following best describes which algorithm? The elements are compared and swapped if the first is found to be greater than the second.

Bubble sorting algorithm

What is an in-place sorting algorithm?

It needs O(1) or O(logn) memory to create auxiliary locations. Explanation: Auxiliary memory is required for storing the data temporarily.

What is the advantage of selection sort over other sorting techniques?

It requires no additional storage space. Explanation: Since selection sort is not an in-place sorting algorithm, it does not require additional storage.

The Worst case occur in linear search algorithm when

Item is the last element in the array or is not there at all. Explanation: The Worst case occur in linear search algorithm when Item is the last element in the array or is not there at all.

Which of the following case does not exist in complexity theory?

Null case Explanation: Null case does not exist in complexity Theory.

The complexity of Binary search algorithm is

O(log n) Explanation: The complexity of binary search is O(logn).

The complexity of merge sort algorithm is

O(n log n) Explanation: The worst-case complexity for merge sort is O(nlogn).

The complexity of linear search algorithm is

O(n) Explanation: The worst-case complexity of linear search is O(n).

The worst-case complexity for insertion sort is

O(n^2 ) Explanation: In worst case nth comparison are required to insert the n^th element into correct position.

The complexity of Bubble sort algorithm is

O(n^2 ) Explanation: The complexity of Bubble sort algorithm is O(n^2 ).

Select the appropriate code that performs selection sort.

int min; for(int j=0; j&lt;arr.length-1; j++) { min = j; for(int k=j+1; k&lt;=arr.length-1; k++) { if(arr[k] &lt; arr[min]) min = k; } int temp = arr[min]; arr[min] = arr[j]; arr[j] = temp; } Explanation: Starting with the first element as 'min' element, selection sort loops through the list to select the least element which is then swapped with the 'min' element.

Optimization of algorithm means:

making that algorithm fast by time and compact by space. Explanation: An Algorithm should be fast and compact.

The complexity of Bubble sort algorithm is

O(n^2 ) Explanation: The worst case complexity for Bubble sort is O(n^2 ); and best case is O(n).

If f(x) = (x 3 - 1) / (3x + 1) then f(x) is

O(x ^2 ) Explanation: 0<(x^3 - 1) / (3x + 1) < x^2

If f(x) = 3x^2 + x^3 logx, then f(x) is

O(x^2 )

In ________ search each element is compared with x till not found.

Sequential Explanation: In linear or sequential search entire list is searched sequentially for x.

Out of following which property algorithms does not share?

Constancy Explanation: All the others are the properties of algorithms.

For an algorithm which is most important characteristic that makes it acceptable:

Correctness and Precision Explanation: An algorithm should be correct otherwise it's of no use even if it is fast and compact.

The worst-case occurs in linear search algorithm when

Item is the last element in the array or is not there at all. Explanation: The Worst case occur in linear search algorithm when Item is the last element in the array or is not there at all.

The below diagram represents what type of sorting algorithm?

Merge sort

You must sort 1 GB of data with only 100 MB of available main memory. Which sorting technique will be most appropriate?

Merge sort

The big-theta notation for function f(n) = 2n^3 + n - 1 is

n^3 Explanation: 2n^3 + n - 1 is less than equal to n^3 .

The time complexity of linear search is given by

none of the mentioned. Explanation: It is O(n), therefore complexity will be linear.

A B-tree of order 4 is built from scratch by 10 successive insertions. What is the maximum number of nodes splitting operations that may take place?

C. 5

Consider an array of elements arr[5]= {5,4,3,2,1} , what are the steps of insertions done while doing insertion sort in the array.

4 5 3 2 1 3 4 5 2 1 2 3 4 5 1 1 2 3 4 5 Explanation: In the insertion sort, just imagine that the first element is already sorted and all the right-side Elements are unsorted, we need to insert all elements one by one from left to right in the sorted Array. Sorted: 5 unsorted: 4 3 2 1 Insert all elements less than 5 on the left (Considering 5 as the key) Now key value is 4 and array will look like this Sorted: 4 5 unsorted: 3 2 1 Similarly, for all the cases the key will always be the newly inserted value and all the values will be compared to that key and inserted in to proper position.

The given array is arr = {1,2,3,4,5}. (bubble sort is implemented with a flag variable). The number of iterations in selection sort and bubble sort respectively are,

4 and 1 Explanation: Selection sort is insensitive to input, hence 4; i.e) (n-1) iterations. Whereas bubble sort iterates only once to set the flag to 0 as the input is already sorted.

The given array is arr = {3,4,5,2,1}. The number of iterations in bubble sort and selection sort respectively are,

5 and 4 Explanation: Since the input array is not sorted, bubble sort takes 5 iterations and selection sort takes 4; i.e) (n-1) iterations.

Consider the following lists of partially sorted numbers. The double bars represent the sort marker. How many comparisons and swaps are needed to sort the next number using the insertion sort. [1 3 4 5 8 9 || 2]

6 comparisons, 5 swaps

If one uses straight two-way merge sort algorithm to sort the following elements in ascending order: 20, 47, 15, 8, 9, 4, 40, 30, 12, 17 then the order of these elements after second pass of the algorithm is: A. 8, 9, 15, 20, 47, 4, 12, 17, 30, 40

8, 15, 20, 47, 4, 9, 30, 40, 12, 17

Which of the following is not true about comparison-based sorting algorithms?

Heap Sort is not a comparison-based sorting algorithm.

In the following scenarios, when will you use selection sort?

Large values need to be sorted with small keys. Explanation: Selection is based on keys, hence a file with large values and small keys can be efficiently sorted with selection sort.

To sort a list with n elements, the insertion sort begins with the __________ element.

Second Explanation: The insertion sort compares second element with first element to start sorting.

The worst-case complexity of quick sort is

O(n^2) Explanation: The worst-case complexity of quick sort is O(n^2 ).

The big-O notation for f(n) = (nlogn + n^2 )(n^3 + 2) is

O(n^5 ) Explanation: 0<n^3 + 2<n^3 , it follows that (nlogn + n^2 )(n^3 + 2) is less than equal to n^5 .

The operation of processing each element in the list is known as

Traversal Explanation: The operation of processing each element in the list is known as Traversal.

On average, a sequential search algorithm would make N/2 number of comparisons for a list of size N.

True

A B-Tree used as an index for a large database table has four levels including the root node. If a new key is inserted in this index, then the maximum number of nodes that could be newly created in the process are:

A. 5 Explanation: Number of children of a node is equal to the number of keys in it plus 1. Given tree has 4 levels, the tree will be increased with one more level if a new key is inserted.

What is an internal sorting algorithm?

Algorithm that uses main memory during the sort. Explanation: Internal sorting algorithm uses internal main memory.

What is an external sorting algorithm?

Algorithm that uses tape or disk during the sort. Explanation: External sorting algorithm uses external memory like tape or disk.

Which of the following problems should be solved using dynamic programming?

Longest common subsequence Explanation: The longest common subsequence problem has both, optimal substructure and overlapping subproblems. Hence, dynamic programming should be used the solve this problem.

2-3 tree is a specific form of _________

B - tree Explanation: The 2-3 trees is a balanced tree. It is a specific form the B - tree. It is B - tree of order 3, where every node can have two child subtrees and one key or 3 child subtrees and two keys.

There are two algorithms suppose A takes 1.41 milli seconds while B take 0.9 milliseconds, which one of them is better considering all other things same.

B is better than A Explanation: B takes less time than A for the same task.

The heap sort algorithm begins by converting the list into a heap, then sorting.

True

The insertion sort algorithm improves on the selection sort method by reducing the number of comparisons.

True

The merge sort algorithm differs from the quick sort only in how it partitions the list, which is to create two nearly equal sub lists.

True

The quick sort algorithm divides the list into two sub lists, then sorts each sub lists, and then combines both sub lists.

True

In dynamic programming, the technique of storing the previously calculated values is called ___________

Memoization Explanation: Memoization is the technique in which previously calculated values are stored, so that, these values can be used to solve other subproblems.

What is the key used in a search algorithm?

Used in operations such as searching, sorting, inserting and deleting.

Which of the following methods can be used to solve the Knapsack problem?

a) Brute force algorithm b) Recursion c) Dynamic programming ANSWER: d) All of the mentioned Explanation: All the mentioned techniques can be used to solve the Knapsack problem.

An algorithm: can be represented through:

a) flow charts b) pseudo codes c) instructions in common language ANSWER: D) ALL MENTIONED Explanation: Algorithm is represented through pseudo codes, normal language sentences or flow charts.


Conjuntos de estudio relacionados

Patho Notes from Prep U - Ch 41 Endocrine

View Set

Short-Run Cost and Output Decisions

View Set

Chapter 19: Assessing the Thorax and Lungs PrepU

View Set

Chapter 12 Motivating Employees: Achieving Superior Performance in the Workplace

View Set

Che giorno è oggi? Qual è la data di oggi?

View Set

Physical Science Chapter 16 // sjhs

View Set