





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
No Electronic Devices Permitted You may not use any electronic devices during the course of this examination, including but not limited to calculators, ...
Typology: Lecture notes
1 / 9
This page cannot be seen from the preview
Don't miss anything!
Student Name: Student ID #
OU Academic Integrity Pledge
On my honor I affirm that I have neither given nor received inappropriate aid in the completion of this exercise.
Signature: Date:
Notes Regarding this Examination
Open Book(s) You may consult any printed textbooks in your immediate possession during the course of this exami- nation.
Open Notes You may consult any printed notes in your immediate possession during the course of this examination.
No Electronic Devices Permitted You may not use any electronic devices during the course of this examination, including but not limited to calculators, computers, and cellular phones. All electronic devices in the student’s possession must be turned off and placed out of sight (for example, in the student’s own pocket or backpack) for the duration of the examination.
Violations Copying another’s work, or possession of electronic computing or communication devices in the testing area, is cheating and grounds for penalties in accordance with school policies.
Section I. Definitions of Time and Space Complexity
Definition of Big O: Let f (n) and g(n) be functions mapping non-negative integers to real numbers. We say that f (n) ∈ O(g(n)) if there is a real number c > 0 and a fixed integer n 0 ≥ 1 such that f (n) ≤ cg(n) for every integer n ≥ n 0. Definition of Big Ω: Let f (n) and g(n) be functions mapping non-negative integers to real numbers. We say that f (n) ∈ Ω(g(n)) if there is a real number c > 0 and a fixed integer n 0 ≥ 1 such that f (n) ≥ cg(n) for every integer n ≥ n 0. Definition of Big Θ: Let f (n) and g(n) be functions mapping non-negative integers to real numbers. We say that f (n) ∈ Θ(g(n)) if there are real numbers c, d > 0 and a fixed integer n 0 ≥ 1 such that dg(n) ≤ f (n) ≤ cg(n) for every integer n ≥ n 0. Note: For questions in this section, consider an algorithm that operates on n data items. It takes 1000 operations to initialize, regardless of the number of data items, then takes 3 n^2 operations to complete.
Section III. Applications of Time and Space Complexity
Consider the following pseudocode (noting that % is modulus division, which returns the remainder after integer division): Algorithm RecursiveFunction (a) // a is a non-negative integer if (a = 0) return 0 else return (a % 2) + (^10) * RecursiveFunction(a / 2) endif
Consider the following pseudocode: Algorithm IterativeFunction (a) // a is a non-negative integer b ← 0 while (a > 0) b ← 10 * b + (a % 2) a ← a / 2 endwhile return b
Consider the following pseudocode: Algorithm RecursiveFunction2 (A) // A is an array of n items (1 or more) if (A.size = 1) return A[0] else B ← A[1 : A.size-1] // creates new array B of n-1 items return max(A[0], RecursiveFunction2 (B)) endif
A. Θ(n) B. Θ(log 2 n) C. Θ(1) D. Θ(n^2 ) E. Θ(n log 2 n)
A. Θ(n) B. Θ(log 2 n) C. Θ(1) D. Θ(n^2 ) E. Θ(n log 2 n)
Consider the following pseudocode: Algorithm IterativeFunction2 (A) // A is an array of n items (1 or more) B ← A // creates new array B of n items m ← B[0] while (B.size > 1) B ← B[1 : B.size-1] // replaces B with array of one fewer items m ← max(m, B[0]) endwhile return m
A. Θ(n) B. Θ(log 2 n) C. Θ(1) D. Θ(n^2 ) E. Θ(n log 2 n)
A. Θ(n) B. Θ(log 2 n) C. Θ(1) D. Θ(n^2 ) E. Θ(n log 2 n)
A. Bubble Sort ∈ Θ(n^2 ) B. Bubble Sort ∈ Θ(n log n) C. Bubble Sort ∈ Θ(n) D. All of the above E. None of the above
A. Quick Sort ∈ O(n^2 ) B. Quick Sort ∈ O(n log n) C. Quick Sort ∈ O(n) D. B and C E. None of the above
A. Quick Sort ∈ Ω(n^2 ) B. Quick Sort ∈ Ω(n log n) C. Quick Sort ∈ Ω(n) D. B and C E. None of the above
A. Quick Sort ∈ Θ(n^2 ) B. Quick Sort ∈ Θ(n log n) C. Quick Sort ∈ Θ(n) D. B and C E. None of the above
A. Straight Insertion Sort B. Bubble Sort C. Quick Sort D. All of the above E. None of the above
A. Straight Insertion Sort B. Bubble Sort C. Quick Sort D. All of the above E. None of the above
A. Better average runtime equivalence class B. Stability with respect to previously sorted results C. Sorts within existing array D. All of the above E. None of the above
A. Better average runtime equivalence class B. Stability with respect to previously sorted results C. Sorts within existing array D. All of the above E. None of the above
Short Answer Question 1: Quicksort (30 points)
Algorithm Quicksort (A, left, right) if (left < right) pivotPoint ← b(left+right)/2c // note central pivot i ← left - 1 j ← right + 1 do do i ← i + 1 while (i < A.size) and (A[i] ≤ A[pivotPoint]) do j ← j - 1 while (j ≥ i) and (A[j] ≥ A[pivotPoint]) if (i < j) then swap (A[i], A[j]) while (i < j) if (i < pivotPoint) then j ← i swap (A[pivotPoint], A[j]) Quicksort (A, left, j-1) Quicksort (A, j+1, right) endif
Show the steps followed by the Quicksort algorithm given above in pseudocode when sorting the following array. Draw one figure for each call to Quicksort. You may omit calls where left ≮ right.
value 26 40 48 61 86 94 57 16 25 11
index 0 1 2 3 4 5 6 7 8 9