CSC202 DataStructureANDAgorithm HO
CSC202 DataStructureANDAgorithm HO
A data structure is a way of organizing the data so that the data can be used efficiently, i.e. it is
concerned with the efficient allocation and manipulation of data. Data structure is a way of defining,
storing & retrieving of data in a structural & systematic way. Data structure is a format for
storing data in a structured manner. It is not a separate programming language. It is just an
implementation method and can be implemented using any one of the programming language
like C, C++, Java, etc. For example, data like photos, videos are stored in gallery with the help
of a data structure.
Different kinds of data structures are suited to different kinds of applications, and some are highly
specialized to specific tasks. For example, B-trees are particularly well-suited for implementation
of databases, while compiler implementations usually use hash tables to look up identifiers.
1
Advantages of a Linked list over an array
Consider a scenario, where we need to store large amount of data in an array. But, the memory
to store that data is not available contiguously. In this case we cannot use array. Hence, we go for
a linked list. Since each node is connected using link, it is not necessary that memory has to be
contiguous. In addition, some of the major differences between a Linked List and an array are
given below.
Arrays Linked List
Array elements can be accessed randomly Random accessing is not possible in linked
using the array index. lists. The elements will have to be accessed
sequentially.
Data elements are stored in contiguous New elements can be stored anywhere and a
locations in memory. reference is created for the new element using
pointers.
STACK
Stack is a linear data structure which follows the order LIFO (Last In First Out) or FILO (First In
Last Out) for accessing elements. Basic operations of stack are Push, Pop, and Peek
The below operations can be performed on a stack −
push() − adds an item to stack
pop() − removes the top stack item
peek() − gives value of top item without removing it
isempty() − checks if stack is empty
isfull() − checks if stack is full
Applications of Stack:
1. Infix to Postfix Conversion using Stack
2. Evaluation of Postfix Expression
3. Reverse a String using Stack
4. Implement two stacks in an array
5. Check for balanced parentheses in an expression
QUEUE
Queue is a linear structure, which follows the order, is First In First Out (FIFO) to access
elements. Mainly the following are basic operations on queue: Enqueue, Dequeue, Front, and
Rear.
2
The difference between stacks and queues is in removing. In a stack, we remove the item the most
recently added; in a queue, we remove the item the least recently added. Both Queues and Stacks
can be implemented using Arrays and Linked Lists.
Linked List
A linked list is a linear data structure (like arrays) where each element is a separate object. Each
element (that is node) of a list is comprising of two items – the data and a reference to the next
node.
Types of Linked List :
1. Singly Linked List: In this type of linked list, every node stores address or reference of
next node in list and the last node has next address or reference as NULL. For example 1-
>2->3->4->NULL
2. Doubly Linked List: Here, here are two references associated with each node, one of the
reference points to the next node and one to the previous node. E.g. NULL<-1<->2<->3-
>NULL
3. Circular Linked List: Circular linked list is a linked list where all nodes are connected to
form a circle. There is no NULL at the end. A circular linked list can be a singly circular
linked list or doubly circular linked list. E.g. 1->2->3->1 [The next pointer of last node is
pointing to the first]
Data Structure used for implementing Least Recently Used (LRU) cache
We use two data structures to implement an LRU Cache.
1. Queue , which is implemented using a doubly linked list. The maximum size of the queue
will be equal to the total number of frames available (cache size).The most recently used
pages will be near rear end and least recently pages will be near front end.
2. A Hash with page number as key and address of the corresponding queue node as value.
Heap
A heap can be thought of as a simple tree data structure, however a heap usually employs one of
two strategies:
3
1. min heap; or
2. max heap
Each strategy determines the properties of the tree and its values. If you were to choose the min
heap strategy then each parent node would have a value that is ≤ than its children. For example,
the node at the root of the tree will have the smallest value in the tree. The opposite is true for the
max heap strategy.
Unlike other tree data structures, a heap is generally implemented as an array rather than a series
of nodes, which each have references to other nodes. The nodes are conceptually the same,
however, having at most two children.
ALGORITHM
Algorithm is a step by step procedure, which defines a set of instructions to be executed in certain
order to get the desired output.
ALGORITHM ANALYSIS
A problem can be solved in more than one ways. Therefore, many solution algorithms can be
derived for a given problem. We analyze available algorithms to find and implement the best
suitable algorithm.
Binary Search
27
A binary search works only on sorted lists or arrays. This search selects from the middle which
splits the entire list into two parts. First the middle is compared.
This search first compares the target value to the mid of the list. If it is not found, then it takes
decision on whether.
BUBBLE SORT
Bubble sort is comparison based algorithm in which each pair of adjacent elements is compared
and elements are swapped if they are not in order. Because the time complexity is Ο(n2), it is not
suitable for large set of data.
INSERTION SORT
Insertion sort divides the list into two sub-list, sorted and unsorted. It takes one element at a time
and finds its appropriate location in sorted sub-list and insert there. The output after insertion is a
sorted sub-list. It iteratively works on all the elements of unsorted sub-list and inserts them to
sorted sub-list in order.
SELECTION SORT
Selection sort is in-place sorting technique. It divides the data set into two sub-lists: sorted and
unsorted. Then it selects the minimum element from unsorted sub-list and places it into the sorted
list. This iterates unless all the elements from unsorted sub-list are consumed into sorted sub-list.
MERGE SORT
Merge sort is sorting algorithm based on divide and conquer programming approach. It keeps on
dividing the list into smaller sub-list until all sub-list has only 1 element. Then it merges them in
a sorted way until all sub-lists are consumed. It has run-time complexity of Ο(n log n) and it needs
Ο(n) auxiliary space.
SHELL SORT
Shell sort can be said to be a variant of insertion sort. Shell sort divides the list into smaller sublist
based on some gap variable and then each sub-list is sorted using insertion sort. In best cases, it
can perform up to Ο(n log n).
QUICK SORT
Quick sort uses divide and conquer approach. It divides the list in smaller 'partitions' using 'pivot'.
The values which are smaller than the pivot are arranged in the left partition and greater values are
arranged in the right partition. Each partition is recursively sorted using quick sort.
Kruskal's algorithm
28
This algorithm treats the graph as a forest and every node it as an individual tree. A tree connects
to another only and only if it has least cost among all available options and does not violate
minimum spanning tree MST properties.
Prim's algorithm
Prim's algorithm treats the nodes as a single tree and keeps on adding new nodes to the spanning
tree from the given graph.
GRAPH
A graph is a pictorial representation of a set of objects where some pairs of objects are connected
by links. The interconnected objects are represented by points termed as vertices, and the links that
connect the vertices are called edges.
TREE
A tree is a minimally connected graph having no loops and circuits.
SPANNING TREE
A spanning tree is a subset of Graph G, which has all the vertices covered with minimum possible
number of edges. A spanning tree does not have cycles and it cannot be disconnected.
How many spanning trees can a graph has?
It depends on how connected the graph is. A complete undirected graph can have maximum nn-1
number of spanning trees, where n is number of nodes.
BINARY TREE
A binary tree has a special condition that each node can have two children at maximum.
RECURSIVE FUNCTION
A recursive function is one, which calls itself, directly or calls a function that in turn calls it. Every
recursive function follows the recursive properties − base criteria where functions stops calling
itself and progressive approach where the functions tries to meet the base criteria in each iteration.
TOWER of HANOI
Tower of Hanoi is a mathematical puzzle, which consists of three tower (pegs) and more than one
rings. All rings are of different size and stacked upon each other where the large disk is always
below the small disk. The aim is to move the tower of disk from one peg to another, without
breaking its properties.
HASHING
Hashing is a technique to convert a range of key values into a range of indexes of an array. By
using hash tables, we can create an associative data storage where data index can be find by
providing its key values.
30
Due to the above limitations, references in C++ cannot be used for implementing data structures
like Linked List, Tree, etc. In Java, references do not have above restrictions, and can be used to
implement all data structures. References being more powerful in Java is the main reason Java
does not need pointers.
References are safer and easier to use:
1) Safer: Since references must be initialized, wild references like wild pointers are unlikely to
exist. It is still possible to have references that do not refer to a valid location
2) Easier to use: References do not need dereferencing operator to access the value. They can be
used like normal variables. ‘&’ operator is needed only at the time of declaration. In addition,
members of an object reference can be accessed with dot operator (‘.’), unlike pointers where arrow
operator (->) is needed to access members.
31