You are on page 1of 34

DATA STRUCTURES AND ALGORITHMS

CHAPTER 2
Data structures Basics

By: DHANISH E

BSCAIT2 /BSCNCS 2/ ECE 3


DATA STRUCTURE

The logical or mathematical model of a particular


organization of data is called data structure

The study of such data structure, which forms the subject


matter of the text, includes the following three steps:

(1) Logical or mathematical description of the structure.


(2) Implementation of the structure on a computer.
(3) Quantitative analysis of the structure, which include
determining the amount of memory needed to store the
structure and the time required to process the structure.
© ISBAT UNIVERSITY – 2020. 02-Oct-21
Primitive data types:

 Primitive data types are the predefine data types


like int, char, float etc in 'C' language. The are the
simplest data types.

© ISBAT UNIVERSITY – 2020. 02-Oct-21


Composite data types:

They are the data types which are derived from the
primitive data type. They are slightly complex then the
primitive.
They can be homogeneous and heterogeneous.

Composite data type are broadly classified into two major


categories:

o Linear Data Type


o Non Linear Data Type

© ISBAT UNIVERSITY – 2020. 02-Oct-21


Classification of Data Structures

© ISBAT UNIVERSITY – 2020. 02-Oct-21


DATA STRUCTURE

Data Structure is a systematic way to organize data in order


to use it efficiently. Following terms are the foundation terms
of a data structure.

 Interface − Each data structure has an interface. Interface


represents the set of operations that a data structure
supports. An interface only provides the list of supported
operations, type of parameters they can accept and return
type of these operations.
 Implementation − Implementation provides the internal
representation of a data structure. Implementation also
provides the definition of the algorithms used in the
operations of the data structure.

© ISBAT UNIVERSITY – 2020. 02-Oct-21


Characteristics of a Data Structure

 Correctness − Data structure implementation should


implement its interface correctly.
 Time Complexity − Running time or the execution
time of operations of data structure must be as
small as possible.
 Space Complexity − Memory usage of a data
structure operation should be as little as possible.

© ISBAT UNIVERSITY – 2020. 02-Oct-21


Need for Data Structure

 As applications are getting complex and data


rich, there are three common problems that
applications face

▪ Data Search
▪ Processor speed
▪ Multiple requests

© ISBAT UNIVERSITY – 2020. 02-Oct-21


Execution Time Cases

 Worst Case − This is the scenario where a particular data structure


operation takes maximum time it can take. If an operation's worst
case time is ƒ(n) then this operation will not take more than ƒ(n) time
where ƒ(n) represents function of n.
 Average Case − This is the scenario depicting the average execution
time of an operation of a data structure. If an operation takes ƒ(n)
time in execution, then m operations will take mƒ(n) time.
 Best Case − This is the scenario depicting the least possible execution
time of an operation of a data structure. If an operation takes ƒ(n)
time in execution, then the actual operation may take time as the
random number which would be maximum as ƒ(n).

© ISBAT UNIVERSITY – 2020. 02-Oct-21


Operations on Data Structures

The basic operations that are performed on data structures are as follows:

Insertion: Insertion means addition of a new data element in a data structure.

Deletion: Deletion means removal of a data element from a data structure if it is found.

Searching: Searching involves searching for the specified data element in a data structure.

Traversal: Traversal of a data structure means processing all the data elements present in it.

Sorting: Arranging data elements of a data structure in a specified order is called sorting.

Merging: Combining elements of two similar data structures to form a new data structure of
the same type, is called merging.

© ISBAT UNIVERSITY – 2020. 02-Oct-21


ADT (Abstract Data Types)

 Abstract data types are the entities that are


definitions of data and operations but do not have
implementation details.
 A data structure is the implementation for an ADT

“An abstract data structure or type "is defined indirectly, only by the
operations that may be performed on it and by mathematical constraints
on the effects (and possibly cost) of those operations."

© ISBAT UNIVERSITY – 2020. 02-Oct-21


List ADT


