mid1 prac sol

.pdf

School

University of Maryland, College Park *

*We aren’t endorsed by this school

Course

351

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

4

Uploaded by DeanBaboonPerson1023 on coursehero.com

Kruskal CMSC351: Practice Midterm 1 Fall 2023 These are practice problems for the upcoming midterm exam. You will be given a sheet of notes for the exam. Also, go over your homework assignments. Warning: This does not necessarily reflect the length, difficulty, or coverage of the actual exam. Problem 1. Assume that you are sorting an array of size n using Bubble Sort, where n = 2 k 1. Assume that the smallest element is in the first location. The next two smallest elements are in the next two locations in some order. The next four smallest elements are in the next four locations in some order. The next eight smallest elements are in the next eight locations in some order. Etc. For example, assume n = 7 (and k = 3), the input might look like 10, 30, 20, 50, 70, 40, 60 The algorithm does not know this, and executes without this extra information. (a) Assume that the elements within each group are in reverse order. For example, if n = 7 (and k = 3), the input will look like 10, 30, 20, 70, 60, 50, 40 What is the number of exchanges? Get the exact answer as a function of k , and the exact high order term as a function of n . Show your work. Solution: Since n = 2 k 1, we know that k = lg( n + 1). We know from class that the worst case number of exchanges for Bubble Sort on a list of size m is ( m 1) m/ 2. We can treat each group as an independent Bubble Sort. This gives k 1 X i =0 (2 i 1)2 i 2 = 1 2 4 k 1 4 1 1 2 (2 k 1) = 1 2 (2 k ) 2 1 3 1 2 (2 k 1) = 1 2 ( n + 1) 2 1 3 1 2 (( n + 1) 1) = n ( n 1) 6 (b) Assume that the elements within each group are in random order. What is the average number of exchanges? Get the exact answer as a function of k , and the exact high order term as a function of n . Show your work. Solution: We know from class that the average case number of exchanges for Bubble Sort on a list of size m is ( m 1) m/ 4. We can treat each group as an independent Bubble Sort. This gives k 1 X i =0 (2 i 1)2 i 4 = 1 4 4 k 1 4 1 1 4 (2 k 1) = 1 4 (2 k ) 2 1 3 1 4 (2 k 1) = 1 4 ( n + 1) 2 1 3 1 4 (( n + 1) 1) = n ( n 1) 12
Kruskal CMSC351: Practice Midterm 1 Fall 2023 Problem 2. Assume you have an array of numbers, where each value occurs at most twice. We consider sums of contiguous numbers in the array. But we only consider such sums whose two endpoints have the same value. The sum includes the two equal values themselves. So if the two equal numbers are at index i and index j ( i < j ) in array A , then we sum all the values A [ i ] , A [ i + 1] , . . . , A [ j ]. (a) Give an algorithm that finds the maximum such sum. Make your algorithm as efficient as possible. Describe the algorithm briefly in English and in psuedo code. (b) Analyze the running time of your algorithm. Problem 3. Let A [1 , .., n ] be an array of n numbers (some positive and some negative). (a) Give an algorithm to find which two numbers have sum closest to zero. Make your algorithm as efficient as possible. Write it in pseudo-code. (b) Analyze its running time. Problem 4. Assume the you have a heap of size n = 2 m 1 (stored in an array). (The largest element is at the root.) You may describe the following algorithms in high level language, but they must be clear. Give your answers as a function of n . You may assume n is large. You may use extra memory. (a) Give an algorithm, minimizing the number of comparisons, to find the smallest element in the heap. Exactly how many comparisons does it take. (b) Give an algorithm, minimizing the number of comparisons, to find the second smallest element in the heap. Exactly how many comparisons does it take. (c) Give an algorithm, minimizing the number of comparisons, to find the third smallest element in the heap. Exactly how many comparisons does it take. Problem 5. Consider the following algorithm for finding the two smallest elements in a list: Sort the first two elements. Call them special . Then iterate through the remainder of the elements one at a time. First compare each new element to the larger of the two special elements, and if necessary compare it to the smaller. The two new special elements will be the smallest two of those three elements. (a) Write the pseudocode for this algorithm. (b) What is the exact best case number of comparisons? Justify and simplify. Solution: n 1 (c) What is the exact worst case number of comparisons? Justify and simplify. Solution: 2 n 3 Page 2 of 4
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help