SlideShare a Scribd company logo
© Oxford University Press 2011. All rights reserved.
Data Structures Using CData Structures Using C
Reema TharejaReema Thareja, Assistant Professor, Institute of
Information Technology and Management,
New Delhi
© Oxford University Press 2011. All rights reserved.
CHAPTER 4
INTRODUCTION TO
DATA STRUCTURES
© Oxford University Press 2011. All rights reserved.
INTRODUCTION
• A data structure is nothing but an arrangement of data either in
computer's memory or on the disk storage.
• Some common examples of data structure includes arrays, linked
lists, queues, stacks, binary trees, and hash tables.
• Data structures are widely applied in areas like:
a. Compiler design
b. Operating system
c. Statistical analysis package
d. DBMS
e. Numerical analysis
f. Simulation
g. Artificial Intelligence
h. Graphics
© Oxford University Press 2011. All rights reserved.
DIFFERENT TYPES OF
DATA STRUCTURES
© Oxford University Press 2011. All rights reserved.
Arrays
• 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).
• Arrays are declared using the following syntax.
type name[size];
1st
element 2nd
element
3rd
element
4th
element 5th
element 6th
element 7th
element 8th
element 9th
element 10th
element
marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7] marks[8] marks[9]
The limitations with arrays include:
Arrays are of fixed size.
Data elements are stored in continuous memory locations which
may not be available always.
Adding and removing of elements is problematic because of
shifting the elements from their positions.
© Oxford University Press 2011. All rights reserved.
Linked List
• Linked list is a very flexible dynamic data structure in which
elements can be added to or deleted from anywhere at will.
• In contrast to using static arrays, a programmer need not worry
about how many elements will be stored in the linked list. This
feature enables the programmers to write robust programs which
require much less maintenance.
• In a linked list, each element (is called a node) is allocated space
as it is added to the list. Every node in the list points to the next
node in the list. Therefore, in a linked list every node contains two
types of information:
The data stored in the node and
A pointer or link to the next node in the list
• Advantage: Provides quick insert, and delete operations
• Disadvantage: Slower search operation and requires more memory
space
1 2 3 4 5 6 7 X
© Oxford University Press 2011. All rights reserved.
Stack
• In computer’s memory stacks can be represented as a linear array.
• Every stack has a variable TOP associated with it. Top is 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. There is another variable
MAX which will be used to store the maximum number of elements that
the stack can store.
• If TOP = NULL, then it indicates that the stack is empty and if TOP =
MAX, then the stack is full.
• If TOP = -1, it indicates that there is no element in the stack.
• Advantage: Last-in, first-out access (LIFO)
• Disadvantage: Slow access to other elements
is full.
A AB ABC ABCD ABCDE
0 1 2 3 TOP = 4 5 6 7 8 9
© Oxford University Press 2011. All rights reserved.
Queue
• A queue is a FIFO (First In First Out) data structure in which the
element that was inserted first is the first one to be taken out.
• The elements in a queue are added at one end called the rear and
removed from the other one end called front. Like stacks, queues can
be implemented either using arrays or linked lists.
• When Rear = MAX – 1, where MAX is the size of the queue that is,
MAX specifies the maximum number of elements in the queue.
• If front = -1 and rear = -1, this means there is no element in the queue.
• Advantage: Provides first-in, first-out data access
• Disadvantage: Slow access to other items
9 7 18 14 36 45
0 FRONT = 1 2 3 4 REAR = 6 7 8 9
© Oxford University Press 2011. All rights reserved.
Tree
• A binary tree is a data structure which is defined as a collection of
elements called nodes.
• Every node contains a "left" pointer, a "right" pointer, and a data
element.
• Every binary tree has a root element pointed by a "root" pointer.
The root element is the topmost node in the tree.
• If root = NULL, then it means the tree is empty.
• Advantages: Provides quick search, insert and delete operations
• Disadvantage: Complicated deletion algorithm
1
32
4
8
5 6 7
12
10 1
1
ROOT NODE
T2T1
9
© Oxford University Press 2011. All rights reserved.
Graph
• A graph is a collection of vertices (also called nodes) and edges that
connect these vertices.
• A graph is often viewed as a generalization of the tree structure,
where instead of a having a purely parent-to-child relationship
between tree nodes, any kind of complex relationships between the
nodes can be represented.
• While in trees, the nodes can have many children but only one
parent, a graph on the other hand relaxes all such kinds of
restrictions.
• unlike trees, graphs do not have any root node. Rather, every node in
the graph can be connected with any other node in the graph. When
two nodes are connected via an edge, the two nodes are known as
neighbors.
• Advantages: Best models real-world situations
• Disadvantages: Some algorithms are slow and very complex
A
D
B C
E
© Oxford University Press 2011. All rights reserved.
ABSTRACT DATA TYPE
• An Abstract Data Type (ADT) is the way at which we look at a data
structure, focusing on what it does and ignoring how it does its job.
• For example, stack and queue are perfect examples of an abstract
data type. We can implement both these ADTs using an array or a
linked list. This demonstrates the "abstract" nature of stacks and
queues.
• In C, an Abstract Data Type can be a structure considered without
regard to its implementation. It can be thought of as a "description" of
the data in the structure with a list of operations that can be
performed on the data within that structure.
• The end user is not concerned about the details of how the methods
carry out their tasks. They are only aware of the methods that are
available to them and are concerned only about calling those
methods and getting back the results but not HOW they work.
© Oxford University Press 2011. All rights reserved.
CODE DESCRIPTION
typedef struct stack_Rep
stack_Rep;
instance representation record
typedef stack_Rep *stack_T; pointer to a stack instance
typedef void *stack_Item; value to be stored on stack
stack_T create_stack(void); Create an empty stack instance
void push(stack_T st, stack_Item
v)
Add an item at the top of the stack
stack_Item pop(stack_T st); Remove the top item from the stack
and return it
int is_empty(stack_T st); Check whether stack is empty
Stack.h to be used in abstract stack implementation
#include <stack.h>
stack_T s = create_stack();
int val = 09;
s = push(t, &val);
void *v = pop(s);
if (is_empty(s))
{ … }
Implementation of abstract stack using stack.h
© Oxford University Press 2011. All rights reserved.
ALGORITHM
• The typical meaning of "algorithm" is a formally defined procedure for
performing some calculation.
• An algorithm provides a blueprint to write a program to solve a
particular problem.
• It is considered to be an effective procedure for solving a problem in
finite number of steps. That is, a well-defined algorithm always
provides an answer and is guaranteed to terminate.
• Algorithms are mainly used to achieve software re-use. Once we
have an idea or a blueprint of a solution, we can implement in any
high level language like C, C++, Java, so on and so forth.
• Write an algorithm to find whether a number is even or odd
• Step 1: Input the first number as A
• Step 2: IF A%2 =0
• Then Print "EVEN"
• ELSE
• PRINT "ODD"
• Step 3: END
© Oxford University Press 2011. All rights reserved.
TIME AND SPACE COMPLEXITY
OF ALGORITHM
• To analyze an algorithm means determining the amount of resources (such
as time and storage) needed to execute it. Algorithms are generally designed
to work with an arbitrary number of inputs, so the efficiency or complexity of
an algorithm is stated in terms of time complexity and space complexity.
• The time Complexity of an algorithm is basically the running time of the
program as a function of the input size. On similar grounds, space complexity
of an algorithm is the amount of computer memory required during the
program execution, as a function of the input size
• Time complexity of an algorithm depends on the number of machine
instructions in which a program executes. This number is primarily dependant
on the size of the program's input and the algorithm used.
• The space needed by a program depends on:
Fixed part, that varies with problem to problem. It includes space needed for
storing instructions, constants, variables and structured variables (like arrays,
structures)
Variable part, that varies from program to program. It includes space needed
for recursion stack, and for structured variables that are allocated space
dynamically during the run-time of the program.
© Oxford University Press 2011. All rights reserved.
Calculating Algorithm Efficiency
• If a function is linear (without any loops or recursions), the efficiency of
that algorithm or the running time of that algorithm can be given as the
number of instructions it contains.
• If an algorithm contains certain loops or recursive functions then the
efficiency of that algorithm may vary depending on the number of loops
and the running time of each loop in the algorithm.
• The efficiency of an algorithm is expressed in terms of the number of
elements that has to be processed. So, if n is the number of elements,
then the efficiency can be stated as
f(n) = efficiency
Linear loops
for(i=0;i<n;i++)
statement block
f(n) = efficiency
Logarithmic Loops
for(i=1;i<n;i*=2) for(i=0;i<n;i/=2)
statement block; statement block;
f(n) = log n f(n) = log n
© Oxford University Press 2011. All rights reserved.
Calculating Algorithm Efficiency
contd.Nested Loops
• Total no. of iterations = no. of iterations in inner loop * no. of iterations in outer
loop
• In case of nested loops, we will analyze the efficiency of the algorithm based on
whether it’s a linear logarithmic, quadratic or dependant quadratic nested loop.
Linear logarithmic
for(i=0;i<n;i++)
for(j=1; j<n;j*=2)
statement block;
f(n)= n log n
Quadratic Loop
for(i=0;i<n;i++)
for(j=1; j<n;j++)
statement block;
f(n) = n * n
Dependent Quadratic
for(i=0;i<n;i++)
for(j=1; j<i;j++)
statement block;
f(n) = n (n + 1)/2
OR f(n) = n (n + 1)/2
© Oxford University Press 2011. All rights reserved.
BIG OH NOTATION
• Big-Oh notation where the "O" stands for "order of" is concerned with
what happens for very large values of n.
• For example, if a sorting algorithm performs n2
operations to sort just n
elements, then that algorithm would be described as an O(n2
)
algorithm.
• When expressing complexity using Big Oh notation, constant
multipliers are ignored. So a O(4n) algorithm is equivalent to O(n),
which is how it should be written.
• If f(n) and g(n) are functions defined on positive integer number n, then
f(n) = O(g(n))
• That is, f of n is big oh of g of n if and only if there exists positive
constants c and n, such that
f (n) ≤ Cg(n) ≤ n
• This means that for large amounts of data, f(n) will grow no more than
a constant factor than g(n). Hence, g provides an upper bound.
© Oxford University Press 2011. All rights reserved.
Categories of Algorithms
• Constant time algorithm that have running time complexity given as
O(1)
• Linear time algorithm that have running time complexity given as
O(n)
• Logarithmic time algorithm that have running time complexity given
as O(log n)
• Polynomial time algorithm that have running time complexity given
as O(nk) where k>1
• Exponential time algorithm that have running time complexity given
as O(2n
)
Number of operations for different functions of n
n O(1) O(log n) O(n) O(n log n) O(n2
) O(n3
)
1 1 1 1 1 1 1
2 1 1 2 2 4 8
4 1 2 4 8 16 64
8 1 3 8 24 64 512
16 1 4 16 64 256 4,096
© Oxford University Press 2011. All rights reserved.
NP- COMPLETE
• In computational complexity theory, the complexity class NP-C (Non-
deterministic Polynomial time- Complete), is a class of problems that
exhibits two properties:
• A set of problems is said to be NP is any given solution to the problem
can be verified quickly in polynomial time.
• If the problem can be solved quickly in polynomial time, then it implies
that every problem in NP can be solved in polynomial time
• Even if a given solution to a problem can be verified quickly, there is
no known efficient way to locate a solution in the first place. Therefore,
it is not wrong to say that solutions to NP-complete problems may not
even be known.
• A problem p in NP is also in NPC if and only if every other problem in
NP can be transformed into p in polynomial time.
• if any single problem in NP-complete can be solved quickly, then
every problem in NP can also be quickly solved.

