Assignment Advanced Data Structure and Algorithm: Group A
Assignment Advanced Data Structure and Algorithm: Group A
Group A
❑ State the characteristics of an algorithm.
The word Algorithm means “a process or set of rules to be followed in
calculations or other problem-solving operations”. Therefore Algorithm
refers to a set of rules/instructions that step -by-step define how a work is
to be executed upon in order to get the expected results. An algorithm
should have the following characteristics –
Clear and Unambiguous: Algorithm should be clear and unambiguous. Each of its
steps should be clear in all aspects and must lead to only one meaning.
▪ Well-Defined Inputs: If an algorithm says to take inputs, it should be well-
defined inputs.
▪ Well-Defined Outputs: The algorithm must clearly define what output will be
yielded and it should be well-defined as well.
▪ Finite-ness: The algorithm must be finite, i.e. it should not end up in an infinite
loops or similar.
▪ Feasible: The algorithm must be simple, generic and practical, such that it can
be executed upon will the available resources. It must not contain some future
technology, or anything.
Merge Sort is quite fast, and has a time complexity of O(n*log n). It is also a
stable sort, which means the "equal" elements are ordered in the same order in
the sorted list. Hence the total time for mergeSort function will become n(log n
+ 1), which gives us a time complexity of O(n*log n).
• It requires equal amount of additional space as the unsorted array. Hence its
not at all recommended for searching large unsorted arrays.
• It is the best Sorting technique used for sorting Linked Lists.
#include<stdio.h>
int fibonacci(int);
void main ()
{ int
n,f;
printf("Enter the value of
n?"); scanf("%d",&n); f =
fibonacci(n); printf("%d",f);
}
int fibonacci (int n)
{
if (n==0)
{
return 0;
}
Output else if (n == 1)
Enter the value of n?
12
4
return 1;
❑ State the basic difference between a tree and a graph.
structure
❑ Define complete binary tree.
A complete binary tree is a binary tree where each level ‘l’ except the
last has 2^l nodes and the nodes at the last level are all left aligned.
Complete binary trees are mainly used in heap based data
structures.
The nodes in the complete binary tree are inserted from left to right in
one level at a time.
If a level is full, the node is inserted in a new level.Below are some of the
complete binary trees.
b c
Group B
Binary_Search(a,key,lb,ub) begin
Step 1: [initialization]
lb=0
ub=n-1;
Step 2: [search for the item] Repeat through step 4 while lower bound(lb)
is less than upper bound.
Its examples are: array, stack, While its examples are: trees and
6. queue, linked list, etc. graphs.
Pop operation is used to remove an item from stack, first get the
element and then decrease TOP pointer.
POP_STACK(STACK,TO
P,ITEM)
Algorithm to pop an
element from stack.
1) IF TOP = 0 then
Print “Stack is
empty”;
Exit;
2) Otherwise
ITEM: =STACK
(TOP);
TOP:=TOP – 1;
3) End of IF
4) Exit
IS_FULL(STACK,TOP,M
AX,STATUS)
Algorithm to check
stack is full or not.
STATUS contains the
result status.
IS_EMPTY(STACK,TOP,
MAX,STATUS)
Algorithm to check
stack is empty or not.
STATUS contains the
result status.
1) IF TOP = 0 then
STATUS:=true;
2) Otherwise
STATUS:=false;
3) End of IF
4) Exit
Preorder = D B A E F C ( Root,Left,Right )
B E
A C F
Postorder=A,B,C,F,E,D
• Arrays are bound by the limitation of their size. You have to specify
the number of elements in the array beforehand. Whereas in the
linked list you can add any number of elements.
• Suppose you have to insert any element at the beginning in an
array then you will have to shift all the elements one place right to
make room for the new element. This operation is expensive as it
takes O(n) time. Whereas you can insert any element at the
beginning in the linked list in O(1) time. Same is the case with
deletion of elements.
Disadvantage s of linked list over arrays:
• If the array is sorted you can apply binary search to search any
element. But even if linked list is sorted you cannot apply binary
search and so the complexity of searching in linked list is O(n).
• Extra memory space is required for the pointer with each element in
the linked list.
• Arrays have better cache locality than linked list which can greatly
• Step 1 - Consider the first element of the list as pivot (i.e., Element
at first position in the list).
• Step 2 - Define two variables i and j. Set i and j to first and last
elements of the list respectively.
The process to add an element into queue is called Enqueue and the
process of removal of an element from queue is called Dequeue.
If the value of front is less than MIN or 0, it tells that the queue is
not yet initialized, hence empty.
Algorithm-
Step 1: IF REAR = MAX - 1
Write OVERFLOW
Go to step
[END OF IF]
Step 2: IF FRONT = -1 and REAR = -1
SET FRONT = REAR = 0
ELSE
SET REAR = REAR + 1
[END OF IF]
Step 3: Set QUEUE[REAR] = NUM
Step 4: EXIT
iv). delete an element from the queue :-
If, the value of front is -1 or value of front is greater than rear , write an
underflow message and exit.
Otherwise, keep increasing the value of front and return the item stored at
the front end of the queue at each time.
Algorithm
Step 1: IF FRONT = -1 or FRONT > REAR
Write UNDERFLOW
ELSE
SET VAL = QUEUE[FRONT]
SET FRONT = FRONT + 1
[END OF IF]
Step 2: EXIT