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

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

View Full Document Right Arrow Icon
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.
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/21/2009 for the course CS 3343 taught by Professor Staff during the Spring '08 term at The University of Texas at San Antonio- San Antonio.

Page1 / 3

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

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online