A list contains elements of same type arranged in sequential
order and following operations can be performed on the list.
get() – Return an element from the list at any given position.
insert() – Insert an element at any position of the list.
remove() – Remove the first occurrence of any element from a
non-empty list.
removeAt() – Remove the element at a specified location from a
non-empty list.
replace() – Replace an element at any position by another
element.
size() – Return the number of elements in the list.
isEmpty() – Return true if the list is empty, otherwise return false.
isFull() – Return true if the list is full, otherwise return false.

© ISBAT UNIVERSITY – 2020. 02-Oct-21


Stack ADT

A Stack contains elements of same type arranged in


sequential order. All operations takes place at a single end
that is top of the stack and following operations can be
performed:
push() – Insert an element at one end of the stack called top.
pop() – Remove and return the element at the top of the
stack, if it is not empty.
peek() – Return the element at the top of the stack without
removing it, if the stack is not empty.
size() – Return the number of elements in the stack.
isEmpty() – Return true if the stack is empty, otherwise return
false.
isFull() – Return true if the stack is full, otherwise return false.
© ISBAT UNIVERSITY – 2020. 02-Oct-21
Queue ADT

A Queue contains elements of same type arranged in


sequential order. Operations takes place at both ends, insertion
is done at end and deletion is done at front. Following
operations can be performed:
enqueue() – Insert an element at the end of the queue.
dequeue() – Remove and return the first element of queue, if
the queue is not empty.
peek() – Return the element of the queue without removing it, if
the queue is not empty.
size() – Return the number of elements in the queue.
isEmpty() – Return true if the queue is empty, otherwise return
false.
isFull() – Return true if the queue is full, otherwise return false.
© ISBAT UNIVERSITY – 2020. 02-Oct-21
 for example, the List ADT can be implemented using
arrays, or singly linked list or doubly linked list.
Similarly, stack ADT and Queue ADT can be
implemented using arrays or linked lists.

© ISBAT UNIVERSITY – 2020. 02-Oct-21


Algorithms

 Algorithm is a step-by-step procedure, which


defines a set of instructions to be executed in a
certain order to get the desired output.
 Algorithms are generally created independent of

underlying languages, i.e. an algorithm can be


implemented in more than one programming
language.
which can be expressed either as an informal high level description as pseudocode or
using a flowchart.

© ISBAT UNIVERSITY – 2020. 02-Oct-21


Features of algorithms
17

Unambiguous − Algorithm should be clear and


unambiguous. Each of its steps (or phases), and their
inputs/outputs should be clear and must lead to only one
meaning.

Input − An algorithm should have 0 or more well-defined


inputs.

Output − An algorithm should have 1 or more well-defined


outputs, and should match the desired output.

Finiteness − Algorithms must terminate after a finite number


of steps.
© ISBAT UNIVERSITY – 2020. 02-Oct-21
How to Write an Algorithm?
18

There are no well-defined standards for writing algorithms.

Rather, it is problem and resource dependent. Algorithms are never


written to support a particular programming code.

As we know that all programming languages share basic code


constructs like loops (do, for, while), flow-control (if-else), etc.
These common constructs can be used to write an algorithm.

We write algorithms in a step-by-step manner, but it is not always


the case. Algorithm writing is a process and is executed after the
problem domain is well-defined. That is, we should know the
problem domain, for which we are designing a solution.

© ISBAT UNIVERSITY – 2020. 02-Oct-21


Example 1

Problem − Design an algorithm to add two numbers and


display the result.

Step 1 − START
Step 2 − declare three integers a, b & c
Step 3 − define values of a & b
Step 4 − add values of a & b
Step 5 − store output of step 4 to c
Step 6 − print c
Step 7 − STOP

© ISBAT UNIVERSITY – 2020. CHAPTER 1 02-Oct-2119


Example 2

Algorithms tell the programmers how to code the program.


Alternatively, the algorithm can be written as −

Step 1 − START ADD