More Related Content

What's hot

Queue ppt
Queue pptQueue ppt
Queue ppt
SouravKumar328
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
Poojith Chowdhary
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
bca2010
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
Elavarasi K
 
linked list
linked list linked list
linked list
Mohaimin Rahat
 
Abstract Data Types
Abstract Data TypesAbstract Data Types
Abstract Data Types
karthikeyanC40
 
TYPES DATA STRUCTURES( LINEAR AND NON LINEAR)....
TYPES DATA STRUCTURES( LINEAR AND NON LINEAR)....TYPES DATA STRUCTURES( LINEAR AND NON LINEAR)....
TYPES DATA STRUCTURES( LINEAR AND NON LINEAR)....
Shail Nakum
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
Sowmya Jyothi
 
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
sumitbardhan
 
Array data structure
Array data structureArray data structure
Array data structure
maamir farooq
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
Adam Mukharil Bachtiar
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
Mohammed Sikander
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
Julie Iskander
 
Linked list
Linked listLinked list
Linked list
Trupti Agrawal
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysissumitbardhan
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
Muhammad Hammad Waseem
 
Arrays
ArraysArrays
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
Lemia Algmri
 
Sorting
SortingSorting
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
Apurbo Datta
 

What's hot (20)

Queue ppt
Queue pptQueue ppt
Queue ppt
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
 
