CPS 350 cumaltive Multiplue choice

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

If an algorithm requires 7 basic operations for an algorithm with a problem size of n, the algorithmic complexity is

O(1)

The merge sort algorithm always divides an array in half (when possible).

True

Given the following array elements, what does the array look like after two iterations of selection sort, arranging items in ascending order? 92 42 73 19 86 33 7 60

7 19 73 42 86 33 92 60

For node with index in a binary heap, its right child (if it has one) is placed at which array index?

2 × i + 1

Write an efficient algorithm to solve the following problem. Problem description: There are two sorted arrays nums1 and nums2 of size m and n respectively. Return the median of the two sorted arrays. You may assume nums1 and nums2 are not BOTH empty, though, one MAY be. Example 1: nums1 = [1, 3]nums2 = [2] The median is 2.0 Example 2: nums1 = [1, 2] nums2 = [3, 4] The median is (2 + 3)/2 = 2.5 Grading notes: Easy approach: Receive 20 points if your method is correct and its running time is O(n + m). Hint: Recall the merge() method. Challenge: Receive 30 points if your method is correct and its running time is O(log(n + m)). Hint: Recall the idea of binary search. class Solution { public double findMedianSortedArrays(int[] nums1, int[] nums2) { //// YOUR IMPLEMENTATION GOES HERE// } // end of method } // end of class

// 1) Create array of size nums1.length+nums2.length [3] [-2] // 2) Merge/Sort nums1 and nums2 into larger array [14] [-10] // 3) Calculate median [3] [-1] class Solution { public double findMedianSortedArrays(int[] nums1, int[] nums2) { int total = num1.length +num2.length; // Calculate total size if (total%2==0){ // If total is even. return (getkth1,0,num1.length-1, num2 , 0 , num2.length-1, total/2) // Not clear what "getkth", "getKth1" does. Looks like this might be where the median would be determined.+ getKth(num1, 0, nums1, 0, nums1.length-1, nums2, 0, nums2.length-1, total/2-1))/2.0; } else { // If total is odd return getKth( nums1,0, nums1.length-1, nums2, 0, nums2.length-1, total/2); } // end of method } // end of class

If we have a heap with n elements, and we add another n elements to it, what is the maximum increase of height associated with the binary tree representation of the heap?

1

When we map a min-heap with n elements to an array with n + 1 elements, we ignore array element 0, and place the root value at which array index?

1

Consider the following someMethod(). // input array a is sortedpublic static int someMethod(int[] a, int target){return help(a, target, 0, a.length - 1);}public static int help(int[] a, int key, int lo, int hi){if (lo > hi) return -1;int mid = lo + (hi - lo) / 2;if (a[mid] == key) return mid;else if (a[mid] < key) return help(a, key, mid+1, hi);else return help(a, key, lo, mid-1);} A. (7pts) Derive the running time (i.e., T(n) of someMethod()) B. (3pts) Write the Big Oh notation of the running time.

1. T(n) = 3 + T(n/2)= 3 + 3 + T(n/4)= 3 + 3 + 3+ T(n/8)...= 3k + T(n/2^k) BASE CASE (i.e., T(1)) 2.SUBSTITUTE k = log n INTO T(n) = 3k + T(n/2^k) = 3k + T(1)T(n) = 3 log n + T(1) = 3 log n + cOrder of algorithm is O(log n)

1.What is the running time for the loop below (i.e., the number of iterations in the loop)? 2. Write the Oh notation for the running time. for(i = 1; i <= n; i++) for(j = 1; j <= i; j++) sum = sum + j;

1. [-2] T(n) = 1 + 2 + 3 + ... + n-1 + n = n(n+1)/2 2. [-2] O(n^2)

What is the running time for the loop below (i.e., the number of iterations in the loop)? Write the Oh notation for the running time. for (i = 1; i <= 1000; i++) for (j = 1; j <= i; j++) sum = sum + j;

1. [-3] T(1000) = 1 + 2 + 3 + ... + 1000 = 1000(1001)/2 = 500,5002. [-2] O(1) 2. [-2]O(1)

Given the following array elements, what does the array look like after one iteration of insertion sort, arranging items in ascending order? 14 5 7 19 23 4 12 21

5 14 7 19 23 4 12 21

Which data structure gives O(1) running time for searching?

A.binary search tree B.red-black BST C.AVL BST D.none of the above(correct)

Java sorting implementations sort objects that implement the ________ interface.

Comparable

The quick sort algorithm always divides an array in half (when possible).

False

Record the final state of the "s" in the array provided after all the specified operations are performed. Be sure to include the values of all elements in the array, as well as the value of front, back, and size.

Final State of 's' Index 0=0 1=23 2=2 3=21 4=3 5=5 6=-4 7=18 8=-33 9=6 value size= 3 value of front= 4 value of back= 7

Consider the following BST diagram: Which of the following trees represents the correct result after inserting element B?

I

You wish to traverse a red-black tree using preorder traversal. Arrange the following actions in the correct order to accomplish this. I Print the root II Print the left subtree recursively III Print the right subtree recursively

I, II, III

A max-heap is: I a complete binary tree II the key of every node (except root) is no bigger than that of its parent III the root contains the largest element

I, II, and III

Which following sort algorithms are in-place? I Selection II Insertion III Merge IV Quick

I, II, and IV

A min-heap is: I a perfect binary tree II the key of every node (except root) is no smaller than that of its parent III the root contains the smallest element

II and III

Which following sort algorithms are stable? I Selection II Insertion III Merge IV Quick

II and III

Which of the following are balanced trees? I binary search tree II red-black BST III binary heap IV hash table

II, III

Consider the following left-leaning red-black tree diagram immediately after W is attached to S with a red link: Should we rotate and/or flip colors after W is inserted?

Left-Rotate

Given N distinct keys, what is the order of the running time for the following sorting algorithm? 1. Shuffle the keys. 2. Insert the keys into a red black BST, one at a time. 3. Do an inorder traversal of the tree.

O(N log N)

What is the complexity of method Key deleteMin() using a min-heap?

O(log (n))

What is the complexity of method void insert(Key k) using a min-heap?

O(log (n))

In the worst case, what is the height of a red-black tree with n nodes?

O(log n)

In the worst case, what is the height of a 3-way heap of N elements?

O(log3 N)

In the average case, quicksort is a(n) ______ algorithm.

O(n log n)

Merge sort is a(n) ______ algorithm in the worst case.

O(n log n)

What is the efficiency of the heapsort algorithm?

O(n log n)

In merge sort, the merge step takes at most _________ comparisons for n items.

O(n)

In the worst case, what is the height of a BST with n nodes?

O(n)

The best-case performance for an array of n items using insertion sort is

O(n)

What is the efficiency of the bucket sort algorithm?

O(n)

In the previous sort algorithm (reproduced below), what is its running time in the worst case? public static void someSortAlgorithm(int[] a) {int N = a.length;for (int i = 0; i < N; i++){for (int j = i; j > 0; j--){if (less(a[j], a[j-1]))exch(a, j, j-1);else break; }}}

O(n2)

The selection sort requires _______ comparisons for an array of n items.

O(n2)

What is the running time of someMethod? // 0 <= lo < hi < a.lengthpublic static int someMethod(Comparable[] a, int lo, int hi){ int i = lo, j = hi+1; while (true){ while (less(a[++i], a[lo]))//less(x,y) returns true if x is less than y {if (i == hi) break;} while (less(a[lo], a[--j])){if (j == lo) break;} if (i >= j)break; exch(a, i, j); // swap a[i] with a[j]}// end of whileexch(a, lo, j); // swap a[lo] with a[j]return j; // return new index of pivot item} // end of method

T(n) = n + c [-3]

What is the time complexity of the following if statement? if (condition) S1 else S2

The complexity of the condition plus the larger of the complexity of S1 or S2

Assume we are using quicksort to sort an array in ascending order. What can we conclude about the elements to the right of the currently placed pivot element?

They are all greater than or equal to the pivot element.

Which of the following statements about inheritance is correct?

You can always use a subclass object in place of a superclass object.

To properly evaluate the effectiveness of an algorithm, you need to determine

all of the above

In a red-black tree, where the root node data value = 45, what do we know about the data values of all the descendants in the right subtree of the root?

all will be > 45

Consider traversal of a tree. If you use a stack and push each node's children from right to left as described in the traversal procedure below, what kind of traversal will you obtain? TRAVERSAL PROCEDURE A. Use a stack, which initially contains only the root. Then repeat the following steps: i. Pop a node ii. Visit it (e.g., print key on the screen) iii. Push its children (in the order from right to left) B. Continue until the stack is empty

pre-order

Binary Search Tree leetcode quiz

class Solution { public int search(int[] nums, int target){ //initialize variables int lowest =0; int mid; int highest = nums.length -1; //while loop that goes through and searches the BST while (lowest<=highest){ mid = lowest + (highest-lowest) /2; //Checks to see if target is equal to mid if( nums[mid]== target) return mid; //Checks to see if target is greater than mid if not checks if target is less than mid if (nums[mid]<target) lowest = mid +1; else highest = mid -1; } // If target doesn't exist return -1 return -1; } }

Which sort algorithm does the following implement? public static void someSortAlgorithm(int[] a) {int N = a.length;for (int i = 0; i < N; i++){for (int j = i; j > 0; j--){if (less(a[j], a[j-1]))exch(a, j, j-1);else break; }}}

insertion sort

Which sort algorithm is used in the sort method in the Java Arrays class when the array length is less than 7?

insertion sort

Which sorting algorithm is generally the slowest for a large number of items?

selection sort

The best-case scenario for an insertion sort is

the elements are already in sorted order

The most programming intensive part of merge sort is

the merge step

Problem size is defined as

the number of items an algorithm processes

The merge method requires a total of n visits. We found that the number of visits required to sort an array of n elements is T(n) = T(n/2) + T(n/2) + n. What does T(n/2) describe?

the total number of visits to merge sort an array of n/2 elements


Set pelajaran terkait

complex 1 practice questions (final)

View Set

Knowledge Check: Chapter 01 Food Choices and Human Health

View Set

Quiz #2 for Intro to Information Systems (Week 9- Week 14)

View Set

Chapter 3 learning curve & mastery activity

View Set

Practice questions-Constitutional Underpinnings

View Set