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

DS Notes Unit 1 - Part 1

The document provides an introduction to linear data structures, including their definitions, classifications, and advantages. It covers various types of linear data structures such as arrays, linked lists, stacks, and queues, along with their operations and abstract data types (ADTs). Additionally, it discusses time and space complexity analysis, asymptotic notations, and the merge sort algorithm.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

DS Notes Unit 1 - Part 1

The document provides an introduction to linear data structures, including their definitions, classifications, and advantages. It covers various types of linear data structures such as arrays, linked lists, stacks, and queues, along with their operations and abstract data types (ADTs). Additionally, it discusses time and space complexity analysis, asymptotic notations, and the merge sort algorithm.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

UNIT I

Introduction to Linear Data Structures: Definition and importance of linear data


structures, Abstract data types (ADTs) and their implementation, Overview of time
and space complexity analysis for linear data structures. Searching Techniques: Linear
& Binary Search, Sorting Techniques: Bubble sort, Selection sort, Insertion Sort, Quick
Sort, Merge Sort.

Data Structure is a way to store and organize data so that it can be used efficiently.
A data structure is not only used for organizing the data. It is also used for processing,
retrieving, and storing data. (or) The organized collection of data is called a ‘Data
Structure’.
Data Structure involves two complementary goals. The first goal is to identify and
develop useful, mathematical entities and operations and to determine what class of
problems can be solved by using these entities and operations. The second goal is to
determine representation for those abstract entities to implement abstract operations
on this concrete representation.
Classification of Data Structure:

Prepared by: A P V D L KUMAR, M.Tech, (Ph.D)


Training and Placement Officer, BVCITS
 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.
Examples of linear data structures are array, stack, queue, linked list, etc.
 Static data structure: Static data structure has a fixed memory size. It
is easier to access the elements in a static data structure.
An example of this data structure is an array.
 Dynamic data structure: In 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.
Examples of this data structure are linked lists, queue, stack, etc.
 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 only.
Examples of non-linear data structures are trees and graphs.

Advantages of Data Structures:


Efficiency: Efficiency of a program depends upon the choice of data structures. For
example: suppose, we have some data and we need to perform the search for a
particular record element. Hence, using array may not be very efficient here.
Reusability: Data structures are reusable, i.e. once we have implemented a
particular data structure, we can use it at any other place.
Abstraction: Data structure is specified by the ADT which provides a level of
abstraction. The client program uses the data structure through interface only,
without getting into the implementation details.

Types of Linear Data Structures are given below:


Arrays:
The array is that type of structure that stores homogeneous elements at memory
locations which are contiguous. The same types of objects are stored sequentially

Prepared by: A P V D L KUMAR, M.Tech, (Ph.D)


Training and Placement Officer, BVCITS
in an array. The main idea of an array is that multiple data of the same type can be
stored together. Before storing the data in an array, the size of the array has to be
defined. Any element in the array can be accessed or modified and the elements
stored are indexed to identify their locations.
Types of arrays:
 One-dimensional array: This is a simple form of array that contains elements,
all of which are the same type of data, in a single row.
 Two-dimensional array: This is also known as a matrix. This type of data
structure has rows and columns and appears like a grid. The elements can be
accessed using two indices- one for column and one for row.
 Multi-dimensional array: These arrays have more than two dimensions.

Linked List: Linked list is a linear data structure which is used to maintain a list in
the memory. It can be seen as the collection of nodes stored at non-contiguous
memory locations. Each node of the list contains a pointer to its adjacent node. It
is a collection of data of same data type but the data items need not be stored in
consecutive.
Stack: Stack is a linear list in which insertion and deletions are allowed only at one
end, called top. It is a Last-In-First-Out linear data structure.
A stack is an abstract data type (ADT), can be implemented in most of the
programming languages. It is named as stack because it behaves like a real-world
stack, for example, piles of plates or deck of cards etc.
Queue: Queue is a linear list in which elements can be inserted only at one end
called rear and deleted only at the other end called front. It is a First-In-First-Out
Linear data structure.
It is an abstract data structure, similar to stack. Queue is opened at both end
therefore it follows First-In-First-Out (FIFO) methodology for storing the data items.
Operations applied on Linear Data Structure:
The following list of operations applied on linear data structures
 Insert an element
 Delete an element

Prepared by: A P V D L KUMAR, M.Tech, (Ph.D)


Training and Placement Officer, BVCITS
 Traverse
 Sort the list of elements
 Search for a data element

