0% found this document useful (0 votes)
12 views2 pages

Data Structures and Algorithms Worksheet

ASTU DSA

Uploaded by

chalchisa.takala
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Data Structures and Algorithms Worksheet

ASTU DSA

Uploaded by

chalchisa.takala
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Adama Science and Technology University

College of Electrical Engineering and Computing


Department of Computer Science and Engineering

CSEg 2101: Data Structure and Algorithms Worksheet I


1. Discuss the following with example
a. Data structures e. Discuss different ways of representing algorithms
b. Primitive/Primary Data structures f. Empirical Analysis of Algorithms
c. Abstract Data Type g. Theoretical Approach for Algorithm Analysis
d. Algorithm and its properties. h. Asymptotic Analysis of Algorithms

2. Write the definition for Big-O and using the definition show that
a. √ b.
3. Arrange the following running times in increasing order of asymptotic growth. Justify your answer!

4. For each of the following six program fragments compute T(n) and Big-O
(1) sum = 0; (4) sum = 0;
for( i = 0; i < n; ++i ) for( i = 0; i < n; ++i )
++sum; for( j = 0; j < i; ++j )
++sum;
(2) sum = 0; (5) sum = 0;
for( i = 0; i < n; ++i ) for( i = 0; i < n; ++i )
for( j = 0; j < n; ++j ) for( j = 0; j < i * i; ++j )
++sum; for( k = 0; k < j; ++k )
++sum;
(3) sum = 0; (6) sum = 0;
for( i = 0; i < n; ++i ) for( i = 1; i < n; ++i )
for( j = 0; j < n * n; ++j ) for( j = 1; j < i * i; ++j )
++sum; if( j % i == 0 )
for( k = 0; k < j; ++k )
++sum;

5. Write the algorithm, find the running time T( ) and the Big-O class for the following problems.
a. Finding the minimum in list of numbers size .
2 2 2 2
b. Finding the sum: 1 +2 +3 +…+ where is a positive integer
th
c. Finding the largest number in list of numbers size N.
d. Copy an array of integers A in to an array B of same size
e. Adding two matrices A and B of size
f. Multiplying two matrices A and B of size
g. For determining a positive number is prime or not
6. An algorithm takes 0.5 ms for input size 100. How large a problem can be solved in 1 min if the running
time is the following:

3
a. T(n)=n b. T(n)=nlgn c. T(n)=n
7. What is searching? Give the algorithm for the following known searching algorithms and their efficiency in
best and worst case.
a. Sequential/ Linear Search
b. Binary Search(provide recursive and non recursive algorithms)
8. What is sorting? Give the algorithm for the following known sorting algorithms and their efficiency in best
and worst case.
a. Insertion sort b. Selection sort c. Bubble sort
d. For the following list show the partially sorted list after each iteration of the outer loop.
8, 9, -1, 5, 0,-2
9. Array is a popular data structure for storing list of items. What is an Array? Discuss its advantage and
limitations.
th th
10. The common operations performed on array include: Accessing i element, inserting at i position,
th
deleting the i element, searching, sorting , copying elements. Using c++:
a. Provide implementation for ADT “MyArray” that supports all the operations listed above. You
can use either struct or class for the ADT definition:
Hint: define the ADT as follows and provide implementation of the above operations

#define MAX 100


struct MyArray{
int data[MAX];
int n=-1;
}

b. Using a static array like above may limit the maximum number of elements to be stored on the
array. Can you provide a dynamic array implementation that increases size when array is full?
Hint: define the ADT as follows using pointer and provide implementation for initialize,
increaseSize operation. increaseSize can be done by creating a new array of double size and
copying all elements.

struct MyDynamicArray{
int *data=NULL;
int n=-1;
}

11. Define Stack.


12. Define Queue.
13. What is the major difference of Stack and Queue
14. Implement Stack and Queue using array.
15. Evaluate the following postfix expression using stack. At each iteration show the content of the stack.
6 5 2 3 + 8 ∗ +3 + ∗
16. Convert the following infix expression to postfix expression using stack. At each iteration show the
content of the stack and the postfix output
a + b * c/d + ( e * f + g ) * h
17. Write a program to reverse a string(character array) using a stack
18. Write a code to implement circular queue (all operations i.e. enque(), display and deque() operations)
19. Given two sorted arrays of integers, L1 and L2, write a function to compute
a. the intersection between L1 and L2
b. the union of L1 and L2 as sorted list L3

