Exam 1 Sample Solution - Data Structure and Algorithms | COP 3530, Exams of Data Structures and Algorithms

Material Type: Exam; Class: DATA STRUC/ALGORITHMS; Subject: COMPUTER PROGRAMMING; University: University of Florida; Term: Unknown 1989;

Typology: Exams

Pre 2010

Uploaded on 09/17/2009

koofers-user-ur5
koofers-user-ur5 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structures & Algorithms
Exam 1
Sample 1, 75 Minutes
Solutions
1. (a) The code is given below.
public void insertZero(int i, int k)
{// Insert k zeroes after element i.
// When i = -1, insert before element 0
if (k < 0 || i < -1 || i >= size)
throw new IllegalArgumentException("improper i
and/or k");
// make sure we have enough space
if (size + k > element.length)
{// get larger array
Object newArray = new Object [size + k];
System.arraycopy(element, 0, newArray, 0, size);
element = newArray;
}
// move k positions up
for (int j = size - 1; j > i; j--)
element[j+k] = element[j];
// fill with zeroes
for (int j = i + 1; j <= i + k; j++)
element[j] = zero;
size += k;
}
(b)
When an exception is thrown the complexity is Theta(1). Otherwise, it takes
O(size + k) time to increase the array size (if necessary). The second for loop
takes O(size)), and the third loop takes O(k) time. Therefore, the complexity
is O(size+k).
2. (a) The code is given below.
pf3
pf4

Partial preview of the text

Download Exam 1 Sample Solution - Data Structure and Algorithms | COP 3530 and more Exams Data Structures and Algorithms in PDF only on Docsity!

Data Structures & Algorithms

Exam 1

Sample 1, 75 Minutes

Solutions

  1. (a) The code is given below. public void insertZero(int i, int k) {// Insert k zeroes after element i. // When i = -1, insert before element 0 if (k < 0 || i < -1 || i >= size) throw new IllegalArgumentException("improper i and/or k"); // make sure we have enough space if (size + k > element.length) {// get larger array Object newArray = new Object [size + k]; System.arraycopy(element, 0, newArray, 0, size); element = newArray; } // move k positions up for (int j = size - 1; j > i; j--) element[j+k] = element[j]; // fill with zeroes for (int j = i + 1; j <= i + k; j++) element[j] = zero; size += k; } (b) When an exception is thrown the complexity is Theta(1). Otherwise, it takes O(size + k) time to increase the array size (if necessary). The second for loop takes O(size)), and the third loop takes O(k) time. Therefore, the complexity is O(size+k).
  2. (a) The code is given below.

public void compress() { int numberToRemove = size / 2; ChainNode currentNode = lastNode.next; // remove the nodes for (int i = 0; i < numberToRemove; i++) {// remove node next to currentNode currentNode.next = currentNode.next.next; currentNode = currentNode.next; } // new size size -= numberToRemove; } (b) The for loop iterates n/2 times, where n is the initial size of the chain. Each iteration of this loop takes O(1) time. Therefore, the complexity of this loop is O(n). The remainder of the code takes O(1) time. Therefore, the overall complexity is O(n). 3 (a) A sample 5 x 5 F-matrix is given below. 1 2 3 4 5 6 0 0 0 0 7 8 9 10 11 12 0 0 0 0 13 0 0 0 0 The compact representation is [1,2,3,4,5,6,7,8,9,10,11,12,13]. (b) The code is given below. public Object get(int i, int j) {// return F(i,j) if ( i < 1 || j < 1 || i > n || j > n) throw new IllegalArgumentException("index out of range"); if (i == 1)

{// return F(i,j) if ( i < 1 || j < 1 || i > n || j > n) throw new IllegalArgumentException("index out of range"); if (i == 1) // first row return element[j - 1]; if (i == (n + 1) / 2) // middle row return element[n – 1 + j]; if (j == 1) // first column if (i < n / 2) // top part return element[2 * n + i - 2]; else // bottom part return element[2 * n + i - 3]; // not part of F return zero; }