ABSTRACT DATA TYPES


Abstract Data Type (ADT) is a type or class for objects whose behavior is defined by
a set of value and a set of operations.
The definition of ADT only mentions what operations are to be performed but not
how these operations will be implemented. It does not specify how data will be
organized in memory and what algorithms will be used for implementing the
operations. The process of providing only the essentials and hiding the details is
known as abstraction is shown in below figure.

LIST ADT :
List is basically the collection of elements arranged in a sequential manner. In
memory we can store the list in two ways: one way is we can store the elements in
sequential memory locations. That means we can store the list in arrays. The other
way is we can use pointers or links to associate elements sequentially. This is
known as linked list.

Prepared by: A P V D L KUMAR, M.Tech, (Ph.D)


Training and Placement Officer, BVCITS
Some of the most essential operations defined in List ADT are listed below.
 front(): returns the value of the node present at the front of the list.
 back(): returns the value of the node present at the back of the list.
 push_front(int val): creates a pointer with value = val and keeps this pointer
to the front of the linked list.
 push_back(int val): creates a pointer with value = val and keeps this pointer
to the back of the linked list.
 pop_front(): removes the front node from the list.
 pop_back(): removes the last node from the list.
 empty(): returns true if the list is empty, otherwise returns false.
 size(): returns the number of nodes that are present in the list.

Stack ADT

A stack is a linear data structure that only allows data to be accessed from the top.
It simply has two operations: push (to insert data to the top of the stack) and pop
(to remove data from the stack). (used to remove data from the stack top).

Prepared by: A P V D L KUMAR, M.Tech, (Ph.D)


Training and Placement Officer, BVCITS
Some of the most essential operations defined in Stack ADT are listed below.

 top(): returns the value of the node present at the top of the stack.
 push(int val): creates a node with value = val and puts it at the stack top.
 pop(): removes the node from the top of the stack.
 empty(): returns true if the stack is empty, otherwise returns false.
 size(): returns the number of nodes that are present in the stack.

Queue ADT

A queue is a linear data structure that allows data to be accessed from both ends.
There are two main operations in the queue: push (this operation inserts data to
the back of the queue) and pop (this operation is used to remove data from the
front of the queue).

Some of the most essential operations defined in Queue ADT are listed below.

 front(): returns the value of the node present at the front of the queue.
 back(): returns the value of the node present at the back of the queue.
 push(int val): creates a node with value = val and puts it at the front of the
queue.
 pop(): removes the node from the rear of the queue.
 empty(): returns true if the queue is empty, otherwise returns false.
 size(): returns the number of nodes that are present in the queue.

Advantages of ADT in Data Structures

The advantages of ADT in Data Structures are:

Prepared by: A P V D L KUMAR, M.Tech, (Ph.D)


Training and Placement Officer, BVCITS
 Provides abstraction, which simplifies the complexity of the data structure
and allows users to focus on the functionality.
 Enhances program modularity by allowing the data structure implementation
to be separate from the rest of the program.
 Enables code reusability as the same data structure can be used in multiple
programs with the same interface.
 Promotes the concept of data hiding by encapsulating data and operations
into a single unit, which enhances security and control over the data.
 Supports polymorphism, which allows the same interface to be used with
different underlying data structures, providing flexibility and adaptability to
changing requirements.

Disadvantages of ADT in Data Structures

There are some potential disadvantages of ADT in Data Structures:

 Overhead: Using ADTs may result in additional overhead due to the need for
abstraction and encapsulation.
 Limited control: ADTs can limit the level of control that a programmer has
over the data structure, which can be a disadvantage in certain scenarios.
 Performance impact: Depending on the specific implementation, the
performance of an ADT may be lower than that of a custom data structure
designed for a specific application.

Overview of time and space complexity analysis for linear data


structures:
Performance Analysis of an Algorithm:
Generally algorithms are measured in terms of time complexity and space
complexity.
1. Time Complexity
Time Complexity of an algorithm is a measure of how much time is required to
execute an algorithm for a given number of inputs. And it is measured by its rate of

Prepared by: A P V D L KUMAR, M.Tech, (Ph.D)