linked list
linked list linked list
linked list
 
Abstract Data Types
Abstract Data TypesAbstract Data Types
Abstract Data Types
 
TYPES DATA STRUCTURES( LINEAR AND NON LINEAR)....
TYPES DATA STRUCTURES( LINEAR AND NON LINEAR)....TYPES DATA STRUCTURES( LINEAR AND NON LINEAR)....
TYPES DATA STRUCTURES( LINEAR AND NON LINEAR)....
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
 
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
 
Array data structure
Array data structureArray data structure
Array data structure
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Linked list
Linked listLinked list
Linked list
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
 
Arrays
ArraysArrays
Arrays
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
Sorting
SortingSorting
Sorting
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 

Viewers also liked

358 33 powerpoint-slides_1-introduction-c_chapter-1
358 33 powerpoint-slides_1-introduction-c_chapter-1358 33 powerpoint-slides_1-introduction-c_chapter-1
358 33 powerpoint-slides_1-introduction-c_chapter-1
sumitbardhan
 
358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10
sumitbardhan
 
358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2
sumitbardhan
 
358 33 powerpoint-slides_13-graphs_chapter-13
358 33 powerpoint-slides_13-graphs_chapter-13358 33 powerpoint-slides_13-graphs_chapter-13
358 33 powerpoint-slides_13-graphs_chapter-13
sumitbardhan
 
