0% found this document useful (0 votes)
5 views23 pages

Data Structures-1

The document outlines a syllabus for data structures, covering basic terminology, types of data structures, sorting and searching algorithms, and their applications. It explains various data structures such as arrays, linked lists, stacks, queues, trees, and graphs, along with their characteristics and operations. Additionally, it discusses algorithms, control structures, and searching techniques, including linear and binary search.

Uploaded by

archana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views23 pages

Data Structures-1

The document outlines a syllabus for data structures, covering basic terminology, types of data structures, sorting and searching algorithms, and their applications. It explains various data structures such as arrays, linked lists, stacks, queues, trees, and graphs, along with their characteristics and operations. Additionally, it discusses algorithms, control structures, and searching techniques, including linear and binary search.

Uploaded by

archana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

14-05-2025

Syllabus

Basic Terminology – data, information, datatype; Data Structures –


Introduction, storage structures- sequential and linked storage
representations; classification of data structures; Applications of data
DATA STRUCTURES structures.

Sorting - Selection Sort, Bubble Sort, Insertion Sort, Quick Sort and
Merge Sort.

Searching - Linear Search and Binary Search.

Introduction To run efficient programs

• A good program is defined as a program that • Data collection,


1. runs correctly • Organization of data into appropriate structures,
2. is easy to read and understand
• Developing and maintaining routines for quality assurance.
3. is easy to debug and
4. is easy to modify.

1
14-05-2025

Data and information Data Structures - Definition

• Data consists of raw, unprocessed facts and figures collected through • A data structure is basically a group of data elements that are put
observations, experiments, or measurements. together under one name, and which defines a particular way of
• Data that has been processed, organized, and structured to mean storing and organizing data in a computer so that it can be used
something is known as information. efficiently.
• When we add context to raw data, we transform it into information,
which makes it a lot more helpful in making decisions, understanding • Data structures are used in almost every program or software
complex situations, or building new knowledge. system.

• Some common examples of data structures are arrays, linked lists,


queues, stacks, binary trees, and hash tables.

SELECTION OF DATA STRUCTURE Elementary Data Structure Organization

• When selecting a data structure to solve a problem, the following steps Data : a value or set of values.
must be performed. • It specifies either the value of a variable or a constant
1. Analysis of the problem to determine the basic operations that must be • e.g., marks of students, name of an employee, address of a customer,
supported. For example, basic operation may include inserting /deleting/ value of pi, etc.).
searching a data item from the data structure.
• Elementary item: a data item that does not have subordinate data
items.
2. Quantify the resource constraints for each operation. • Group item: the one that is composed of one or more subordinate data
items.
3. Select the data structure that best meets these requirements. • For example, a student’s name may be divided into three sub-items—
first name, middle name, and last name—but his roll number would
normally be treated as a single item.

2
14-05-2025

Record: is a collection of data items.


• For example, the name, address, course, and marks obtained are Storage Structures
individual data items. But all these data items can be grouped
together to form a record.

• Storage structures refer to the way data is organized, stored, and


File is a collection of related records. managed in memory for processing by a computer.
• For example, if there are 60 students in a class, then there are 60
records of the students. All these related records are stored in a
file. • These structures are the foundation of data storage in computer systems
• Each record in a file may consist of multiple data items but the and programming, enabling efficient data manipulation, retrieval, and
value of a certain data item uniquely identifies the record in the storage.
file. Such a data item K is called a primary key, and the values K1,
K2 ... in such field are called keys or key values.
• For example, in a student’s record that contains roll number, name,
address, course, and marks obtained, the field roll number is a
primary key.

Sequential Storage Representation Linked storage representation

• Sequential storage, data is stored in a contiguous block of memory locations. This is • Data is stored in nodes, where each node contains two parts:
commonly used for arrays and strings. • Data: The actual value stored in the node.
Characteristics: • Pointer/Link: A reference to the next node in the sequence (or, in the case of
• Contiguous Memory Allocation: All elements are stored one after the other in memory. doubly-linked lists, references to both the next and the previous nodes).
Characteristics:
• Indexing: Direct access using indices, making it efficient for random access.
• Dynamic Memory Allocation: Nodes are created at runtime, allowing the structure
• Fixed Size: The size of the structure is typically fixed at the time of declaration. to grow or shrink as needed.
• Efficient Traversal: Traversal is straightforward due to contiguous allocation. • Non-contiguous Memory Allocation: Nodes may be scattered across different
• Insertion/Deletion Overhead: Inserting or deleting elements may require shifting other memory locations, but they are connected via pointers.
elements, leading to a time complexity of 𝑂(𝑛) in the worst case. • Efficient Insertions/Deletions: Adding or removing nodes can be done efficiently
(𝑂(1)) if the pointer to the position is known.

3
14-05-2025