Training and Placement Officer, BVCITS
growth relative to a standard function. Time Complexity can be calculated by
adding compilation time and execution time. Or it can do by counting the number
of steps in an algorithm.
2. Space Complexity
Space Complexity of an algorithm is a measure of how much storage is required by
the algorithm. Thus space complexity is the amount of computer memory required
during program execution as a function of input elements. The space requirement
of algorithm can be performed at compilation time and run time.
ASYMPTOTIC NOTATIONS
Asymptotic analysis of an algorithm refers to defining the mathematical
boundation/framing of its run-time performance. Using asymptotic analysis, we can
very well conclude the best case, average case, and worst case scenario of an
algorithm.
Asymptotic analysis is input bound i.e., if there's no input to the algorithm, it is
concluded to work in a constant time. Other than the "input" all other factors are
considered constant.
Asymptotic analysis refers to computing the running time of any operation in
mathematical units of computation. For example, the running time of one
operation is computed as f(n) and may be for another operation it is computed as
g(n2). This means the first operation running time will increase linearly with the
increase in n and the running time of the second operation will increase
exponentially when n increases. Similarly, the running time of both operations will
be nearly the same if n is significantly small.
The time required by an algorithm falls under three types −
Best Case − Minimum time required for program execution.
Average Case − Average time required for program execution.
Worst Case − Maximum time required for program execution.
Asymptotic Notations
Following are the commonly used asymptotic notations to calculate the running
time complexity of an algorithm.

Prepared by: A P V D L KUMAR, M.Tech, (Ph.D)


Training and Placement Officer, BVCITS
 Ο Notation
 Ω Notation
 θ Notation
Big Oh Notation, Ο
The notation Ο(n) is the formal way to express the upper bound of an algorithm's
running time. It measures the worst case time complexity or the longest amount of
time an algorithm can possibly take to complete.

For example, for a function f(n)


Ο(f(n)) = { g(n) : there exists c > 0 and n0 such that f(n) ≤ c.g(n) for all n > n0. }
Omega Notation, Ω
The notation Ω(n) is the formal way to express the lower bound of an algorithm's
running time. It measures the best case time complexity or the best amount of time
an algorithm can possibly take to complete.

For example, for a function f(n)


Ω(f(n)) ≥ { g(n) : there exists c > 0 and n0 such that g(n) ≤ c.f(n) for all n > n0. }

Prepared by: A P V D L KUMAR, M.Tech, (Ph.D)


Training and Placement Officer, BVCITS
Theta Notation, θ
The notation θ(n) is the formal way to express both the lower bound and the upper
bound of an algorithm's running time. It is represented as follows –

θ(f(n)) = { g(n) if and only if g(n) = Ο(f(n)) and g(n) = Ω(f(n)) for all n > n0 }

Time Complexity:
Notation Description
Complexity
Constant number of
operations, not
Constant O(1)
depending on the input
data size.
Number of operations
proportional of log(n)
Logarithmic O(logn)
where n is the size of
the input data.
Number of operations
Linear O(n) proportional to the
input data size.
Number of operations
proportional to the
Quadratic O(n²)
square of the size of
the input data.
Number of operations
proportional to the
Cubic O(n³)
cube of the size of the
input data.
Exponential number of
Exponential operations, fast
O(n!), O(kn),O(2n)
growing.

Prepared by: A P V D L KUMAR, M.Tech, (Ph.D)


Training and Placement Officer, BVCITS
Merge Sort
Merge sort is a sorting algorithm based on the Divide and conquer strategy. It works
by recursively dividing the array into two equal halves, then sorts them and
combine them. It takes a time of (nlogn) in the worst case.
What is Merge Sort?
The merge sort algorithm is an implementation of the divide and conquer
technique. Thus, it gets completed in three steps:
1. Divide: In this step, the array/list divides itself recursively into sub-arrays until
the base case is reached.
2. Recursively solve: Here, the sub-arrays are sorted using recursion.
3. Combine: This step makes use of the merge( ) function to combine the sub-
arrays into the final sorted array.
Algorithm for Merge Sort
Step 1: Find the middle index of the array.
Middle = (beg + end)/2
Step 2: Divide the array from the middle.
Step 3: Call merge sort for the first half of the array
mergesort(array, first, middle)
Step 4: Call merge sort for the second half of the array.
mergesort(array, middle+1, last)
Step 5: Merge the two sorted halves into a single sorted array.

Prepared by: A P V D L KUMAR, M.Tech, (Ph.D)


Training and Placement Officer, BVCITS
Example:

************* ALL THE VERY BEST ***********************

Prepared by: A P V D L KUMAR, M.Tech, (Ph.D)


Training and Placement Officer, BVCITS

You might also like