{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# mock exam sol - Mock Exam for Midterm I Analysis of...

This preview shows pages 1–2. Sign up to view the full content.

Mock Exam for Midterm I Analysis of Algorithms CS3343 The actual exam will be closed-book and will include truelfalse and multiple-choice questions. In the actual exam, you will write your answers on the same paper on which the questions are printed. This collection of problems is intended to be representative of the material that is important on the exam. In addition to the problems given here, the problems from in-class practice quizes are also representative of the questions on the exam. 1. If you want to sort an array that you know is already close to being sorted, would it be better to use insertion sort or selection sort? Why? Solution: It is better to use insertion sort because as the algorithm adds a new value to the portion of the list that is already sorted, when it is already in the right place with respect to the other values, the inner loop terminates immediately. When the entire array is already sorted, the algorithm's running time is O(n), while that of selcection sort remains 0(n2). So actually, if the original array is sorted or very close to sorted, with only a few values out of place, insertion sort is even better than mergesort, which remains 0 (n lg n). 2. Write an iterative (i.e., non-recursive) algorithm that performs binary search on a sorted array. Write a loop invariant that enables you to prove that if the given value is in the array, your algorithm fiiids it. Prove your loop invariant holds at the start of each iteration.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern