SlideShare a Scribd company logo
1 of 48
Data Structure and
Algorithm
Lecture No 1
Prepared by Muhammad Farhan
Text Book
• Text book
Data Structures and Algorithms By Mark Allen Weise
• Reference Book
Data structure in C++ by Muhammad Tauqeer Aikman series
Data vs. Information
Data
• A set of values have no
meaning
Information
• Meaningful processed data
Data vs. Information
• Data: 51007
• Information:
• 5/10/07 The date of your final exam.
Data Structure
• Data structure defines a particular way to organize a data in a computer so
that it can be used efficiently.
• Data structure are a containers which hold a data.
• Efficient mean take minimum time and minimum memory space.
• Data structures are used in almost every program
Application of Data Structure
• Data structures are widely applied in the following areas:
• Compiler design
• Operating system
• DBMS
• Artificial intelligence
Classification of Data Structure
• Data structure are normally divided into two broad categories:
• Primitive Data Structure
• Non-Primitive Data Structure
Classification of Data Structure
Data structure
Primitive DS Non-Primitive DS
Integer Float Character PointerFloatInteger Float
Classification of Data Structure
Non-Primitive DS
Linear Non-Linear
Array
Link List Stack
Queue Graph Trees
Primitive Data Structure
• Primitive data structures are the fundamental data types which are supported
by a programming language. Some basic data types are integer, real, character,
float. The terms ‘data type’, ‘basic data type’, and ‘primitive data type’ are
often used interchangeably
Non Primitive Data Structure
• Non-primitive data structures are those data structures which are created
using primitive data structures. Examples of such data structures include
linked lists, stacks, trees, and graphs. Non-primitive data structures can
further be classified into two categories: linear and non-linear data structures.
Linear Data Structure
• If the elements of a data structure are stored in a linear or sequential order,
then it is a linear data structure. Examples include arrays, linked lists, stacks,
and queues
Non Linear Data Structure
• However, if the elements of a data structure are not stored in a sequential
order, then it is a non-linear data structure. Examples include trees and
graphs.
Operation on Data Structure
• The data in data structure is processed using certain operations. Some commonly used
operations performed on data structures are
• Inserting: adding new data items into a data structure is called inserting
• Deleting: Removing data items from a data structures is called deleting.
• Searching : Find specific data items in a data structure is called searching
• Traversing: Accessing each item in a data structure exactly once for processing is called
traversing.
• Sorting: Arranging data items in a data structure into a specific order is called sorting
• Merging: Combining two list of data items into a single data list is called merging.
• The operation through which data structure is created is called the creation
operation.
• For example, to create a variable of integer type in C++, the statement is
written as :
• Int a, b;
• The above statement creates a memory space of two bytes for each variable a
& b.
Algorithm
• The step by step procedure to solve a particular problem is called algorithm.
• For example
• A recipe for cooking a cake is an algorithm
• Program=algorithm + Data Structure
How to write Algorithm?
• Name of algorithm : Every Algorithm is given a name that represents the
purpose of the algorithm. For example: An algorithm to find the sum of two
numbers is given the name “’sum’’.
• Introductory comment: The algorithm name is followed by a brief
description of the tasks that the algorithm performs.
• Steps: The algorithm consists of a sequence of numbered steps. Each step
described one task to be performed.
• Comments: Comments are used to explain purpose of a step or statement. These
may be given at the end of each statement or at the beginning of the step. In C++
the comments are given starting with double slash(//). In algorithms , these are
written between square brackets [ ].
• Variable Name: A variable is an entity that possesses a value. The name of the
variable is chosen according to the name of value it is to hold. For example, a
variable that is used to hold the maximum value is named as Max. Usually , variable
names in algorithm are written in capital letters. A variable name consists of letters,
numeric digits and some special characters. It always begins with a letter. Blank
spaces are not allowed within variable names.
• Operators: Arithmetic (+,-,*,/), Relational(<,>,>=,<=, etc.) and logical (AND,
OR, NOT) operators are used to describe various mathematical operations.
• Assignment Statement: The assignment statement is used to evaluate an
expression and assign the calculated value to the variable. The assignment operator
‘=’ sign is used for this purpose. For example evaluate the expression N+M assign
its value to variable S. the assignment statement is written as S= N+M
Input Statement
• Input or read statement is used in algorithms to enter data into variable. The
statement is written as:
Input variable-Name
For example , to input a value into variable N, the input statement is written
as:
Input N
Output Statement
• To Print a message or contents of a variable, PRINT statement is used with
the following format.
PRINT message or variable name
The message is written within double quotes. To print the contents of variable ,
its is written without double quotes. In C++, usually the ‘cin’ object is used
to get input from the keyboard and ‘cout’ object is used to print the output
on the screen.
Algorithm example
Write an algorithm to get two numbers and to calculate their sum
• STEP 1 : START
• STEP 2 : INPUT FIRST NUMBER N
• STEP 3 : INPUT SECOND NUMBER M
• STEP 4 : ADD N AND M AND STORE RESULT IN S i.e S=N+M
• STEP 5 : PRINT S
• STEP 6 : STOP
Algorithm-Sum
Selection statement
• Selection statement are used for making decisions. The decision is made by testing a
given condition. After testing a condition, a statement or set of statements are
executed or ignored. The structure that implements this logic in a programming
language is called conditional structure. A conditional statement is represented by
the IF structure. It has one of the following two forms:
• 1- IF condition THEN
statement (s)
END IF
Cont…......
• 2- IF condition THEN
BLOCK A
ELSE
BLOCK B
END IF
Selection Statement Example
Write an algorithm to find the greater of the two given numbers
• STEP 1 : START
• STEP 2 : INPUT TWO NUMBER A AND B
• STEP 3 : IF A>B THEN
• STEP 4: PRINT “A IS GREATER”
• STEP 5 : ELSE
• STEP 6 : PRINT “B IS GREATER”
• STEP 7: EXIT
Algorithm-Greater number
Looping Statement
• The looping statement are used to execute certain statement(s) repeatedly.
The statement that are executed repeatedly are called body of the loop. In
algorithm ‘Repeat’ statement is used to execute the statement repeatedly. The
Repeat statement has two different forms
• Repeat for loop
• Repeat while loop
Repeat For Loop
• Repeat for loop is used to execute statements for a specified number of
times. This loop is also called the counter loop. Its general format is :
• Repeat FOR index= I-value TO F-value By S-value
• Where
• I-value represent the initial value for index variable
• F-value represent the final value
• S-value represent the step value i.e. Increment/ decrement value.
Repeat For Example
Write an algorithm to print first ten numbers using REPEAT FOR
strucute
• STEP 1: START
• STEP 2: REPEAT STEP 3 FOR C=1 TO 10 BY 1
• STEP 3: PRINT C [END OF STEP 2 LOOP]
• STEP 4: EXIT
Algorithm-Natural Numbers
Repeat while Structure
• This loop structure is used to execute a statement or a set of statements
repeatedly until the given condition remains true. It is also referred to as
conditional loop. Its general syntax is
• REPEAT WHILE ( condition)
Body of the loop
[end of loop]
Repeat While Example
Write an algorithm to print first ten numbers using REPEAT WHILE strucute
• STEP 1: START
• STEP 2: C=1
• STEP 3: REPEAT STEP 4 TO 5 WHILE (C<=10)
• STEP 4: PRINT C
• STEP 5 C=C+1 [END OF STEP-3 LOOP]
• STEP 6: EXIT
Algorithm-Natural Numbers
Simple/Structure data types
• So far, we have seen only simple data types, such as int, float, and
char.
• Simple variables can hold only one value at any time during program
execution, although that value may change.
• A Structure data type is a data type that can hold multiple values at
the same time. (Synonyms: complex data type, composite data
type)
• The array is one kind of structure data types.
Arrays
• An array is a group of related elements that all have the same
name and the same data type.
• Arrays are static in that they remain the same size throughout
program execution.
• An array’s data items are stored contiguously in memory.
• Each of the data items is known as an element of the array.
Each element can be accessed individually.
Array Declaration and Initialization
int numbers[ 5 ] ;
• The name of this array is “numbers”.
• Initializing an array may be done with an array
initialization, as in :
int numbers[ 5 ] = { 5, 2, 6, 9, 3 } ;
5 2 6 9 3numbers
Accessing Array Elements
• Each element in an array has a subscript (index) associated with it.
• Subscripts are integers and always begin at zero.
• Values of individual elements can be accessed by indexing into the
array. For example,
numbers[ 2 ]
would give the output
The third element = 6.
5 2 6 9 3numbers
0 1 2 3 4
Types of the array
• One-dimensional Array
• Two dimensional Array
One Dimensional Array
• When the consecutive homogeneous data is stored in the form of single row
or column then it is called one dimensional array.
Two dimensional array
• When data is stored in both rows and Coloums then its is called two
dimensional array. It is also called matrices.
How to declare a two dimensional
array?
A two dimensional array:
int arr[2][3];
This array has total 2*3 = 6 elements
Initialization:
We can initialize the array
int arr[2][3] = {10, 11 ,12 ,20 ,21 , 22};
Operation on Array Data Structures
• Traversal
• Insertion
• Deletion
Traversal
Traversal: To print all the element one by one in array.
 
Traversal Algorithm
Algorithm-Traverse (LB, UB, A)
Introductory comment
Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses
A applying an operation process to each element of A.
Step no 1. Start
Step no 2. Repeat steps 3 For I=LB to UB by 1
Step no 3. Print A[I]
[End of step 2 loop]
Step no 4. Exit
Insertion Operation
• Insertion operation is used to insert a new element at specific position in to
one dimensional array. 
In order to insert a new element into one dimensional array we have to create
space for new element. 
Suppose there are N elements in an array and we want to insert a new
element between first and second element. We have to move last N-1
elements down in order to create space for the new element.
Insertion of element at the end of the
array
• Algorithm-Insert(A,N, Item)
• Introductory comment:
• This algorithm is used to insert an element at the end of the array where
• A=array name N=total element item= the element which is to be inserted
• Step no1. Set A[N+1]=item
• Step no2. Set N=N+1
• Step no3. stop
Insertion algorithm at middle
Algorithm-Insertion(A, N,K, ITEM)
Introductory Comment
Let LA be a Linear Array (unordered) with N elements and K is a positive integer such that K<=N.
Following is the algorithm where ITEM is inserted into the Kth
position of LA.
Algorithm
Step no1. Start
Step no2. [Initialize counter] Set J=K
Step no 3. Repeat steps 4 and 5 while N >=J
Step no4. [Move the jth element downward]Set LA[N+ 1]= LA[N]
Step no5. [decrease counter]Set N= N-1
[end of step 3 loop]
Step no 6. [Insert Item] set LA[K]=ITEM
Step no 7. Stop
Deletion operation in array
• Deletion is the operation that removes an element from a given location of
the array
• Delete an element at the end of array
• Delete an element from specific location
Delete a specific element from the array
• Algorithm-Delete
• Introductory comment
• Consider LA is a linear array with N elements and K is a positive integer
such that K<=N. Following is the algorithm to delete an element available
at the Kth
position of LA.
• Step no1. set J=k
• Step no2. Repeat Step no 3 while J<=N-1
• Step no3. Set A[j]=A[j+1] [end of loop]
• Step no4 Set J=J+1
• Step no4. Set N=N-1[reset the number of element in array]
• Step no5. Exit

More Related Content

What's hot

Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysissumitbardhan
 
Ppt on Linked list,stack,queue
Ppt on Linked list,stack,queuePpt on Linked list,stack,queue
Ppt on Linked list,stack,queueSrajan Shukla
 
Data structure and algorithm using java
Data structure and algorithm using javaData structure and algorithm using java
Data structure and algorithm using javaNarayan Sau
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - GraphMadhu Bala
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsJulie Iskander
 
Queue data structure
Queue data structureQueue data structure
Queue data structureanooppjoseph
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structureMAHALAKSHMI P
 
Sparse matrix and its representation data structure
Sparse matrix and its representation data structureSparse matrix and its representation data structure
Sparse matrix and its representation data structureVardhil Patel
 
Presentation on array
Presentation on array Presentation on array
Presentation on array topu93
 
Abstract data types (adt) intro to data structure part 2
Abstract data types (adt)   intro to data structure part 2Abstract data types (adt)   intro to data structure part 2
Abstract data types (adt) intro to data structure part 2Self-Employed
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayimtiazalijoono
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm KristinaBorooah
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applicationssomendra kumar
 

What's hot (20)

Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
 
Ppt on Linked list,stack,queue
Ppt on Linked list,stack,queuePpt on Linked list,stack,queue
Ppt on Linked list,stack,queue
 
Data structure and algorithm using java
Data structure and algorithm using javaData structure and algorithm using java
Data structure and algorithm using java
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Strings in C
Strings in CStrings in C
Strings in C
 
Linked list
Linked listLinked list
Linked list
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
Sparse matrix and its representation data structure
Sparse matrix and its representation data structureSparse matrix and its representation data structure
Sparse matrix and its representation data structure
 
Presentation on array
Presentation on array Presentation on array
Presentation on array
 
Array ppt
Array pptArray ppt
Array ppt
 
Abstract data types (adt) intro to data structure part 2
Abstract data types (adt)   intro to data structure part 2Abstract data types (adt)   intro to data structure part 2
Abstract data types (adt) intro to data structure part 2
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 

Similar to Data structure

ds 1 Introduction to Data Structures.ppt
ds 1 Introduction to Data Structures.pptds 1 Introduction to Data Structures.ppt
ds 1 Introduction to Data Structures.pptAlliVinay1
 
Data structure and algorithm.
Data structure and algorithm. Data structure and algorithm.
Data structure and algorithm. Abdul salam
 
1.1 introduction to Data Structures.ppt
1.1 introduction to Data Structures.ppt1.1 introduction to Data Structures.ppt
1.1 introduction to Data Structures.pptAshok280385
 
CS4443 - Modern Programming Language - I Lecture (2)
CS4443 - Modern Programming Language - I  Lecture (2)CS4443 - Modern Programming Language - I  Lecture (2)
CS4443 - Modern Programming Language - I Lecture (2)Dilawar Khan
 
Data Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptxData Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptxGIRISHKUMARBC1
 
II B.Sc IT DATA STRUCTURES.pptx
II B.Sc IT DATA STRUCTURES.pptxII B.Sc IT DATA STRUCTURES.pptx
II B.Sc IT DATA STRUCTURES.pptxsabithabanu83
 
C programming basic concepts of mahi.pptx
C programming basic concepts of mahi.pptxC programming basic concepts of mahi.pptx
C programming basic concepts of mahi.pptxKorbanMaheshwari
 
Basic Concepts of C Language.pptx
Basic Concepts of C Language.pptxBasic Concepts of C Language.pptx
Basic Concepts of C Language.pptxKorbanMaheshwari
 
Unit 1 Introduction Part 3.pptx
Unit 1 Introduction Part 3.pptxUnit 1 Introduction Part 3.pptx
Unit 1 Introduction Part 3.pptxNishaRohit6
 
Introduction to DS.pptx
Introduction to DS.pptxIntroduction to DS.pptx
Introduction to DS.pptxOnkarModhave
 
ADS Introduction
ADS IntroductionADS Introduction
ADS IntroductionNagendraK18
 
Introduction to data_structure
Introduction to data_structureIntroduction to data_structure
Introduction to data_structureAshim Lamichhane
 
b,Sc it data structure.ppt
b,Sc it data structure.pptb,Sc it data structure.ppt
b,Sc it data structure.pptclassall
 
Introduction to R _IMPORTANT FOR DATA ANALYTICS
Introduction to R _IMPORTANT FOR DATA ANALYTICSIntroduction to R _IMPORTANT FOR DATA ANALYTICS
Introduction to R _IMPORTANT FOR DATA ANALYTICSHaritikaChhatwal1
 
Data Structures in C
Data Structures in CData Structures in C
Data Structures in CJabs6
 

Similar to Data structure (20)

ds 1 Introduction to Data Structures.ppt
ds 1 Introduction to Data Structures.pptds 1 Introduction to Data Structures.ppt
ds 1 Introduction to Data Structures.ppt
 
Data structure and algorithm.
Data structure and algorithm. Data structure and algorithm.
Data structure and algorithm.
 
Data structure Unit-I Part A
Data structure Unit-I Part AData structure Unit-I Part A
Data structure Unit-I Part A
 
1.1 introduction to Data Structures.ppt
1.1 introduction to Data Structures.ppt1.1 introduction to Data Structures.ppt
1.1 introduction to Data Structures.ppt
 
CS4443 - Modern Programming Language - I Lecture (2)
CS4443 - Modern Programming Language - I  Lecture (2)CS4443 - Modern Programming Language - I  Lecture (2)
CS4443 - Modern Programming Language - I Lecture (2)
 
Data Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptxData Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptx
 
UNIT 1.pptx
UNIT 1.pptxUNIT 1.pptx
UNIT 1.pptx
 
U nit i data structure-converted
U nit   i data structure-convertedU nit   i data structure-converted
U nit i data structure-converted
 
C Language Part 1
C Language Part 1C Language Part 1
C Language Part 1
 
Lecture 1 and 2
Lecture 1 and 2Lecture 1 and 2
Lecture 1 and 2
 
II B.Sc IT DATA STRUCTURES.pptx
II B.Sc IT DATA STRUCTURES.pptxII B.Sc IT DATA STRUCTURES.pptx
II B.Sc IT DATA STRUCTURES.pptx
 
C programming basic concepts of mahi.pptx
C programming basic concepts of mahi.pptxC programming basic concepts of mahi.pptx
C programming basic concepts of mahi.pptx
 
Basic Concepts of C Language.pptx
Basic Concepts of C Language.pptxBasic Concepts of C Language.pptx
Basic Concepts of C Language.pptx
 
Unit 1 Introduction Part 3.pptx
Unit 1 Introduction Part 3.pptxUnit 1 Introduction Part 3.pptx
Unit 1 Introduction Part 3.pptx
 
Introduction to DS.pptx
Introduction to DS.pptxIntroduction to DS.pptx
Introduction to DS.pptx
 
ADS Introduction
ADS IntroductionADS Introduction
ADS Introduction
 
Introduction to data_structure
Introduction to data_structureIntroduction to data_structure
Introduction to data_structure
 
b,Sc it data structure.ppt
b,Sc it data structure.pptb,Sc it data structure.ppt
b,Sc it data structure.ppt
 
Introduction to R _IMPORTANT FOR DATA ANALYTICS
Introduction to R _IMPORTANT FOR DATA ANALYTICSIntroduction to R _IMPORTANT FOR DATA ANALYTICS
Introduction to R _IMPORTANT FOR DATA ANALYTICS
 
Data Structures in C
Data Structures in CData Structures in C
Data Structures in C
 

Recently uploaded

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Principled Technologies
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024The Digital Insurer
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 

Recently uploaded (20)

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 

Data structure

  • 1. Data Structure and Algorithm Lecture No 1 Prepared by Muhammad Farhan
  • 2. Text Book • Text book Data Structures and Algorithms By Mark Allen Weise • Reference Book Data structure in C++ by Muhammad Tauqeer Aikman series
  • 3. Data vs. Information Data • A set of values have no meaning Information • Meaningful processed data
  • 4. Data vs. Information • Data: 51007 • Information: • 5/10/07 The date of your final exam.
  • 5. Data Structure • Data structure defines a particular way to organize a data in a computer so that it can be used efficiently. • Data structure are a containers which hold a data. • Efficient mean take minimum time and minimum memory space. • Data structures are used in almost every program
  • 6. Application of Data Structure • Data structures are widely applied in the following areas: • Compiler design • Operating system • DBMS • Artificial intelligence
  • 7. Classification of Data Structure • Data structure are normally divided into two broad categories: • Primitive Data Structure • Non-Primitive Data Structure
  • 8. Classification of Data Structure Data structure Primitive DS Non-Primitive DS Integer Float Character PointerFloatInteger Float
  • 9. Classification of Data Structure Non-Primitive DS Linear Non-Linear Array Link List Stack Queue Graph Trees
  • 10. Primitive Data Structure • Primitive data structures are the fundamental data types which are supported by a programming language. Some basic data types are integer, real, character, float. The terms ‘data type’, ‘basic data type’, and ‘primitive data type’ are often used interchangeably
  • 11. Non Primitive Data Structure • Non-primitive data structures are those data structures which are created using primitive data structures. Examples of such data structures include linked lists, stacks, trees, and graphs. Non-primitive data structures can further be classified into two categories: linear and non-linear data structures.
  • 12. Linear Data Structure • If the elements of a data structure are stored in a linear or sequential order, then it is a linear data structure. Examples include arrays, linked lists, stacks, and queues
  • 13. Non Linear Data Structure • However, if the elements of a data structure are not stored in a sequential order, then it is a non-linear data structure. Examples include trees and graphs.
  • 14. Operation on Data Structure • The data in data structure is processed using certain operations. Some commonly used operations performed on data structures are • Inserting: adding new data items into a data structure is called inserting • Deleting: Removing data items from a data structures is called deleting. • Searching : Find specific data items in a data structure is called searching • Traversing: Accessing each item in a data structure exactly once for processing is called traversing. • Sorting: Arranging data items in a data structure into a specific order is called sorting • Merging: Combining two list of data items into a single data list is called merging.
  • 15. • The operation through which data structure is created is called the creation operation. • For example, to create a variable of integer type in C++, the statement is written as : • Int a, b; • The above statement creates a memory space of two bytes for each variable a & b.
  • 16. Algorithm • The step by step procedure to solve a particular problem is called algorithm. • For example • A recipe for cooking a cake is an algorithm • Program=algorithm + Data Structure
  • 17. How to write Algorithm? • Name of algorithm : Every Algorithm is given a name that represents the purpose of the algorithm. For example: An algorithm to find the sum of two numbers is given the name “’sum’’. • Introductory comment: The algorithm name is followed by a brief description of the tasks that the algorithm performs. • Steps: The algorithm consists of a sequence of numbered steps. Each step described one task to be performed.
  • 18. • Comments: Comments are used to explain purpose of a step or statement. These may be given at the end of each statement or at the beginning of the step. In C++ the comments are given starting with double slash(//). In algorithms , these are written between square brackets [ ]. • Variable Name: A variable is an entity that possesses a value. The name of the variable is chosen according to the name of value it is to hold. For example, a variable that is used to hold the maximum value is named as Max. Usually , variable names in algorithm are written in capital letters. A variable name consists of letters, numeric digits and some special characters. It always begins with a letter. Blank spaces are not allowed within variable names.
  • 19. • Operators: Arithmetic (+,-,*,/), Relational(<,>,>=,<=, etc.) and logical (AND, OR, NOT) operators are used to describe various mathematical operations. • Assignment Statement: The assignment statement is used to evaluate an expression and assign the calculated value to the variable. The assignment operator ‘=’ sign is used for this purpose. For example evaluate the expression N+M assign its value to variable S. the assignment statement is written as S= N+M
  • 20. Input Statement • Input or read statement is used in algorithms to enter data into variable. The statement is written as: Input variable-Name For example , to input a value into variable N, the input statement is written as: Input N
  • 21. Output Statement • To Print a message or contents of a variable, PRINT statement is used with the following format. PRINT message or variable name The message is written within double quotes. To print the contents of variable , its is written without double quotes. In C++, usually the ‘cin’ object is used to get input from the keyboard and ‘cout’ object is used to print the output on the screen.
  • 22. Algorithm example Write an algorithm to get two numbers and to calculate their sum • STEP 1 : START • STEP 2 : INPUT FIRST NUMBER N • STEP 3 : INPUT SECOND NUMBER M • STEP 4 : ADD N AND M AND STORE RESULT IN S i.e S=N+M • STEP 5 : PRINT S • STEP 6 : STOP Algorithm-Sum
  • 23. Selection statement • Selection statement are used for making decisions. The decision is made by testing a given condition. After testing a condition, a statement or set of statements are executed or ignored. The structure that implements this logic in a programming language is called conditional structure. A conditional statement is represented by the IF structure. It has one of the following two forms: • 1- IF condition THEN statement (s) END IF
  • 24. Cont…...... • 2- IF condition THEN BLOCK A ELSE BLOCK B END IF
  • 25. Selection Statement Example Write an algorithm to find the greater of the two given numbers • STEP 1 : START • STEP 2 : INPUT TWO NUMBER A AND B • STEP 3 : IF A>B THEN • STEP 4: PRINT “A IS GREATER” • STEP 5 : ELSE • STEP 6 : PRINT “B IS GREATER” • STEP 7: EXIT Algorithm-Greater number
  • 26. Looping Statement • The looping statement are used to execute certain statement(s) repeatedly. The statement that are executed repeatedly are called body of the loop. In algorithm ‘Repeat’ statement is used to execute the statement repeatedly. The Repeat statement has two different forms • Repeat for loop • Repeat while loop
  • 27. Repeat For Loop • Repeat for loop is used to execute statements for a specified number of times. This loop is also called the counter loop. Its general format is : • Repeat FOR index= I-value TO F-value By S-value • Where • I-value represent the initial value for index variable • F-value represent the final value • S-value represent the step value i.e. Increment/ decrement value.
  • 28. Repeat For Example Write an algorithm to print first ten numbers using REPEAT FOR strucute • STEP 1: START • STEP 2: REPEAT STEP 3 FOR C=1 TO 10 BY 1 • STEP 3: PRINT C [END OF STEP 2 LOOP] • STEP 4: EXIT Algorithm-Natural Numbers
  • 29. Repeat while Structure • This loop structure is used to execute a statement or a set of statements repeatedly until the given condition remains true. It is also referred to as conditional loop. Its general syntax is • REPEAT WHILE ( condition) Body of the loop [end of loop]
  • 30. Repeat While Example Write an algorithm to print first ten numbers using REPEAT WHILE strucute • STEP 1: START • STEP 2: C=1 • STEP 3: REPEAT STEP 4 TO 5 WHILE (C<=10) • STEP 4: PRINT C • STEP 5 C=C+1 [END OF STEP-3 LOOP] • STEP 6: EXIT Algorithm-Natural Numbers
  • 31. Simple/Structure data types • So far, we have seen only simple data types, such as int, float, and char. • Simple variables can hold only one value at any time during program execution, although that value may change. • A Structure data type is a data type that can hold multiple values at the same time. (Synonyms: complex data type, composite data type) • The array is one kind of structure data types.
  • 32. Arrays • An array is a group of related elements that all have the same name and the same data type. • Arrays are static in that they remain the same size throughout program execution. • An array’s data items are stored contiguously in memory. • Each of the data items is known as an element of the array. Each element can be accessed individually.
  • 33. Array Declaration and Initialization int numbers[ 5 ] ; • The name of this array is “numbers”. • Initializing an array may be done with an array initialization, as in : int numbers[ 5 ] = { 5, 2, 6, 9, 3 } ; 5 2 6 9 3numbers
  • 34. Accessing Array Elements • Each element in an array has a subscript (index) associated with it. • Subscripts are integers and always begin at zero. • Values of individual elements can be accessed by indexing into the array. For example, numbers[ 2 ] would give the output The third element = 6. 5 2 6 9 3numbers 0 1 2 3 4
  • 35. Types of the array • One-dimensional Array • Two dimensional Array
  • 36. One Dimensional Array • When the consecutive homogeneous data is stored in the form of single row or column then it is called one dimensional array.
  • 37. Two dimensional array • When data is stored in both rows and Coloums then its is called two dimensional array. It is also called matrices.
  • 38. How to declare a two dimensional array? A two dimensional array: int arr[2][3]; This array has total 2*3 = 6 elements Initialization: We can initialize the array int arr[2][3] = {10, 11 ,12 ,20 ,21 , 22};
  • 39. Operation on Array Data Structures • Traversal • Insertion • Deletion
  • 40. Traversal Traversal: To print all the element one by one in array.  
  • 41. Traversal Algorithm Algorithm-Traverse (LB, UB, A) Introductory comment Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation process to each element of A. Step no 1. Start Step no 2. Repeat steps 3 For I=LB to UB by 1 Step no 3. Print A[I] [End of step 2 loop] Step no 4. Exit
  • 42. Insertion Operation • Insertion operation is used to insert a new element at specific position in to one dimensional array.  In order to insert a new element into one dimensional array we have to create space for new element.  Suppose there are N elements in an array and we want to insert a new element between first and second element. We have to move last N-1 elements down in order to create space for the new element.
  • 43. Insertion of element at the end of the array • Algorithm-Insert(A,N, Item) • Introductory comment: • This algorithm is used to insert an element at the end of the array where • A=array name N=total element item= the element which is to be inserted • Step no1. Set A[N+1]=item • Step no2. Set N=N+1 • Step no3. stop
  • 44. Insertion algorithm at middle Algorithm-Insertion(A, N,K, ITEM) Introductory Comment Let LA be a Linear Array (unordered) with N elements and K is a positive integer such that K<=N. Following is the algorithm where ITEM is inserted into the Kth position of LA.
  • 45. Algorithm Step no1. Start Step no2. [Initialize counter] Set J=K Step no 3. Repeat steps 4 and 5 while N >=J Step no4. [Move the jth element downward]Set LA[N+ 1]= LA[N] Step no5. [decrease counter]Set N= N-1 [end of step 3 loop] Step no 6. [Insert Item] set LA[K]=ITEM Step no 7. Stop
  • 46. Deletion operation in array • Deletion is the operation that removes an element from a given location of the array • Delete an element at the end of array • Delete an element from specific location
  • 47. Delete a specific element from the array • Algorithm-Delete • Introductory comment • Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the algorithm to delete an element available at the Kth position of LA.
  • 48. • Step no1. set J=k • Step no2. Repeat Step no 3 while J<=N-1 • Step no3. Set A[j]=A[j+1] [end of loop] • Step no4 Set J=J+1 • Step no4. Set N=N-1[reset the number of element in array] • Step no5. Exit