358 33 powerpoint-slides_3-pointers_chapter-3
358 33 powerpoint-slides_3-pointers_chapter-3358 33 powerpoint-slides_3-pointers_chapter-3
358 33 powerpoint-slides_3-pointers_chapter-3
sumitbardhan
 
358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5
sumitbardhan
 
358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7
sumitbardhan
 
358 33 powerpoint-slides_8-linked-lists_chapter-8
358 33 powerpoint-slides_8-linked-lists_chapter-8358 33 powerpoint-slides_8-linked-lists_chapter-8
358 33 powerpoint-slides_8-linked-lists_chapter-8
sumitbardhan
 
358 33 powerpoint-slides_6-strings_chapter-6
358 33 powerpoint-slides_6-strings_chapter-6358 33 powerpoint-slides_6-strings_chapter-6
358 33 powerpoint-slides_6-strings_chapter-6
sumitbardhan
 
358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14
sumitbardhan
 
358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9
sumitbardhan
 
Mapreduce Algorithms
Mapreduce AlgorithmsMapreduce Algorithms
Mapreduce Algorithms
Amund Tveit
 
Hadoop MapReduce Fundamentals
Hadoop MapReduce FundamentalsHadoop MapReduce Fundamentals
Hadoop MapReduce Fundamentals
Lynn Langit
 
358 33 powerpoint-slides_15-hashing-collision_chapter-15
358 33 powerpoint-slides_15-hashing-collision_chapter-15358 33 powerpoint-slides_15-hashing-collision_chapter-15
358 33 powerpoint-slides_15-hashing-collision_chapter-15
sumitbardhan
 

Viewers also liked (14)

358 33 powerpoint-slides_1-introduction-c_chapter-1
358 33 powerpoint-slides_1-introduction-c_chapter-1358 33 powerpoint-slides_1-introduction-c_chapter-1
358 33 powerpoint-slides_1-introduction-c_chapter-1
 
358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10
 
358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2
 
358 33 powerpoint-slides_13-graphs_chapter-13
358 33 powerpoint-slides_13-graphs_chapter-13358 33 powerpoint-slides_13-graphs_chapter-13
358 33 powerpoint-slides_13-graphs_chapter-13
 
358 33 powerpoint-slides_3-pointers_chapter-3
358 33 powerpoint-slides_3-pointers_chapter-3358 33 powerpoint-slides_3-pointers_chapter-3
358 33 powerpoint-slides_3-pointers_chapter-3
 
358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5
 
358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7
 
358 33 powerpoint-slides_8-linked-lists_chapter-8
358 33 powerpoint-slides_8-linked-lists_chapter-8358 33 powerpoint-slides_8-linked-lists_chapter-8
358 33 powerpoint-slides_8-linked-lists_chapter-8
 
358 33 powerpoint-slides_6-strings_chapter-6
358 33 powerpoint-slides_6-strings_chapter-6358 33 powerpoint-slides_6-strings_chapter-6
358 33 powerpoint-slides_6-strings_chapter-6
 
