Documents
Resources
Learning Center
Upload
Plans & pricing Sign in
Sign Out

tutorials

VIEWS: 18 PAGES: 5

									                           Department of Computer Science
                              Data Structures (CSC211)
                          Tutorial (1) (2st Semester 14230-31)
                                    Date: 5/4/1431H

Time: 1 hours                                                        Marks: 10

Question 1.

   (i)     Draw the Overview of components of Data Structures
   (ii)    What is the worst-case time complexity of insert operation in an ArrayList?
   (iii)   What is the best-case time complexity of delete operation in an ArrayList?
   (iv)    What is the time complexity of insertion operation of Linked list.



Question 2.
  (a) What is the time complexity (as step count) of the following code fragment?
      Give the count for each statement and the total count. Note that
         n
              n(n  1)
       i  2 .
       i 1

             for (int i = 0; i < n - 1; i++)
                 for(int j = i + 1; j < n; j++)
             {
                     tmp = a[i,j];
                     a[i,j] = a[j,i];
                     a[j,i] = tmp;
               }
  (b) Find the Big-O for the following: (2n 3  n  1) log( 2n 2  3)


Question 3.
For the following piece of pseudo code, find the number of times MyStatement gets
executed:
              sum = 0;
              for (i=1; i<=n; i*=2)
                 for (j=1; j<=n; j*=2)
                   sum++; // MyStatement
                              Department of Computer Science
                                 Data Structures (CSC211)
                             Tutorial (2) (2st Semester 14230-31)
                                      Date: 13/4/1431H

Time: 1 hours                                                                 Marks: 10
Question 1.
    1.   Given that f1(n)=O(n2) and f2(n)=O(n2), then f1(n) = f2(n). True or false?



    2. Which of the following functions has the worst Big-O complexity?
              (a.) 100n (b.) 2n (c.) 1000 (d.) 2n    (e.) n100 (f.) n log n



    3. For small values of n, an algorithm of complexity O(n2) may be more time-
       efficient than an algorithm of complexity O(n). True or false?



    4. If the number of basic operations in an algorithm is given by:
         2
       2n + 1000 n log n + 300n + 4000
       Write the Big-O complexity of the algorithm.

    5. By finding appropriate values of c and n0, prove that:
       f(n) = 3 n2 + 8 n + 4 is O(n2)

    6. Find the exact number of basic operations in the following program fragment
       double x, y;
       x = 2.5 ; y = 3.0;
       for(int i = 0; i <= n; i++)
       {
                a[i] = x * y;
                x = 2.5 * x;
                y = y + a[i];
       }

Question 2.
Consider this version of a linked list where reference tail does not exist:

public class LinkedList{
   protected Element head;
   public void insert(Object obj){
     // to be completed
   }
   ...
   public final class Element{
       Object data;
       Element next;
       Element(Object obj, Element element){
          data = obj;
          next = element;      }

     }}
Write the implementation details of the insert() method, by which Object obj is inserted at the end
of an instance of Major1LinkedList.
Question 3.
Study the following code segment carefully and write the big-O complexity of the parts
     indicated in the table at the end of the question. You may write your answer
     directly without counting the number of basic operations.
        public static int calculate(int n){
          int result = 0;
          for(int k = 1; k <= n; k++)
               for(int j = 1; j <= n; k++)
                      result += k*j;
           return result;
      }
Question 4.
Study the following program carefully and write down its output.

          public class Major1Q5{
               public static void myMethod(int n) {
                  if (n>0) {
                     System.out.print(" "+n);
                     myMethod(n-1);
                     myMethod(n-1);
                  }
               }
               public static void main(String args []){
                  myMethod(3);
               }
          }
          Output:




Question 5.
    1. The following operations are applied to an empty queue of integers:
       enqueue(3),enqueue(5),dequeue(),enqueue(8),enqueue(2),dequeue()
       What element is at the front of the queue and what element is at the rear of the
       queue after these operations?


              Element at queue Front          Element at queue Rear



    2. Evaluate the postfix expression 5 2 4 * 2 / + 3 -
    3. Consider an arithmetic expression E containing only one type of parenthesis: {, (,
       or [. Which data structure is suitable for verifying the validity of E?
    4. Given the expression (4*3 + 6/2), convert it to its postfix form.
    5. Given the expression 6*(5+((2+3)*8)+3) convert it to its postfix form
Question 5.

Write the output generated by the following code:
StackX theStack = new StackX();

theStack.push("A");
theStack.push("B");                                 The top of the stack
theStack.push("C");                                                        "C"
System.out.print(theStack.peek());
System out.println(theStack.isEmpty());                                    "B"
while (!theStack.isEmpty())                                                "A"
{
    System.out.println(theStack.pop());
}


Question 6.
Write the output generated by the following code:
       StackX theStack = new StackX();
       StackX.push(3);
       StackX.push(2);
       StackX.push(1);
       System.out.println(StackX.isEmpty());
       System.out.println(StackX.peek());
       StackX.pop();
       System.out.println(StackX.peek());
       StackX.pop();
       System.out.println(StackX.peek());
       StackX.pop();
       System.out.println(StackX.isEmpty());
                          Department of Computer Science
                             Data Structures (CSC211)
                         Tutorial (3) (2st Semester 14230-31)
                                  Date: 15/4/1431H

Time: 1 hours                                                        Marks: 10


Study the following code segment carefully count the number of steps and then write
     the big-O complexity of the parts indicated in the table at the end of the question.


int n = 1000;                                // ……………….
int operations = 0;                          // ……………………
int sum = 0;
for (int j = 1; j ≤ n; j++) {                // ….. + ….. + ……
  operations++;                              // …………………
  sum += j;                                  // ……………………….




public static void swap(String[] array, int left, int right)
{
  String temp = array[left];      // …………………….
  array[left] = array[right];     // ……………………………
  array [right] = temp;           // ……………………………….
}




// Print @ for each n
for (int i = 0; i < 2 * n; i++)               // ………….. + ………….. + …………..
    sum++;                                     // …………………………….




double sum = 0.0;             // ……………
double largest = a[0];        // ……………………….
for (int i = 1; i < n; i++) { // …………….. +                       ………………..      +
…………………
  sum += a[i];                // …………………..
  if (a[i] > largest)         // ………………………..
    largest = a[i];           // …………….
}



Given the expression infix expression a + b * c + (d * e + f) * g convert     to postfix
     xpression

								
To top