Assignment 1.
Assignment 1.
20023002361
Assignment 1
ADT stands for Abstract Data Type. It defines a data type by its values and operations, without
specifying its implementation.
(it means that adt tells you what kind of data it can hold(numbers, letters) and what you can
do with the data (add, remove, search)
________________________________________________________________________________________________________
2Q) Give the names of linear types of data structures and nonlinear data structures
Linear:
Array
Linked List
Stack
Queue
Non-Linear:
________________________________________________________________________________________________________
An array is a collection of items of the same data type stored in contiguous memory locations
________________________________________________________________________________________________________
Static Arrays: Static arrays are data structures with a fixed size that is determined at compile
time(size of the array is decided when the code is compiled). Once defined, the size of a
static array cannot be altered
Dynamic Arrays: Dynamic arrays allow for variable sizes that can be determined at runtime
(size of the array can be adjusted while the program is executing, you can remove or add
elements). This flexibility makes them suitable for applications where the number of elements
may change frequently.
________________________________________________________________________________________________________
Overflow
Overflow occurs when you try to insert an element into a data structure that is already
full. There is no more space available to accommodate the new element.
Stacks: If you try to push an element onto a stack that has reached its maximum
capacity, an overflow occurs.
Queues: If you try to enqueue an element into a full queue, an overflow occurs.
Arrays: If you try to insert an element beyond the declared size of an array, it can lead
to an overflow (writing to memory outside the array's bounds).
Underflow
Underflow occurs when you try to remove an element from a data structure that is
already empty. There are no elements to remove.
Stacks: If you try to pop an element from an empty stack, an underflow occurs.
Queues: If you try to dequeue an element from an empty queue, an underflow occurs.
________________________________________________________________________________________________________
6Q) What are the asymptotic symbols? Which symbol is used to show worst case, best case
and average case complexity?
Asymptotic Symbols
These symbols are used to describe the growth rate of functions (like the time or space an
algorithm takes) as the input size approaches infinity. The main ones are:
Big O notation (O): Describes the upper bound or worst-case complexity. It represents
the maximum amount of time or space an algorithm will take.
Omega notation (Ω): Describes the lower bound or best-case complexity. It represents
the minimum amount of time or space an algorithm will take.
Theta notation (Θ): Describes the tight bound or average-case complexity (in some
situations). It represents both the upper and lower bounds, meaning the algorithm's
performance is within a specific range.
________________________________________________________________________________________________________
________________________________________________________________________________________________________
8Q) What is worst case, best case and average case complexity of linear search ?
In the worst-case scenario, the element being searched for is either not present in the list
or located at the last position. This means that every element must be checked, resulting
in a time complexity of O(n)O(n), where n is the number of elements in the list
The best-case scenario occurs when the target element is found at the first position of
the list. In this case, only one comparison is needed, leading to a time complexity of
O(1)O(1)
The average case assumes that the target element could be located anywhere in the list.
On average, it would take about half of the elements to be checked, which results in a
time complexity of O(n)O(n), similar to the worst case
________________________________________________________________________________________________________
________________________________________________________________________________________________________
Sorted Data Structure: The array or list must be sorted in either ascending or descending
order
Random Access Capability: The data structure should allow for constant-time access to
its elements, which is characteristic of arrays. This means you can directly access any
element using its index.
________________________________________________________________________________________________________
11Q) Which search is also called as sequential search?
The search method that is also called sequential search is known as linear search.
________________________________________________________________________________________________________
12Q) What is double ended queue ? whats its condition of full and empty?
A double-ended queue (deque) is a data structure that allows insertion and deletion of
elements from both the front and rear ends.
________________________________________________________________________________________________________
13Q) What are conditions of full and empty for two stacks in one array ?
________________________________________________________________________________________________________
1. Insertion
2. Deletion
3. Search
4. Traversal
5. Sorting
6. Merging
7. Reversal
8. Indexing
________________________________________________________________________________________________________
________________________________________________________________________________________________________
A pointer is a variable that stores the memory address of another variable. It allows direct
access to memory locations, enabling efficient manipulation of data structures and dynamic
memory management.
________________________________________________________________________________________________________
A priority queue is a specialized type of queue in which each element is associated with a
priority value. In a priority queue, elements with higher priority are served before those with
lower priority. If two elements have the same priority, they are served according to their
order in the queue
________________________________________________________________________________________________________
Yes, heaps are an excellent and common way to implement priority queues. In fact, they are
often the preferred implementation due to their efficiency.
________________________________________________________________________________________________________
int a[4]={7,8,22,4};
int a={7,8,22,4}
int a()={7,8,22,4}
int a[n]={7,8,22,4}
int a = {7,8,22,4}; is invalid because it does not specify an array size or use square
brackets.
int a() = {7,8,22,4}; is invalid as it attempts to declare a function instead of an array.
int a[n] = {7,8,22,4}; is valid only if n is defined as a constant expression at compile time.
________________________________________________________________________________________________________
void display() {
if(i % 2 == 0) {
sum += a[i]; }
printf(“%d”,sum);
________________________________________________________________________________________________________
void showstring() {
int n = s.size();
} printf(“%s”,s)
A) O(n)
________________________________________________________________________________________________________