358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14
 
358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9
 
Mapreduce Algorithms
Mapreduce AlgorithmsMapreduce Algorithms
Mapreduce Algorithms
 
Hadoop MapReduce Fundamentals
Hadoop MapReduce FundamentalsHadoop MapReduce Fundamentals
Hadoop MapReduce Fundamentals
 
358 33 powerpoint-slides_15-hashing-collision_chapter-15
358 33 powerpoint-slides_15-hashing-collision_chapter-15358 33 powerpoint-slides_15-hashing-collision_chapter-15
358 33 powerpoint-slides_15-hashing-collision_chapter-15
 

Similar to 358 33 powerpoint-slides_4-introduction-data-structures_chapter-4

UNIT 1.pptx
UNIT 1.pptxUNIT 1.pptx
UNIT 1.pptx
JohnStuart83
 
Data Structures in C
Data Structures in CData Structures in C
Data Structures in C
Jabs6
 
Data Structure & Algorithm.pptx
Data Structure & Algorithm.pptxData Structure & Algorithm.pptx
Data Structure & Algorithm.pptx
Mumtaz
 
Data Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptxData Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptx
GIRISHKUMARBC1
 
Learn advanced java programming
Learn advanced java programmingLearn advanced java programming
Learn advanced java programming
TOPS Technologies
 
DS Module 1.pptx
DS Module 1.pptxDS Module 1.pptx
DS Module 1.pptx
SaralaT3
 
1.Introduction to Data Structures and Algorithms.pptx
1.Introduction to Data Structures and Algorithms.pptx1.Introduction to Data Structures and Algorithms.pptx
1.Introduction to Data Structures and Algorithms.pptx
BlueSwede
 
DS Module 1.pptx
DS Module 1.pptxDS Module 1.pptx
DS Module 1.pptx
sarala9
 
Lecture 1 and 2
Lecture 1 and 2Lecture 1 and 2
Lecture 1 and 2
SaheedTundeZubairSTA
 
Data structures and algorithms short note (version 14).pd
Data structures and algorithms short note (version 14).pdData structures and algorithms short note (version 14).pd
Data structures and algorithms short note (version 14).pd
Nimmi Weeraddana
 
intr_ds.ppt
intr_ds.pptintr_ds.ppt
intr_ds.ppt
PrakharNamdev3
 
STACK.pptx
STACK.pptxSTACK.pptx
STACK.pptx
Dr.Shweta
 
Data structure
Data structureData structure
Data structure
Muhammad Farhan
 
Unit 2 linear data structures
Unit 2   linear data structuresUnit 2   linear data structures
Unit 2 linear data structures
Senthil Murugan
 
Data Structures & Algorithms
Data Structures & AlgorithmsData Structures & Algorithms
Data Structures & Algorithms
Muhammad Jahanzaib
 
TSAT Presentation1.pptx
TSAT Presentation1.pptxTSAT Presentation1.pptx
TSAT Presentation1.pptx
Rajitha Reddy Alugati
 
Data structures - Introduction
Data structures - IntroductionData structures - Introduction
Data structures - Introduction
DeepaThirumurugan
 
Data structure and algorithm.
Data structure and algorithm. Data structure and algorithm.
Data structure and algorithm.
Abdul salam
 
Introduction to data structures (ss)
Introduction to data structures (ss)Introduction to data structures (ss)
Introduction to data structures (ss)
Madishetty Prathibha
 
Data structure
 Data structure Data structure
Data structure
Shahariar limon
 

Similar to 358 33 powerpoint-slides_4-introduction-data-structures_chapter-4 (20)

UNIT 1.pptx
UNIT 1.pptxUNIT 1.pptx
UNIT 1.pptx
 
Data Structures in C
Data Structures in CData Structures in C
Data Structures in C
 
Data Structure & Algorithm.pptx
Data Structure & Algorithm.pptxData Structure & Algorithm.pptx
Data Structure & Algorithm.pptx
 
Data Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptxData Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptx
 
Learn advanced java programming
Learn advanced java programmingLearn advanced java programming
Learn advanced java programming
 
DS Module 1.pptx
DS Module 1.pptxDS Module 1.pptx
DS Module 1.pptx
 
