0% found this document useful (0 votes)
0 views

Introduction of Data Structure Final Template[1]

The document provides a comprehensive introduction to data structures, defining key concepts such as data, records, and files, and explaining the need for data structures in organizing and managing data efficiently. It classifies data structures into linear and non-linear types, detailing specific structures like arrays, linked lists, stacks, queues, trees, and graphs, along with their applications. Additionally, it covers algorithm analysis, sorting techniques, and asymptotic notations to evaluate algorithm performance.

Uploaded by

Faiz Dosani
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Introduction of Data Structure Final Template[1]

The document provides a comprehensive introduction to data structures, defining key concepts such as data, records, and files, and explaining the need for data structures in organizing and managing data efficiently. It classifies data structures into linear and non-linear types, detailing specific structures like arrays, linked lists, stacks, queues, trees, and graphs, along with their applications. Additionally, it covers algorithm analysis, sorting techniques, and asymptotic notations to evaluate algorithm performance.

Uploaded by

Faiz Dosani
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 73

Introduction to Data Structure

GENERAL CONCEPTS OF DATA STRUCTURE

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

Algorithm + Data Structure = Program


Basic Terminology

1] Data: Data can be defined as an elementary value or the collection of values.


for example, student’s Name and its id are the data about the student.

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.

4] File: A File is a collection of various records of one type of entity.


for example, if there are 60 employees in the class, then there will be 20 records in the related file
where each record contains the data about each employee.
5] Attribute and Entity: An entity represents the class of certain objects. it contains
various attributes. Each attribute represents the particular property of that entity.

6] Field: Field is a single elementary unit of information representing the attribute of an


entity

Need for Data Structure

1] it gives different level of organization data.


2] It tells how data can be stored and accessed in its elementary level.
3] Provide operation on group of data, such as adding an item, looking up highest
priority item.
4] Provide a means to manage huge amount of data efficiently.
CLASSIFICATION OF DATA STRUCTURES
Types of Data Structure with its properties and
operations
Types of Data Structure
• Linear data structure
• Non-linear data structure

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.

• Dynamic data structure: In the dynamic data structure, the size is


not fixed. It can be randomly updated during the runtime which may
be considered efficient concerning the memory (space) complexity
of the code. Example: stack and queue data structures.
Array

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.

Examples: tree and graph data structures.


Tree

• 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.

• Asymptotic Notations are mathematical tools used to analyse the performance of


algorithms by understanding how their efficiency changes as the input size grows.
Big – O (O)
Definition
• ‘O’ is the representation for the Big-O Notation .
• Big-O is the method used to express thee Upper Bound of the running time of an algorithm .
• It Returns the highest possible output value(Big-O) for a given input .

• Therefore, It gives the worst-case complexity of an algorithm .


Big-O (O)

• Big-O is defined as:

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 (Ω)

Omega is defined as:

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 (θ)

Theta is defined as:

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)

Advantages of Selection Sort


• Easy to understand and implement, making it ideal for teaching basic sorting
concepts.
• Requires only a constant O(1) extra memory space.

Disadvantages of the Selection Sort


• Not efficient for large datasets (time complexity).
• Does not adapt to already sorted data.
Application of selection sort algorithm:

• 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.

Disadvantages of Insertion sort:


• Inefficient for large lists.
• Not as efficient as other sorting algorithms (e.g., merge sort, quick sort) for most cases.
Heap Sort
Definition
• Heap sort is a sorting algorithm that uses a heap data structure to arrange elements in order.
• It first builds a heap , then repeatedly removes the largest (or smallest) element and places it in
the sorted part of the array.
Heap Sort Algorithm
• Step 1 : Create a Max Heap function. This function will change the given array data to the max
heap.
• Step 2 : Swap the root node(the largest element) with the last element. Then heapify the root
node of the current tree to maintain the property of Max Heap.
• Step 3 : Repeat Step 2 until the max heap becomes empty. Place the element in the correct
position.
• Step 4 : Repeat Step 2 and Step 3 until every element of the array is the correct sport.
• Step 5 : End
Time Complexity Graph for Heap Sort:-
Detailed Working of Heap Sort:
Consider an array ->

15 20 7 9 30

Step 1: Treat the Array as a Complete Binary Tree

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

Finaly the array become ->


7 9 15 20 30

As now array is sorted no swap will occur


7 9 15 20 30
So the final sorted array is:-

In this method , The time complexity is


O(nlog n)
• Best case
• Average case
• Worst case
Heapify Method Step 1 :Compare the value at the
current node(index i) with its left child
• Building of max heap will take only O(n) time . (indexed at 2*i + 1) and its right
child(indexed at 2*i + 2), if they exist
• O(n) time is better than O(n log n).
• Start heapify method from non-leaf element which Step 2 :Find the largest of current
node, left child and right child.
is at highest index.
• Leaf node start from from ⌊⌋ +1 to n.
Step 3 :Swap the largest of all
• 1,17,10,30 is a leaf node with the current node( if needed).