Common questions

Powered by AI

Arrays are advantageous due to their simplicity and efficiency in accessing elements; each element can be accessed directly via its index, providing O(1) access time. They are also space-efficient if the size of the needed storage is known beforehand. However, arrays have several limitations: they require contiguous memory allocation, making large arrays difficult to manage in fragmented memory; they have a fixed size, lacking flexibility for dynamic data sets; and operations such as insertion and deletion are costly, requiring shifting of elements.

Big-O notation gives a high-level understanding of the time complexity of an algorithm by describing its growth rate concerning input size. It helps in comparing and categorizing algorithms irrespective of machine-specific factors. To prove a function f(n) is O(n^2), you must show there exist constants c > 0 and n0 ≥ 0 such that for all n ≥ n0, f(n) ≤ c * n^2. This involves solving for c and n0 such that the inequality holds, thereby demonstrating that the function's growth doesn't exceed n^2 beyond a certain point.

Converting an infix expression to postfix involves reading the infix expression left to right and using a stack to systematically rearrange operators based on precedence rules. Operators are pushed onto the stack, while operands are added directly to the result. When encountering an operator of lower precedence than the operator on the stack, the operator from the stack is popped to the result until the stack is empty or a lower precedence operator is found. Parentheses are used to override precedence: upon encountering '(', operators are pushed onto the stack until ')' is encountered, then operators are popped to the result up to '('. This structured approach ensures a valid postfix where operators appear after operands.

An Abstract Data Type (ADT) is a model for a certain data structure that defines the kind of data it can hold, the operations that are permissible on that data, and the types of parameters these operations can accept. However, it does not specify how the operations are implemented. For instance, a stack can be considered an ADT. It operates on the principle of Last-In-First-Out (LIFO) and supports operations like push, pop, and peek. The implementation of the stack's operations could be using arrays or linked lists, but the user of the ADT is abstracted from these implementation details.

Asymptotic analysis examines the behavior of an algorithm as the input size approaches infinity, focusing on growth trends rather than precise performance metrics. It provides a framework for comparing algorithms independently of machine-specific factors, focusing on worst-case, average-case, and best-case complexities. This analysis helps in understanding scalability and efficiency, allowing decisions on the choice of algorithms depending on the size of data and required performance, crucial for designing efficient and scalable software systems.

Dynamic arrays are preferable when the size of the dataset grows dynamically or is not known in advance, as they can change size automatically when full, maintaining efficiency in terms of space allocation. They overcome the fixed size limitation of static arrays, thus providing greater flexibility and scalability. While they have higher overhead due to allocation and copying operations, this trade-off is often justified in applications where unpredictable data size can be expected.

A stack is a collection of elements with a Last-In-First-Out (LIFO) principle, meaning the last added element is the first one to be removed, supporting operations like push and pop. A queue follows a First-In-First-Out (FIFO) principle, where elements are added at the back and removed from the front, supporting operations such as enqueue and dequeue. The key difference is in the order of element removal; a stack removes the most recently added element, whereas a queue removes the least recently added element.

The empirical method of algorithm analysis involves implementing the algorithm and running it on sample data sets to measure its performance in terms of execution time, memory usage, and other resources. This is practical and gives real-world performance insights. In contrast, theoretical analysis uses mathematical techniques to evaluate an algorithm abstractly without implementation, focusing on worst-case, average-case, and best-case scenarios. While empirical analysis reflects practical performance, theoretical analysis provides a general understanding of an algorithm's efficiency and scalability.

Theoretical analysis of an algorithm involves evaluating its computational resources, such as time and space, as a function of input size. This is done without actual implementation, often using mathematical tools to assess performance and determine asymptotic behavior in terms of Big-O notation. It's important because it allows computer scientists to predict the performance and scalability of algorithms, choose the most efficient ones for specific tasks, and identify potential bottlenecks before deployment. This foresight is crucial in designing systems that require high efficiency and quick response times.

Bubble sort repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. This process is repeated for each element until the list is sorted. It is considered inefficient because it has a worst-case and average time complexity of O(n^2), suitable only for small datasets with simple iteration, causing excessive comparisons and swaps. Its inefficiency becomes noticeable in large datasets where more efficient sorting algorithms like quicksort or mergesort are preferred.

You might also like