Step 2 − get values of a & b
Step 3 − c ← a + b
Step 4 − display c
Step 5 − STOP

© ISBAT UNIVERSITY – 2020. CHAPTER 1 02-Oct-2120


Problem − Write an algorithm to add two numbers and display the
result.
 step 1 − START

 step 2 − declare three integer variables a, b & c

 step 3 − assign values for a & b

 step 4 − add values of a & b

 step 5 − store output of step 4 to c

 step 6 − print c

 step 7 − STOP

© ISBAT UNIVERSITY – 2020. 02-Oct-21


PSEUDO CODE

````````````````````
START ADD
get values of a & b
c←a+b
display c
STOP

© ISBAT UNIVERSITY – 2020. 02-Oct-21


Algorithm Analysis

Efficiency of an algorithm can be analyzed at two different stages,


before implementation and after implementation. They are the
following −
 A Priori Analysis − This is a theoretical analysis of an algorithm.
Efficiency of an algorithm is measured by assuming that all other
factors, for example, processor speed, are constant and have no
effect on the implementation.
 A Posterior Analysis − This is an empirical analysis of an
algorithm. The selected algorithm is implemented using
programming language. This is then executed on target computer
machine. In this analysis, actual statistics like running time and
space required, are collected.

© ISBAT UNIVERSITY – 2020. 02-Oct-21


Algorithm Complexity

Time Factor − Time is measured by counting the number of


key operations such as comparisons in the sorting algorithm.

Space Factor − Space is measured by counting the maximum


memory space required by the algorithm.

The complexity of an algorithm f(n) gives the running time


and/or the storage space required by the algorithm in terms
of n as the size of input data.

© ISBAT UNIVERSITY – 2020. 02-Oct-2125


Big Oh Notation, Ο

 Big O notation is used to describe the


performance or complexity of an algorithm.
 Big O specifically describes the worst-case
scenario, and can be used to describe the
execution time required or the space used (e.g. in
Bachmann–Landau notation

memory or on disk) by an algorithm.


 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.
© ISBAT UNIVERSITY – 2020. 02-Oct-21
Common Notations

constant O(1) describes an algorithm Ο(1)


that will always execute in the
same time (or space)
regardless of the size of the
input data set.
linear O(N) describes an algorithm Ο(n)
whose performance will grow
linearly and in direct
proportion to the size of the
input data set.
logarithmic − Ο(log n)

n log n − Ο(n log n)


2 2
quadratic O(N ) represents an algorithm Ο(n )
whose performance is directly
proportional to the square of
the size of the input data set.
3
cubic − Ο(n )

© ISBAT UNIVERSITY – 2020. 02-Oct-21


void printFirstElementOfArray(int arr[])
{
printf("First element of array = %d",arr[0]);
}
void printAllElementOfArray(int arr[], int
size)
{
for (int i = 0; i < size; i++)
{
printf("%d\n", arr[i]);
}
}
void printAllPossibleOrderedPairs(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
printf("%d = %d\n", arr[i], arr[j]);
}
}
}
O(log n): Logarithmic Time Operation

 Problem to be solved: Assume we have a box that contains numbers (1, 2, 3, 4,


… 16) and all the numbers are in order. You are asked to find a number 16 in
the box.
 The catch here is that the numbers are in order. Let's split the numbers into two
parts. A total of 16 numbers is divided into two sets each containing eight
numbers.
int n = 16; int[] numbers = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};

int numberToSearch = 16;


int median = 16/2 = 8;

int[] split1 = {1,2,3,4,5,6,7,8};


int[] split2 = {9,10,11,12,13,14,15,16};

© ISBAT UNIVERSITY – 2020. 02-Oct-21


This can be written as,

In math, if n = 2xthen log2 n = x. (Refer


Binary Logarithm)

Hence 16 = 24 can be written as log2 16 =


4.

This can be written as log2 n, or simply


O(log n).
34

Thank you

© ISBAT UNIVERSITY – 2020. 02-Oct-21

You might also like