Introduction of Data Structure Final Template[1]
Introduction of Data Structure Final Template[1]
Data structure is the structural representation of logical relationship between data elements.
This means that a data structure organizes data items based on the relationship between the data
elements.
Example: A house can be identified by the house name, location, number of floors and so on.
These structured set of variables depend on each other to identify the exact house. Similarly,
data structure is a structured set of variables that are linked to each other, which forms the basic
component of a system
2] Group Items: Data items which have subordinate data items are called Group item.
for example, name of a student can have first name and the last name .
3] Record: Record can be defined as the collection of various data items, for example, if we talk
about the student entity, then its name, address, course and marks can be grouped together to
form the record for the student.
Linear data structure: Data structure in which data elements are arranged sequentially or
linearly, where each element is attached to its previous and next adjacent elements, is called a
linear data structure.
Non-linear data structure: Data structures where data elements are not placed sequentially or
linearly are called non-linear data structures. In a non-linear data structure, we can’t traverse all the
elements in a single run.
Linear data structure
• Static data structure: Static data structure has a fixed memory size.
It is easier to access the elements in a static data structure.
Example: array data structure.
An array is a linear data structure and it is a collection of element of same data type stored
at contiguous memory locations.
Applications
• It helps in implementing sorting algorithm.
• It is also used to implement other data structures like Stacks, Queues, Heaps, Hash tables, etc.
Linked list
• A linked list is a linear data structure in which elements are not stored at contiguous memory
locations. The elements in a linked list are linked using pointers as shown in the below image.
Applications
• Linked lists are used to implement other data structures like stacks, queues, etc.
• They are used to perform undo operations.
Stack
• Stack is a linear data structure that follows LIFO(Last in first out) principle i.e., entering and
retrieving data is possible from only one end. The entering and retrieving of data is also called
push and pop operation in a stack.
Applications
• It is used for parenthesis checking and string reversal.
• The stack data structure is used in the evaluation and conversion of arithmetic expressions.
Queue
• Queue is a linear data structure that follows First In First Out(FIFO) principle i.e. the data
item stored first will be accessed first. In this, entering is done from one end and retrieving data
is done from other end.
Application
• It helps to maintain the playlist in media players.
• Queues are used for job scheduling in the operating system .
Non-linear data structure
• Data structures where data elements are not placed sequentially or
linearly are called non-linear data structures. In a non-linear data
structure, we can’t traverse all the elements in a single run.
• A tree is a non-linear and hierarchical data structure where the elements are arranged in a tree-
like structure. In a tree, the topmost node is called the root node. Each node contains some data,
and data can be of any type. It consists of a central node, structural nodes, and sub-nodes which
are connected via edges.
Application
• B-Tree and B+ Tree are used to implement indexing in databases.
• Domain Name Server also uses a tree data structure.
Graph
• A graph is a non-linear data structure that consists of vertices (or nodes) and edges. It consists
of a finite set of vertices and set of edges that connect a pair of nodes.
Application
• The operating system uses Resource Allocation Graph.
• Also used in the World Wide Web where the web pages represent the nodes.
Best and Worst case analysis
Definition
• Best and worst-case analysis is a method used in algorithm analysis , business decision-making
, and risk assessment to evaluate the most optimistic (best-case) and most pessimistic (worst-
case) outcomes.
1.Algorithm Analysis
• Best and worst-case analysis is commonly used in computer science to evaluate an algorithm’s
efficiency.
• Best-case complexity (Ω notation): The minimum time an algorithm takes for an input .
• Example : In binary search , the best case occurs when the target element is found in the first
comparison , leading to O(1) complexity.
• Worst-Case Complexity (O notation): The maximum time an
algorithm takes for an input.
• Example: In bubble sort, the worst case happens when the array is completely unsorted , leading
to O(n²) complexity
• Best-Case Scenario: Data is properly normalized, indexed, and stored efficiently.
• Query execution and updates are fast.
• Worst-Case Scenario: Unnormalized tables with redundant data cause high storage us and slow
queries.
• Example: Storing the same customer details in multiple tables leads to da inconsistency
Conclusion
• Best-case performance is achieved using proper indexing, optimized queries, and well-
structured transactions.
• Worst-case scenarios occur due to full table scans, poor indexing, deadlocks, and inefficient
joins
Asymptotic Notations
Definition
• A problem may have various algorithmic solutions , In order to choose the best algorithm
for a particular process you must be able to judge the time taken to run a particular
solution.
F(n)C*G(n)
Omega (Ω)
Definition
• ‘Ω’ is the representation for omega notation .
• Omega is the method used to express the lower bound of the running time of an algorithm .
• It is defined as the condition that allows an algorithm to complete statement execution in the
shortest amount of time.
• Thus , It provides the best case complexity of an algorithm.
Omega (Ω)
F(n)C*G(n)
Theta(θ)
Definition
• ‘θ’ is the representation for theta notation.
• Omega is used when the upper bound and the lower bound of an algorithm are in the same
order of magnitude.
• Since , It represents the upper and the lower bound of the running time of an algorithm.
• It is used for analyzing the average-case complexity of an algorithm.
Theta (θ)
C1*G(n)F(n)C2*G(n)
Sorting
Definition
• Sorting refers to rearrangement of a given array or list of elements according to a
comparison operator on the elements.
• The comparison operator is used to decide the new order of elements in the
respective data structure.
Two techniques are used for sorting
• Selection Sorting
• Insertion Sorting
• Heap Sorting
• Shell Sorting
SELECTION SORT
Definition:
• Selection Sort is a comparison-based sorting algorithm
• It is a simple algorithm that organizes a list of items by repeatedly finding the smallest element in
the unsorted part and swapping it with the first unsorted element.
• It's a simple, efficient algorithm that's useful for small datasets.
Steps:
•First we find the smallest element and swap it with the first element. This way we get the smallest
element at its correct position.
•Then we find the smallest among remaining elements (or second smallest) and swap it with the second
element.
•We keep doing this until we get all elements moved to correct position.
How insertion sort works:
Program:
Output:
Time Complexity
• Best Case (Already Sorted):
• Worst Case (Reverse Sorted):
• Average Case:
Space complexity
• O(1) (constant space)
• With the help of this sorting algorithm, we can sort the list of student by their grades or names in
a small class.
• We can also organize the files by their creation date or size in a directory.
• Also, with the help of this algorithm, we can sort the deck of cards in ascending or descending
order.
• We can also able to arrange the list of items by price in a small e-commerce store.
INSERTION SORT
• Definition:
• Insertion sort is one of the simple and comparison-based sorting algorithms.
• The basic idea behind the algorithm is to virtually divide the given list into two
parts: a sorted part and an unsorted part, then pick an element from the unsorted
part and insert it in its place in the sorted part .
• It does this till all the elements are placed in the sorted part.
How insertion sort works:
Program:
Output:
Time Complexity:
• Best case : O(n)
• Average case :O
• Worst case : O(1)
Space Complexity:
• O(1)
Advantages of Insertion Sort:
• Simple and easy to implement.
• Stable sorting algorithm.
• Efficient for small lists and nearly sorted lists.
• Space-efficient as it is an in-place algorithm.
• Adoptive, the number of inversions is directly proportional to number of swaps. For example,
no swapping happens for a sorted array and it takes O(n) time only.
15 20 7 9 30
15
NON-LEAF
20 7
9 30
LEAF
Step 2: Built a max or min heap
30>20 30>15
15 15 So swap 30 20>15
So swap
So swap
7 30 7 15 7
20
9 30 9 20 9 20
30
7
30 20 7 9 15
20
9 15
Step 3: remove the max element
30 15 20
As 20 > 15
20 7 20 7 15 7
20 7 swap
9 15 9 15 9
9
20 15 7 9 30
15 20 7 9 30
30 20 7 9 15
9
15 9
15 > 9
7 So swap 15 7
9 7 15 7
9
15 7 9 20 30
9 15 7 20 30
15 7 9 20 30
7 9>7 9
So swap
7
9 7
7
• We start heapify methode from Step 4 :If a swap was done, make sure
the subtree that was rooted at the
index where the highest value was
found also meet the max heap
property by recursively applying the
heapify() to it
15 5 20 1 17 10 30
1 17 10 30
Algorithm of Heapify method :
Step 2 :Find the largest of current node, left child and right
child.
20
5
30
5
1 17 10 30
1 17 10 20
15 30
15
30 20
17 17
20
17
1 5 10 20 1 5 10 15
1 5 10 30
• Every element is checked and if it is found then that particular item is returned,
otherwise the search continues till the end of the data collection.
• Searching begins from first element and continues until the desired element is found
or end of the file is reached.
• Linear Search is applied on the unsorted or unordered list when there are fewer
elements in a list.
ALGORITHM/STEPS :
• Read the search element which is to be searched
• Initially Compare, the search element with the first element in the
list. If both are matching, then display "element found" and
terminate the function
• If both are not matching, then compare search element with the
next element in the list.
• Repeat steps until the search element is compared with the last
element in the list.
• If the last element in the list is also doesn't match, then display
"Element not found" and terminate the function.
Linear Search Logic
•Small Data Sets : Linear Search is preferred over binary search when we have small data
sets.
Advantages of Linear Search Algorithm:
• Linear search can be used irrespective of whether the array is sorted or not. It can be used on arrays
of any data type.
Consider an array arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91}, and the target = 23.
Time Complexity:
• Best Case: O(1)
• Average Case: O(log N)
• Worst Case: O(log N)