CLASSIFICATION OF DATA STRUCTURES Non-primitive Data Structures:


• Non-primitive data structures are those data structures which are
created using primitive data structures.
• Data structures are generally categorized into two classes: primitive • Examples :linked lists, stacks, trees, and graphs.
and non-primitive data structures. • Non-primitive data structures can further be classified into two
Primitive : categories: linear and non-linear data structures.
• Primitive data structures are the fundamental data types which are Linear data structures:
supported by a programming language. • If the elements of a data structure are stored in a linear or
• Some basic data types are integer, real, character, and boolean. sequential order, then it is a linear data structure.
• The terms ‘data type’, ‘basic data type’, and ‘primitive data type’ • Examples include arrays, linked lists, stacks, and queues.
are often used interchangeably.

• Linear data structures can be represented in memory in two


different ways:
Data structures supported by C
1.To have a linear relationship between elements by means of sequential
memory locations.
2. To have a linear relationship between elements by means of links. • Arrays
• Linked Lists
Non- Linear data structures: • Stacks
If the elements of a data structure are not stored in a sequential • Queues
order, then it is a non-linear data structure.
• Trees
The relationship of adjacency is not maintained between elements
• Graphs
of a non-linear data structure.
Examples include trees and graphs.

4
14-05-2025

Arrays
• In C, the array index starts from zero. This means that the array marks
will contain 10 elements in all.
• An array is a collection of similar data elements. These data elements
have the same data type.
• The elements of the array are stored in consecutive memory locations
and are referenced by an index (also known as the subscript).
• In C, arrays are declared using the following syntax:
type name[size];
• For example, int marks[10];

Linked Lists
Limitations of arrays:
• Arrays are of fixed size.
• A linked list is a very flexible, dynamic data structure in which
• Data elements are stored in contiguous memory locations which elements (called nodes) form a sequential list.
may not be always available.
• In a linked list, each node is allocated space as it is added to the list.
• Insertion and deletion of elements can be problematic because of
shifting of elements from their positions. • Every node in the list points to the next node in the list.
• Therefore, in a linked list, every node contains the following two
types of data:
• The value of the node or any other data that corresponds to that node
• A pointer or link to the next node in the list

5
14-05-2025

Stack
The last node in the list contains a NULL pointer to indicate that it
is the end or tail of the list.
• A stack is a linear data structure in which insertion and deletion of
elements are done at only one end, which is known as the top of
the stack.
• Stack is called a last-in, first-out (LIFO) structure.
• In the computer’s memory, stacks can be implemented using
arrays or linked lists.
• Advantage: Easier to insert or delete data elements
• Disadvantage: Slow search operation and requires more memory
space.

• Every stack has variables:


Queues
• top :
• used to store the address of the topmost element of the stack.
• It is this position from where the element will be added or deleted.
• A queue is a first-in, first-out (FIFO) data structure in which the
• MAX : element that is inserted first is the first one to be taken out.
Used to store the maximum number of elements that the stack can store.
• The elements in a queue are added at one end called the rear
• If top = NULL, then it indicates that the stack is empty and if top = and removed from the other end called the front.
MAX–1, then the stack is full. • Like stacks, queues can be implemented by using either arrays or
• A stack supports three basic operations: linked lists.
• Push : adds an element to the top of the stack. • Every queue has front and rear variables that point to the position
• Pop : removes the element from the top of the stack. from where deletions and insertions can be done, respectively.
• Peep : returns the value of the topmost element of the stack (without • A queue is full when rear = MAX – 1.
deleting it).
• If front = NULL and rear = NULL, then there is no element in the
• Before operations we must check overflow and underflow queue.
conditions.

6
14-05-2025

• Every queue has front and rear variables that point to the position
from where deletions and insertions can be done, respectively. Trees

• A tree is a non-linear data structure which consists of a collection


• To add an element 45 to the queue: of nodes arranged in a hierarchical order.

• One of the nodes is designated as the root node, and the


remaining nodes can be partitioned into disjoint sets such that
each set is a sub-tree of the root.
• To delete an element in the queue:
• The simplest form of a tree is a binary tree.

Binary Trees Graphs

• A binary tree consists of a root node and left and • A graph is a non-linear data structure which is a collection of vertices
right sub-trees, where both sub-trees are also (also called nodes) and edges that connect these vertices.
binary trees.
• A graph is often viewed as a generalization of the tree structure, where
• Each node contains a data element, a left pointer instead of a purely parent-to-child relationship between tree nodes, any
which points to the left sub-tree, and a right
pointer which points to the right sub-tree. kind of complex relationships between the nodes can exist.
• The root element is the topmost node which is • A node in the graph may represent a city and the edges connecting the
pointed by a ‘root’ pointer. If root = NULL then the nodes can represent roads.
tree is empty. • A graph can also be used to represent a computer network where the
• Advantage: Provides quick search, insert, and nodes are workstations and the edges are the network connections.
delete operations
• Disadvantage: Complicated deletion algorithm