1.Introduction to Data Structures and Algorithms.pptx
1.Introduction to Data Structures and Algorithms.pptx1.Introduction to Data Structures and Algorithms.pptx
1.Introduction to Data Structures and Algorithms.pptx
 
DS Module 1.pptx
DS Module 1.pptxDS Module 1.pptx
DS Module 1.pptx
 
Lecture 1 and 2
Lecture 1 and 2Lecture 1 and 2
Lecture 1 and 2
 
Data structures and algorithms short note (version 14).pd
Data structures and algorithms short note (version 14).pdData structures and algorithms short note (version 14).pd
Data structures and algorithms short note (version 14).pd
 
intr_ds.ppt
intr_ds.pptintr_ds.ppt
intr_ds.ppt
 
STACK.pptx
STACK.pptxSTACK.pptx
STACK.pptx
 
Data structure
Data structureData structure
Data structure
 
Unit 2 linear data structures
Unit 2   linear data structuresUnit 2   linear data structures
Unit 2 linear data structures
 
Data Structures & Algorithms
Data Structures & AlgorithmsData Structures & Algorithms
Data Structures & Algorithms
 
TSAT Presentation1.pptx
TSAT Presentation1.pptxTSAT Presentation1.pptx
TSAT Presentation1.pptx
 
Data structures - Introduction
Data structures - IntroductionData structures - Introduction
Data structures - Introduction
 
Data structure and algorithm.
Data structure and algorithm. Data structure and algorithm.
Data structure and algorithm.
 
Introduction to data structures (ss)
Introduction to data structures (ss)Introduction to data structures (ss)
Introduction to data structures (ss)
 
Data structure
 Data structure Data structure
Data structure
 

Recently uploaded

Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 

Recently uploaded (20)

Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 