• 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 Step 4 :If a swap was done, make sure


the subtree that was rooted at the
20
index where the highest value was
5
1 2 3 4 5 6 7

15 5 20 1 17 10 30
1 17 10 30
Algorithm of Heapify method :

Step 1 :Compare the value at the current node(index i) with its


left child (indexed at 2*i + 1) and its right child(indexed at 2*i +
2), if they exist

Step 2 :Find the largest of current node, left child and right
child.

Step 3 :Swap the largest of all with the current


node( if needed).

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

Step 4 :If a swap was done, make sure the


subtree that was rooted at the index where
the highest value was
15
15

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

Now the array becomes -> After this we do a deletion of element


And after deletion the array will be
15 5 20 1 17 10 30
30 20 17 10 15 5 1
Pseudo code for
heapify method:-
Heap sort is considered an unstable sorting algorithm because it
does not guarantee the relative order of equal elements, meaning
that when sorting a list with duplicate values, the original order of
those duplicates may be changed during the heap operations,
resulting in an altered sequence in the final sorted output.
SEARCHING
Definition:
• Searching is the process of locating given value position in a list of values, i.e. search
is a process of finding a value in a list of values.
• Search is said to be successful if the element being searched is found or unsuccessful
when the element being searched is not found.
• A search typically answers in either True or False as to whether the item is present.
Two techniques are used for searching:
• Linear/ Sequential Search.
• Binary Search.
Linear Search Algorithm
• In Linear search, a sequential search is made over all elements one by one. In other
words linear search searches an element or value from an array till the desired
element is not found in sequential order.

• 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

for(i = 0; i < size; i++)


{
If(a[i]==data/key){
printf("Element is found at %d
index", i);
break;
}
}
if(i== size){
Printf("Given element is not found in the
list!!!");
}
Example
For example : consider the array arr[]={10,50,30,70,80,60,20,90,40} and key = 30
Time Complexity of Linear Search Algorithm:
• Best Case : In the best case, the key might be present at the first index. So the best case
complexity is O(1)
• Worst Case : In the worst case, the key might be present at the last index i.e., opposite to
the end from which the search has started in the list. So the worst-case complexity is
O(N) where N is the size of the list.
• Average Case : O(N)

Applications of Linear Search Algorithm:


Unsorted Lists : When we have an unsorted array or list, linear search is most commonly
used to find any element in the collection.

•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.

• Does not require any additional memory.

• It is a well-suited algorithm for small datasets.

Disadvantages of Linear Search Algorithm:


• Linear search has a time complexity of O(N), which in turn makes it slow for large datasets.

• Not suitable for large arrays.


Binary Search
• Definition
• Binary search is the most popular Search algorithm based on Divide and conquer
technique. It works on a sorted array list either in ascending or descending order.
• In this method firstly find the middle element and to search an element compare the
value with the elements in the middle position of the array.
• If the value is matched, then search is successful otherwise list is divided into two
halves one from the first element to the middle element i.e. first half or lower half and
second half or upper half from middle to last element
• If the value is less than the middle element, then it must lie in the lower half of the
array and if it's greater than the element then it must lie in the upper half of the array.
We repeat this procedure on the lower (or upper) half of the array.
• Binary Search is useful when there are large numbers of elements in an array.
STEPS / ALGORITHM
Let X be the element to be searched in an array A[0 – (n-1)] .
• Select the element which have to be searched
• Find the middle value of an array using formula:
mid= (low + high)/2.
Here low refers to lower bound of an array i.e. 0 and high refers to upper bound of an array i.e. n-
1.
• If the element to be searched is the middle element then the search terminates otherwise.
• If the element to be searched is less than the middle element then the search is performed in left
sub array and high is set to mid-1.
• Otherwise, if the element to be searched is greater than the middle element then the search is
performed in right sub array and low is set to mid+1. In this way the problem size is reduced to half
after every comparison.
• Repeat the same process until we find the search element in the list or until sublist contains only one
element.
• If that element also doesn't match with the search element, then display "Element not found in the
list!" and terminate the function.
BINARY SEARCH LOGIC
while (low <= high)
{
int mid = (low + high)/ 2;
if (arr[mid] == key) // Check if key is at mid
{
return mid;
}
else if (arr[mid] >key) // If key is smaller, ignore right half
{
high = mid - 1;
}
// If key is larger, ignore left half
else
{
low = mid + 1;
}
}
// Key not found
return -1;
}
Example

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)

Advantages of Binary Search


• Binary search is faster than linear search, especially for large arrays.
• Binary search is well-suited for searching large datasets that are stored in external memory,
such as on a hard drive or in the cloud.

Disadvantages of Binary Search


• The array should be sorted.
• Binary search requires that the data structure being searched be stored in contiguous memory
locations.

You might also like