7
14-05-2025

• Unlike trees, graphs do not have any root node. Rather, every node in the Operations on Data Structures
graph can be connected with every another node in the graph.
• When two nodes are connected via an edge, the two nodes are known as
neighbours.
• Advantage: Best models real-world situations
• Traversing
• Disadvantage: Some algorithms are slow and very complex • Searching
• Inserting
• Deleting
• Sorting
• Merging

APPLICATIONS OF DATA STRUCTURES ALGORITHMS

• Compiler design • Algorithm: ‘A formally defined procedure for performing some


• Operating system calculation’.
• Statistical analysis package • If a procedure is formally defined, then it can be implemented
using a formal language, and such a language is known as a
• DBMS- Database Management System programming language.
• Numerical analysis • An algorithm provides a blueprint to write a program to solve a
• Simulation particular problem.
• Artificial intelligence • An effective procedure for solving a problem in finite number of
steps
• Graphics
• Algorithms are mainly used to achieve software reuse.

8
14-05-2025

DIFFERENT APPROACHES TO DESIGN AN CONTROL STRUCTURES USED IN


ALGORITHM ALGORITHMS

• An algorithm has a finite number of steps. Some steps may involve


decision-making and repetition.
• When working with data structures, algorithms are used to
perform operations on the stored data. • An algorithm may employ one of the following control structures:
• A complex algorithm is often divided into smaller units called (a) sequence
modules. (b) decision
• This process of dividing an algorithm into modules is called (c) repetition.
modularization

Sequence Decision

• By sequence, we mean that each step of an algorithm is executed • Decision statements are used when the execution of a process depends
on the outcome of some condition.
in a specified order.
• Syntax: IF condition Then process
• A condition in this context is any statement that may evaluate to either
a true value or a false value.
• example, if x = y, then print EQUAL.
• A decision statement can also be stated in the following manner:
IF condition
Then process1
ELSE
process2
• This form is popularly known as the IF–ELSE construct

9
14-05-2025

Repetition

• Repetition, which involves executing one or more steps for a


number of times, can be implemented using constructs such as
while, do–while, and for loops.
• These loops execute one or more steps until some condition is
true.

Tasks:

• Write an algorithm to print largest of 3 given numbers.


• Write an algorithm to print all even numbers between 1 to n.

SEARCHING
• Write an algorithm to print all multiples of 5 between 1 to 500.

• To find whether a particular value is present in an array or not.


• If the value is present in the array, then searching is said to be successful and
the searching process gives the location of that value in the array.
• If the value is not present in the array, the searching process displays an
appropriate message and in this case searching is said to be unsuccessful.

10
14-05-2025

Types of searching techniques Linear Search

• There are two popular methods for searching the array elements: • Also called as sequential search.
linear search and binary search. • It works by comparing the value to be searched with every element of
• The algorithm that should be used depends entirely on how the the array one by one in a sequence until a match is found.
values are organized in the array. • Mostly used to search an unordered list of elements.
• For example, if the elements of the array are arranged in ascending • For example, if an array A[] is declared and initialized as,
order, then binary search should be used, as it is more efficient for
sorted lists in terms of complexity. • int A[] = {10, 8, 2, 7, 3, 4, 9, 1, 6, 5};
• the value to be searched is VAL = 7, then searching means to find
whether the value ‘7’ is present in the array or not.

Linear Search- Algorithm Complexity of Linear Search Algorithm

• Linear search executes in O(n) time where n is the number of elements


10 8 2 7 3 4 9 1 6 5
in the array.
• best case : VAL =first element of the array.
• worst case : VAL is not present in the array or VAL=last element of the
array.
• In both the cases, n comparisons will have to be made.
• However, the performance of the linear search algorithm can be
improved by using a sorted array.

11
14-05-2025

Binary Search- Example