358 33 powerpoint-slides_4-introduction-data-structures_chapter-4

  • 1. © Oxford University Press 2011. All rights reserved. Data Structures Using CData Structures Using C Reema TharejaReema Thareja, Assistant Professor, Institute of Information Technology and Management, New Delhi
  • 2. © Oxford University Press 2011. All rights reserved. CHAPTER 4 INTRODUCTION TO DATA STRUCTURES
  • 3. © Oxford University Press 2011. All rights reserved. INTRODUCTION • A data structure is nothing but an arrangement of data either in computer's memory or on the disk storage. • Some common examples of data structure includes arrays, linked lists, queues, stacks, binary trees, and hash tables. • Data structures are widely applied in areas like: a. Compiler design b. Operating system c. Statistical analysis package d. DBMS e. Numerical analysis f. Simulation g. Artificial Intelligence h. Graphics
  • 4. © Oxford University Press 2011. All rights reserved. DIFFERENT TYPES OF DATA STRUCTURES
  • 5. © Oxford University Press 2011. All rights reserved. Arrays • 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). • Arrays are declared using the following syntax. type name[size]; 1st element 2nd element 3rd element 4th element 5th element 6th element 7th element 8th element 9th element 10th element marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7] marks[8] marks[9] The limitations with arrays include: Arrays are of fixed size. Data elements are stored in continuous memory locations which may not be available always. Adding and removing of elements is problematic because of shifting the elements from their positions.
  • 6. © Oxford University Press 2011. All rights reserved. Linked List • Linked list is a very flexible dynamic data structure in which elements can be added to or deleted from anywhere at will. • In contrast to using static arrays, a programmer need not worry about how many elements will be stored in the linked list. This feature enables the programmers to write robust programs which require much less maintenance. • In a linked list, each element (is called a node) is allocated space as it is added to the list. Every node in the list points to the next node in the list. Therefore, in a linked list every node contains two types of information: The data stored in the node and A pointer or link to the next node in the list • Advantage: Provides quick insert, and delete operations • Disadvantage: Slower search operation and requires more memory space 1 2 3 4 5 6 7 X
  • 7. © Oxford University Press 2011. All rights reserved. Stack • In computer’s memory stacks can be represented as a linear array. • Every stack has a variable TOP associated with it. Top is 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. There is another variable MAX which will be used to store the maximum number of elements that the stack can store. • If TOP = NULL, then it indicates that the stack is empty and if TOP = MAX, then the stack is full. • If TOP = -1, it indicates that there is no element in the stack. • Advantage: Last-in, first-out access (LIFO) • Disadvantage: Slow access to other elements is full. A AB ABC ABCD ABCDE 0 1 2 3 TOP = 4 5 6 7 8 9
  • 8. © Oxford University Press 2011. All rights reserved. Queue • A queue is a FIFO (First In First Out) data structure in which the element that was inserted first is the first one to be taken out. • The elements in a queue are added at one end called the rear and removed from the other one end called front. Like stacks, queues can be implemented either using arrays or linked lists. • When Rear = MAX – 1, where MAX is the size of the queue that is, MAX specifies the maximum number of elements in the queue. • If front = -1 and rear = -1, this means there is no element in the queue. • Advantage: Provides first-in, first-out data access • Disadvantage: Slow access to other items 9 7 18 14 36 45 0 FRONT = 1 2 3 4 REAR = 6 7 8 9
  • 9. © Oxford University Press 2011. All rights reserved. Tree • A binary tree is a data structure which is defined as a collection of elements called nodes. • Every node contains a "left" pointer, a "right" pointer, and a data element. • Every binary tree has a root element pointed by a "root" pointer. The root element is the topmost node in the tree. • If root = NULL, then it means the tree is empty. • Advantages: Provides quick search, insert and delete operations • Disadvantage: Complicated deletion algorithm 1 32 4 8 5 6 7 12 10 1 1 ROOT NODE T2T1 9
  • 10. © Oxford University Press 2011. All rights reserved. Graph • A graph is a collection of vertices (also called nodes) and edges that connect these vertices. • A graph is often viewed as a generalization of the tree structure, where instead of a having a purely parent-to-child relationship between tree nodes, any kind of complex relationships between the nodes can be represented. • While in trees, the nodes can have many children but only one parent, a graph on the other hand relaxes all such kinds of restrictions. • unlike trees, graphs do not have any root node. Rather, every node in the graph can be connected with any other node in the graph. When two nodes are connected via an edge, the two nodes are known as neighbors. • Advantages: Best models real-world situations • Disadvantages: Some algorithms are slow and very complex A D B C E
  • 11. © Oxford University Press 2011. All rights reserved. ABSTRACT DATA TYPE • An Abstract Data Type (ADT) is the way at which we look at a data structure, focusing on what it does and ignoring how it does its job. • For example, stack and queue are perfect examples of an abstract data type. We can implement both these ADTs using an array or a linked list. This demonstrates the "abstract" nature of stacks and queues. • In C, an Abstract Data Type can be a structure considered without regard to its implementation. It can be thought of as a "description" of the data in the structure with a list of operations that can be performed on the data within that structure. • The end user is not concerned about the details of how the methods carry out their tasks. They are only aware of the methods that are available to them and are concerned only about calling those methods and getting back the results but not HOW they work.
  • 12. © Oxford University Press 2011. All rights reserved. CODE DESCRIPTION typedef struct stack_Rep stack_Rep; instance representation record typedef stack_Rep *stack_T; pointer to a stack instance typedef void *stack_Item; value to be stored on stack stack_T create_stack(void); Create an empty stack instance void push(stack_T st, stack_Item v) Add an item at the top of the stack stack_Item pop(stack_T st); Remove the top item from the stack and return it int is_empty(stack_T st); Check whether stack is empty Stack.h to be used in abstract stack implementation #include <stack.h> stack_T s = create_stack(); int val = 09; s = push(t, &val); void *v = pop(s); if (is_empty(s)) { … } Implementation of abstract stack using stack.h
  • 13. © Oxford University Press 2011. All rights reserved. ALGORITHM • The typical meaning of "algorithm" is a formally defined procedure for performing some calculation. • An algorithm provides a blueprint to write a program to solve a particular problem. • It is considered to be an effective procedure for solving a problem in finite number of steps. That is, a well-defined algorithm always provides an answer and is guaranteed to terminate. • Algorithms are mainly used to achieve software re-use. Once we have an idea or a blueprint of a solution, we can implement in any high level language like C, C++, Java, so on and so forth. • Write an algorithm to find whether a number is even or odd • Step 1: Input the first number as A • Step 2: IF A%2 =0 • Then Print "EVEN" • ELSE • PRINT "ODD" • Step 3: END
  • 14. © Oxford University Press 2011. All rights reserved. TIME AND SPACE COMPLEXITY OF ALGORITHM • To analyze an algorithm means determining the amount of resources (such as time and storage) needed to execute it. Algorithms are generally designed to work with an arbitrary number of inputs, so the efficiency or complexity of an algorithm is stated in terms of time complexity and space complexity. • The time Complexity of an algorithm is basically the running time of the program as a function of the input size. On similar grounds, space complexity of an algorithm is the amount of computer memory required during the program execution, as a function of the input size • Time complexity of an algorithm depends on the number of machine instructions in which a program executes. This number is primarily dependant on the size of the program's input and the algorithm used. • The space needed by a program depends on: Fixed part, that varies with problem to problem. It includes space needed for storing instructions, constants, variables and structured variables (like arrays, structures) Variable part, that varies from program to program. It includes space needed for recursion stack, and for structured variables that are allocated space dynamically during the run-time of the program.
  • 15. © Oxford University Press 2011. All rights reserved. Calculating Algorithm Efficiency • If a function is linear (without any loops or recursions), the efficiency of that algorithm or the running time of that algorithm can be given as the number of instructions it contains. • If an algorithm contains certain loops or recursive functions then the efficiency of that algorithm may vary depending on the number of loops and the running time of each loop in the algorithm. • The efficiency of an algorithm is expressed in terms of the number of elements that has to be processed. So, if n is the number of elements, then the efficiency can be stated as f(n) = efficiency Linear loops for(i=0;i<n;i++) statement block f(n) = efficiency Logarithmic Loops for(i=1;i<n;i*=2) for(i=0;i<n;i/=2) statement block; statement block; f(n) = log n f(n) = log n
  • 16. © Oxford University Press 2011. All rights reserved. Calculating Algorithm Efficiency contd.Nested Loops • Total no. of iterations = no. of iterations in inner loop * no. of iterations in outer loop • In case of nested loops, we will analyze the efficiency of the algorithm based on whether it’s a linear logarithmic, quadratic or dependant quadratic nested loop. Linear logarithmic for(i=0;i<n;i++) for(j=1; j<n;j*=2) statement block; f(n)= n log n Quadratic Loop for(i=0;i<n;i++) for(j=1; j<n;j++) statement block; f(n) = n * n Dependent Quadratic for(i=0;i<n;i++) for(j=1; j<i;j++) statement block; f(n) = n (n + 1)/2 OR f(n) = n (n + 1)/2
  • 17. © Oxford University Press 2011. All rights reserved. BIG OH NOTATION • Big-Oh notation where the "O" stands for "order of" is concerned with what happens for very large values of n. • For example, if a sorting algorithm performs n2 operations to sort just n elements, then that algorithm would be described as an O(n2 ) algorithm. • When expressing complexity using Big Oh notation, constant multipliers are ignored. So a O(4n) algorithm is equivalent to O(n), which is how it should be written. • If f(n) and g(n) are functions defined on positive integer number n, then f(n) = O(g(n)) • That is, f of n is big oh of g of n if and only if there exists positive constants c and n, such that f (n) ≤ Cg(n) ≤ n • This means that for large amounts of data, f(n) will grow no more than a constant factor than g(n). Hence, g provides an upper bound.
  • 18. © Oxford University Press 2011. All rights reserved. Categories of Algorithms • Constant time algorithm that have running time complexity given as O(1) • Linear time algorithm that have running time complexity given as O(n) • Logarithmic time algorithm that have running time complexity given as O(log n) • Polynomial time algorithm that have running time complexity given as O(nk) where k>1 • Exponential time algorithm that have running time complexity given as O(2n ) Number of operations for different functions of n n O(1) O(log n) O(n) O(n log n) O(n2 ) O(n3 ) 1 1 1 1 1 1 1 2 1 1 2 2 4 8 4 1 2 4 8 16 64 8 1 3 8 24 64 512 16 1 4 16 64 256 4,096
  • 19. © Oxford University Press 2011. All rights reserved. NP- COMPLETE • In computational complexity theory, the complexity class NP-C (Non- deterministic Polynomial time- Complete), is a class of problems that exhibits two properties: • A set of problems is said to be NP is any given solution to the problem can be verified quickly in polynomial time. • If the problem can be solved quickly in polynomial time, then it implies that every problem in NP can be solved in polynomial time • Even if a given solution to a problem can be verified quickly, there is no known efficient way to locate a solution in the first place. Therefore, it is not wrong to say that solutions to NP-complete problems may not even be known. • A problem p in NP is also in NPC if and only if every other problem in NP can be transformed into p in polynomial time. • if any single problem in NP-complete can be solved quickly, then every problem in NP can also be quickly solved.