Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

CS 2413 Data Structures EXAM 1 Fall 2018, Page 1 of 9, Lecture notes of Data Structures and Algorithms

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

2022/2023

Uploaded on 05/11/2023

ekagarh
ekagarh 🇺🇸

4.6

(33)

271 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 2413 Data Structures EXAM 1 Fall 2018, Page 1 of 9
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.
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download CS 2413 Data Structures EXAM 1 Fall 2018, Page 1 of 9 and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

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.

  1. (2 points) Which of the following equations correctly describes the runtime of this algorithm? A. f (n) = 1000 B. f (n) = n^2 C. f (n) = 3n^2 D. f (n) = n^2 + 1000 E. f (n) = 3n^2 + 1000
  2. (2 points) Which of the following equivalence classes would be valid to describe its Big O runtime? A. n B. n^2 C. n^3 D. A and B E. B and C
  3. (2 points) Which of the following equivalence classes would be best to describe its Big O runtime? A. n B. n^2 C. n^3 D. A and B E. B and C
  4. (2 points) Which of the following equivalence classes would be valid to describe its Big Ω runtime? A. n B. n^2 C. n^3 D. A and B E. B and C
  5. (2 points) Which of the following equivalence classes would be best to describe its Big Ω runtime? A. n B. n^2 C. n^3 D. A and B E. B and C
  6. (2 points) Which of the following equivalence classes would be valid to describe its Big Θ runtime? A. n B. n^2 C. n^3 D. A and B E. B and C

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

  1. (2 points) What is the time complexity of the RecursiveFunction pseudocode shown above? A. Θ(a) B. Θ(log a) C. Θ(1) D. Θ(a/2) E. Θ(a%2)
  2. (2 points) What is the space complexity of the RecursiveFunction pseudocode shown above? A. Θ(a) B. Θ(log a) C. Θ(1) D. Θ(a/2) E. Θ(a%2)

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

  1. (2 points) What is the time complexity of the IterativeFunction pseudocode shown above? A. Θ(a) B. Θ(log a) C. Θ(1) D. Θ(a/2) E. Θ(a%2)
  2. (2 points) What is the space complexity of the IterativeFunction pseudocode shown above? A. Θ(a) B. Θ(log a) C. Θ(1) D. Θ(a/2) E. Θ(a%2)

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

  1. (2 points) What is the time complexity of the RecursiveFunction2 pseudocode shown above?

A. Θ(n) B. Θ(log 2 n) C. Θ(1) D. Θ(n^2 ) E. Θ(n log 2 n)

  1. (2 points) What is the space complexity of the RecursiveFunction2 pseudocode shown above?

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

  1. (2 points) What is the time complexity of the IterativeFunction2 pseudocode shown above?

A. Θ(n) B. Θ(log 2 n) C. Θ(1) D. Θ(n^2 ) E. Θ(n log 2 n)

  1. (2 points) What is the space complexity of the IterativeFunction2 pseudocode shown above?

A. Θ(n) B. Θ(log 2 n) C. Θ(1) D. Θ(n^2 ) E. Θ(n log 2 n)

  1. (2 points) Which of the following are true?

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

  1. (2 points) Which of the following are true?

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

  1. (2 points) Which of the following are true?

A. Quick Sort ∈ Ω(n^2 ) B. Quick Sort ∈ Ω(n log n) C. Quick Sort ∈ Ω(n) D. B and C E. None of the above

  1. (2 points) Which of the following are true?

A. Quick Sort ∈ Θ(n^2 ) B. Quick Sort ∈ Θ(n log n) C. Quick Sort ∈ Θ(n) D. B and C E. None of the above

  1. (2 points) Which of the following algorithms would be best to sort a randomly ordered array of data?

A. Straight Insertion Sort B. Bubble Sort C. Quick Sort D. All of the above E. None of the above

  1. (2 points) Which of the following algorithms would be best to sort a nearly sorted array of data?

A. Straight Insertion Sort B. Bubble Sort C. Quick Sort D. All of the above E. None of the above

  1. (2 points) Which of the following is an advantage of Quick Sort over Merge Sort?

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

  1. (2 points) Which of the following is an advantage of Merge Sort over Quick Sort?

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