Binary Search
• The algorithm will proceed in the following manner.
• BEG = 0, END = 10, MID = (0 + 10)/2 = 5
• Binary search is a searching algorithm that works efficiently with a 0 1 2 3 4 5 6 7 8 9 10
sorted list. • Now, VAL = 9 and A[MID] = A[5] = 5
• Similar to search in a telephone directory, dictionary. • A[5] is less than VAL, therefore, we now search for the value in the second half of
the array.
• Consider an array A[] that is declared and initialized as
• Now, BEG = MID + 1 = 6, END = 10, MID = (6 + 10)/2 =16/2 = 8
• int A[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
• VAL = 9 and A[MID] = A[8] = 8
• The value to be searched is VAL = 9.
• A[8] is less than VAL, therefore, we now search for the value in the second half of
the segment.
• Now, BEG = MID + 1 = 9, END = 10, MID = (9 + 10)/2 = 9
• Now, VAL = 9 and A[MID] = 9.

Binary Search- Example Binary Search- Algorithm


0 1 2 3 4 5 6 7 8 9 10

• if VAL is not equal to A[MID], then the values of BEG, END, and MID will be
changed depending on whether VAL is smaller or greater than A[MID].
• (a) If VAL < A[MID], then VAL will be present in the left segment of the array. So,
END = MID – 1.
• (b) If VAL > A[MID], then VAL will be present in the right segment of the array. So,
BEG = MID + 1.
• Finally, if VAL is not present in the array, then eventually, END will be less than
BEG.
• When this happens, the algorithm will terminate and the search will be
unsuccessful.

12
14-05-2025

arranging the elements of an array


ascending or
descending

A[0] < A[1] < A[2] < ...... <


A[N].
an algorithm that puts the elements
of a list in a certain order, which can be either numerical order,
lexicographical order, or any user-defined order

sort key
• Internal sorting data stored in the
computer’s memory sort key comparisons
records in the list will be moved
• External sorting data stored in files

6. Stability equivalent
elements or records retain their relative positions even after sorting is
done

1
14-05-2025

consecutive adjacent pairs of elements in the array are


compared with each other.
• If the element at the lower index is greater than the element at the higher
index, the two elements are interchanged

Example-A[] = {30, 52, 29, 87, 63, 27, 19, 54}

29, 30, 52, 63, 27, 19, 54, 87


30, 29, 52, 87, 63, 27, 19, 54

30, 29, 52, 63, 87, 27, 19, 54


29, 30, 52, 27, 63, 19, 54, 87
30, 29, 52, 63, 27, 87, 19, 54
29, 30, 52, 27, 19, 63, 54, 87
30, 29, 52, 63, 27, 19, 87, 54

30, 29, 52, 63, 27, 19, 54, 87 29, 30, 52, 27, 19, 54, 63, 87

2
14-05-2025

29, 27, 30, 19, 52, 54, 63, 87


29, 30, 27, 52, 19, 54, 63, 87

29, 27, 19, 30, 52, 54, 63, 87


29, 30, 27, 19, 52, 54, 63, 87

27, 29, 19, 30, 52, 54, 63, 87 19, 27, 29, 30, 52, 54, 63, 87

27, 19, 29, 30, 52, 54, 63, 87

3
14-05-2025

comparisons

f(n) = (n – 1) + (n – 2) + (n – 3) + ..... + 3 + 2 + 1
( )( ) ( )
f(n) = = //sum of natural numbers
f(n) = − = O(𝑛 )
O(𝑛 ).
𝑛 n
is the total number of elements

once we have detected that the array is sorted, the algorithm


must not be executed further

have a variable flag which is set to TRUE


made FALSE when a swapping

4
14-05-2025

best case that it inserts each item into its


O(n) proper place in the final list
worst case
perform slower than the original algorithm two sets
sorted unsorted
original bubble sort
O(𝑛 ) index 0
is initially in the sorted set rest
unsorted

5. During each iteration of the algorithm, the first element in the unsorted set
is picked up and inserted into the correct position in the sorted set.

compare A[K] with A[K–1], then with A[K–2], A[K–3], and


so on until we meet an element A[J] such that A[J] <= A[K].
Sorted

Unsorted

5
14-05-2025

best case
O(n)

worst case

O(𝑛 )
(K–1)/2

efficient to use on small sets

O(𝑛 ).

twice as fast as the bubble sort

only O(1) of additional memory space

6
14-05-2025

ARR N Pass 1 POS


ARR[POS] ARR[0]

Pass 2
ARR[POS] ARR[1]

Pass N–1
ARR[POS] ARR[N–2]

7
14-05-2025

developed by C. A. R. pivot
Hoare.
partition exchange sort all elements that are
divide-and-conquer less than the pivot appear before all elements greater than the
pivot element come after it
O(n log n)
O(𝑛 ). pivot is placed in its final position
partition operation

Step 1 scan
loc = 0, left = 0, and right = n–1 from left to right

Step 2 scan the array from


right to left

right = loc pivot


correct position
a[loc] > a[right], then interchange a[loc] < a[left], then
interchange

8
14-05-2025

Best-case analysis:
Worst Case:
sublists
returned by the partitioning routine is of size n − 1
pivot happens to be the smallest or largest log n
O(log n).
O(n)
O(n log n)

• This means that the call tree is a linear chain of n − 1 nested calls.
The ith call does O(n − i) work to do the partition, and , so in that
case quicksort takes O(n²) time.

9
14-05-2025

focuses on two main concepts

• Divide

• Conquer
• Combine

array is of length 0 or 1, then it is already sorted

10
14-05-2025

O(n log n).

O(n)

11

You might also like