SlideShare a Scribd company logo
3
Most read
18
Most read
19
Most read
Course Title: Data Structures and
Algorithms
Course code: CS211
Credit Hours: 4(3+1)
Prerequisite: Introduction to Computer
Programming
Course objectives:
Detailed study of the basic notions of the design of
algorithms and the underlying data structures.
Several measures of complexity are introduced.
Emphasis on the Algorithms development, Testing
complexity, and efficiency of algorithms.
Learning outcomes:
Fundamental algorithms design principles such as
Divide and Conquer, Different sorting and searching
algorithms are introduced and explored through case
studies to demonstrate the design of efficient
solutions to computational problems. On completion
of the module a student should be able to:
 Identify the fundamental strategies in algorithmic
design
 Distinguish which strategy is appropriate to solve
a given problem
 Classify different algorithmic strategies
 Analyze a given algorithm and assess its
efficiency
 Apply techniques of proof by induction to verify
certain properties of algorithms.
 Coursework will enhance a student's algorithm
design and program implementation skills.
Course Outlines :
Introduction to Data Structures and Algorithms;
Complexity Analysis; Arrays; Sorting Algorithms:
Insertion Sort, Selection Sort, Bubble Sort, Shell
Sort, Heap Sort, Quick Sort, Merge Sort, Radix Sort,
Bucket Sort; Linked Lists: Singly Linked Lists,
Doubly Linked Lists, Circular List; Stacks, Queues,
and Priority Queue; Recursion: Function call and
Recursion Implementation, Tail Recursion, Non-tail
Recursion, Indirect Recursion, Nested Recursion,
Backtracking. Trees: Binary Trees, Binary Heap,
Binary Search. Tree Traversal, Insertion, Deletion,
and Balancing a Tree; Heap; B-Tree; Spanning Tree,
Splay Trees; Graphs: Representation, Traversal,
Shortest Path, and Cycle Detection; Isomorphic
Graphs; Graph Traversal Algorithms; Hashing;
Memory Management and Garbage Collection.
LAB Work:
1 Write a program to search an element in a two
dimensional array
2 Using iteration and recursion concepts write
programs for finding the element in the array
using the Binary search method.
3 Write a program to perform following
operations on tables by user defined
functions: Addition, Subtraction,
Multiplication, and Transpose.
4 Write a program using iteration and recursion
concepts for quick sort.
5 Write a program to implement various
operations on strings.
6 Write a program for swapping two numbers
using call by value and call by reference
strategies.
7 Write a program to implement Binary search
tree.
8 Write a program to create a Linked List and
perform operations such as insert, delete,
update and reverse.
9 Write a program to simulate various sorting
and searching algorithms.
10 Write a program to simulate various Graph
traversing techniques.
11 Write a program to simulate various tree
traversal techniques.
12 Implement data structures in C++
Reference Materials (Latest Editions of the
following books):
1. Data Structures and Algorithm Analysis, Mark
Allen Weiss, Florida International University,
Addison-Wesley
2. Algorithms, Robert Sedgewick, Princeton
University Publisher: Addison- Wesley Professional
3. Data Structures: Abstraction and Design Using
Java, Koffman and Wolfgang, Wiley;
4. Data Structures and Algorithms in C++, Adam
Drozdek, Course Technology;
Grading Policy:
1.Assignment /Presentation/ Quizzes /Group
Discussions 15%
2.Attendance (Minimum class attendance 75% of
the total lectures ) 05%
3.Mid Term Examination
30%
4.Final Examination
50%
Data Structure:
Data :
• The collection of raw material or facts and
figures about any object is called data.
• e.g
• Student data
• Name, Age, Rno, Class, Section, Gender ,
Address, Phone no, Blood group,
Data Structure
• Organization of data into computer memory is
called data structure.
• Data structure deals with “How data is organized
in memory”.
• Data may be organized in many different ways:
• The logical or mathematical model of particular
organization of data is called a data structure.
• The choice of a particular data model depends
on two considerations.
• First it must be rich enough in structure to mirror
the actual relationships of the data in real world.
• The structure should be simple enough that one
.can effectively process the data when necessary.
Classification of Data Structure
• Linear
• Non linear
Linear
• The organization of data in which each node
except first and last have only one proceeding
and succeeding node is called linear data
structure
•
• Arrays
• Stacks
• Queues
• Lists, etc
Non Linear
• When the data is stored in the computer memory
in hierarchical way or in non linear form, then it
is called non linear data structure
• e.g
• Trees
• Files
• Graphs
Algorithms:
• An algorithm is any well-defined computational
procedure that takes some values, or set of
values, as input and produces some value, or set
of values, as output. An algorithm is thus a
sequence of computational steps that transform
the input into output.
• It can be described in a natural language,
pseudocode, a flow- chart, or even a
programming language.
Criteria/Characteristics of Algorithms
• Finiteness
• Definiteness - no ambiguity /unambiguous
• Input
• Output
• Effectiveness
• Efficient
• Concise and Compact
• Sequence No.
• Correctness
Finiteness
• An algorithm must terminate after a finite
number of steps and further each step must be
executable in finite amount of time.
• In order to establish a sequence of steps as an
algorithm, it should be established that it
terminates (in finite number of steps) on all
allowed inputs.
Definiteness (no ambiguity , clarity )
• Each steps of an algorithm must be precisely
defined and it should be unambiguous.
Inputs
• An algorithm must have input values from a
specified set.
• An algorithm has zero or more but only finite
number of inputs.
Output:
• An algorithm must produce some values called
output.
• An algorithm has one or more outputs.
• The requirement of at least one output is
obviously essential, because, otherwise we
cannot know the answer/ solution provided by
the algorithm.
• The outputs have specific relation to the inputs,
where the relation is defined by the algorithm.
Effectiveness
• Effectiveness is the capability of producing a
desired result.
• An algorithm should be effective/useful.
Efficient
• An algorithm should not use unnecessary
memory locations.
Concise and compact
• An algorithm should be concise and compact.
Sequence No.
• Each step of algorithm must have a sequence
number.
Correctness
• Each step of an algorithm must be correctly
defined.
Need for Data Structures
 Data structures organize data  more efficient
programs.
 More powerful computers  more complex
applications.
 More complex applications demand more
calculations.
Efficiency
 A solution is said to be efficient if it solves the
problem within its resource constraints.
– Space
– Time
– The cost of a solution is the amount of
resources that the solution consumes.
Selecting a Data Structure
Select a data structure as follows:
1.Analyze the problem to determine the resource
constraints a solution must meet.
2.Determine the basic operations that must be
supported. Quantify the resource constraints for
each operation.
3.Select the data structure that best meets these
requirements
Some Questions to Ask
• Are all data inserted into the data structure at the
beginning, or are insertions interspersed with
other operations?
• Can data be deleted?
• Are all data processed in some well-defined
order, or is random access allowed?
Data Structure Philosophy
 Each data structure has costs and benefits.
 Rarely is one data structure better than another
in all situations.
 A data structure requires:
– space for each data item it stores,
– time to perform each basic operation,
– programming effort.
Arrays:
 Elementary data structure that exists as built-in
in most programming languages.
An array is a collection of items stored at contiguous
memory locations. The idea is to store multiple items
of the same type together. This makes it easier to
calculate the position of each element by simply
adding an offset to a base value, i.e., the memory
location of the first element of the array (generally
denoted by the name of the array).
main( int argc, char** argv )
{
int x[6];
int j;
for(j=0; j < 6; j++)
x[j] = 2*j;
}
 Array declaration: int x[8];
 An array is collection of cells of the same type.
 The collection has the name ‘x’.
 The cells are numbered with consecutive
integers.
 To access a cell, use the array name and an
index:
x[0], x[1], x[2], x[3], x[4], x[5]
Array Layout
Array cells are contiguous in computer memory.
The memory can be thought of as an array.
X[0]
X[1]
X[2]
X[3]
X[4]
X[5]
X[6]
X[7]
What is Array Name?
 ‘x’ is an array name but there is no variable
x. ‘x’ is not an lvalue.
 For example, if we have the code
int a, b;
then we can write
b = 2;
a = b;
a = 5;
 But we cannot write
2 = a;
 ‘x’ is not an lvalue
int x[6];
int n;
x[0] = 5;
x[1] = 2;
x = 3; // not allowed
x = a + b; // not allowed
x = &n; // not allowed
Dynamic Arrays
 You would like to use an array data
structure but you do not know the size
of the array at compile time.
 You find out when the program
executes that you need an integer array
of size n=20.
 Allocate an array using the new
operator:
int* y = new int[20]; // or int* y = new
int[n]
y[0] = 10;
y[1] = 15; // use is the same
 ‘y’ is a lvalue; it is a pointer that holds
the address of 20 consecutive cells in
memory.
 It can be assigned a value. The new
operator returns as address that is
stored in y.
 We can write:
y = &x[0];
y = x; // x can appear on the
right
// y gets the address of
the
// first cell of the x array
 We must free the memory we got
using the new operator once we are
done with the y array.
delete[ ] y;

 We would not do this to the x array
because we did not use new to create
it.
There is a substantial difference
between declaring a normal array
and allocating dynamic memory
for a block of memory using new.
The most important difference is
that the size of a regular array
needs to be a constant
expression, and thus its size has
to be determined at the moment
of designing the program, before
it is run, whereas the dynamic
memory allocation performed
by new allows to assign memory
during runtime using any variable
value as size.
The LIST Data Structure
 The List is among the most generic of data
structures.
 Real life:
a.shopping list,
b.groceries list,
c.list of people to invite to dinner
d.List of presents to get
 A list is collection of items that are all of the
same type (grocery items, integers, names)
 The items, or elements of the list, are stored in
some particular order
 It is possible to insert new elements into various
positions in the list and remove any element of
the list
 List is a set of elements in a linear order.
For example, data values a1, a2, a3, a4 can be
arranged in a list:
(a3, a1, a2, a4)
In this list, a3, is the first element, a1 is the
second element, and so on
 The order is important here; this is not just a
random collection of elements, it is an ordered
collection
List Operations
Useful operations
• createList(): create a new list (presumably
empty)
• copy(): set one list to be a copy of another
• clear(); clear a list (remove all elments)
• insert(X, ?): Insert element X at a particular
position in the list
• remove(?): Remove element at some position in
the list
• get(?): Get element at a given position
• update(X, ?): replace the element at a given
position with X
• find(X): determine if the element X is in the list
• length(): return the length of the list.
List Operations
 We need to decide what is meant by “particular
position”; we have used “?” for this.
There are two possibilities:
1.Use the actual index of element: insert after
element 3, get element number 6. This
approach is taken by arrays
2.Use a “current” marker or pointer to refer to
a particular position in the list.
List Operations
 If we use the “current” marker, the following
four methods would be useful:
 start(): moves to “current” pointer to the
very first element.
 tail(): moves to “current” pointer to the very
last element.
 next(): move the current position forward
one element
 back(): move the current position backward
one element

More Related Content

Similar to Data structure and algorithm. (20)

PPT
Intro_2.ppt
MumitAhmed1
 
PPT
Intro.ppt
SharabiNaif
 
PPT
Data Structure Lec #1
University of Gujrat, Pakistan
 
PPT
algo 1.ppt
example43
 
PPT
CS301-lec01.ppt
omair31
 
PDF
Unit-I PPT hususi sisooshsgv. Eijeieieooekejj
sanketkurve7
 
PPTX
Introduction to Data Structures & Algorithms
Afaq Mansoor Khan
 
PPTX
data structures and its importance
Anaya Zafar
 
PPT
Data structure
Muhammad Farhan
 
PPTX
RAJAT PROJECT.pptx
SayedMohdAsim2
 
PPT
Data structure and algorithm with java by shikra
jateno3396
 
PPTX
UNIT 1.pptx
JohnStuart83
 
PPTX
UNIT 1 Memory ManagementMemory Management.pptx
harsh1212000552
 
PDF
Chapter 1 Introduction to Data Structures and Algorithms.pdf
Axmedcarb
 
PPTX
Intro to Data Structure & Algorithms
Akhil Kaushik
 
PDF
DSA 1- Introduction.pdf
AliyanAbbas1
 
PPTX
UNIT_5_Data Wrangling.pptx
BhagyasriPatel2
 
PPT
assignment character education assignment
tsegayeblen57
 
PPT
computer notes - Data Structures - 1
ecomputernotes
 
Intro_2.ppt
MumitAhmed1
 
Intro.ppt
SharabiNaif
 
Data Structure Lec #1
University of Gujrat, Pakistan
 
algo 1.ppt
example43
 
CS301-lec01.ppt
omair31
 
Unit-I PPT hususi sisooshsgv. Eijeieieooekejj
sanketkurve7
 
Introduction to Data Structures & Algorithms
Afaq Mansoor Khan
 
data structures and its importance
Anaya Zafar
 
Data structure
Muhammad Farhan
 
RAJAT PROJECT.pptx
SayedMohdAsim2
 
Data structure and algorithm with java by shikra
jateno3396
 
UNIT 1.pptx
JohnStuart83
 
UNIT 1 Memory ManagementMemory Management.pptx
harsh1212000552
 
Chapter 1 Introduction to Data Structures and Algorithms.pdf
Axmedcarb
 
Intro to Data Structure & Algorithms
Akhil Kaushik
 
DSA 1- Introduction.pdf
AliyanAbbas1
 
UNIT_5_Data Wrangling.pptx
BhagyasriPatel2
 
assignment character education assignment
tsegayeblen57
 
computer notes - Data Structures - 1
ecomputernotes
 

More from Abdul salam (16)

PDF
Physics final term notes written by Naeem Khan .
Abdul salam
 
PDF
Mid term handwritten physics notes by Naeem Khan
Abdul salam
 
PPTX
STACK AND IT,S OPERATION.pptx Lect 3 OF CS5.pptx
Abdul salam
 
PPTX
Actions inside processing chip, assembly language.pptx
Abdul salam
 
PPTX
ASSEMBLER AND DEBUGGER.pptx lect 4 cs5.pptx
Abdul salam
 
PDF
MICROPROCESSOR Handwritten Notes BY SULIMAN ASGHAR.pdf
Abdul salam
 
PDF
Design and analysis of algorithm final course
Abdul salam
 
PDF
Handwritten notes of all protocol by Naeem Khan
Abdul salam
 
PDF
Design & analysis of algorithm notes.pdf
Abdul salam
 
PPTX
FLAGS AND REGISTERS.pptx CS5 IMP.pptx ..
Abdul salam
 
PPTX
MICROPROCESSOR.pptx Microprocessor lec 2
Abdul salam
 
PPTX
ideology of Pakistan.pptx. ideology of Pakistan
Abdul salam
 
PDF
full course of software engineering mid term.pdf
Abdul salam
 
PDF
ASSIGNMENT (1) political science gazi university Multan, Pakistan.
Abdul salam
 
PDF
precis writing.
Abdul salam
 
PDF
Speaking with confidence-converted.pdf
Abdul salam
 
Physics final term notes written by Naeem Khan .
Abdul salam
 
Mid term handwritten physics notes by Naeem Khan
Abdul salam
 
STACK AND IT,S OPERATION.pptx Lect 3 OF CS5.pptx
Abdul salam
 
Actions inside processing chip, assembly language.pptx
Abdul salam
 
ASSEMBLER AND DEBUGGER.pptx lect 4 cs5.pptx
Abdul salam
 
MICROPROCESSOR Handwritten Notes BY SULIMAN ASGHAR.pdf
Abdul salam
 
Design and analysis of algorithm final course
Abdul salam
 
Handwritten notes of all protocol by Naeem Khan
Abdul salam
 
Design & analysis of algorithm notes.pdf
Abdul salam
 
FLAGS AND REGISTERS.pptx CS5 IMP.pptx ..
Abdul salam
 
MICROPROCESSOR.pptx Microprocessor lec 2
Abdul salam
 
ideology of Pakistan.pptx. ideology of Pakistan
Abdul salam
 
full course of software engineering mid term.pdf
Abdul salam
 
ASSIGNMENT (1) political science gazi university Multan, Pakistan.
Abdul salam
 
precis writing.
Abdul salam
 
Speaking with confidence-converted.pdf
Abdul salam
 
Ad

Recently uploaded (20)

PPTX
SlideEgg_501298-Agentic AI.pptx agentic ai
530BYManoj
 
PPTX
apidays Singapore 2025 - The Quest for the Greenest LLM , Jean Philippe Ehre...
apidays
 
PDF
NIS2 Compliance for MSPs: Roadmap, Benefits & Cybersecurity Trends (2025 Guide)
GRC Kompas
 
PPTX
apidays Helsinki & North 2025 - Agentic AI: A Friend or Foe?, Merja Kajava (A...
apidays
 
PDF
apidays Helsinki & North 2025 - Monetizing AI APIs: The New API Economy, Alla...
apidays
 
PPTX
BinarySearchTree in datastructures in detail
kichokuttu
 
PDF
A GraphRAG approach for Energy Efficiency Q&A
Marco Brambilla
 
PPTX
apidays Helsinki & North 2025 - Vero APIs - Experiences of API development in...
apidays
 
PDF
Using AI/ML for Space Biology Research
VICTOR MAESTRE RAMIREZ
 
PPTX
apidays Helsinki & North 2025 - Running a Successful API Program: Best Practi...
apidays
 
PDF
Research Methodology Overview Introduction
ayeshagul29594
 
PPTX
Listify-Intelligent-Voice-to-Catalog-Agent.pptx
nareshkottees
 
PPTX
ER_Model_with_Diagrams_Presentation.pptx
dharaadhvaryu1992
 
PDF
OPPOTUS - Malaysias on Malaysia 1Q2025.pdf
Oppotus
 
PDF
apidays Singapore 2025 - Trustworthy Generative AI: The Role of Observability...
apidays
 
PDF
apidays Singapore 2025 - Surviving an interconnected world with API governanc...
apidays
 
PPTX
apidays Singapore 2025 - Designing for Change, Julie Schiller (Google)
apidays
 
PDF
apidays Helsinki & North 2025 - How (not) to run a Graphql Stewardship Group,...
apidays
 
PDF
Simplifying Document Processing with Docling for AI Applications.pdf
Tamanna36
 
PDF
apidays Helsinki & North 2025 - API-Powered Journeys: Mobility in an API-Driv...
apidays
 
SlideEgg_501298-Agentic AI.pptx agentic ai
530BYManoj
 
apidays Singapore 2025 - The Quest for the Greenest LLM , Jean Philippe Ehre...
apidays
 
NIS2 Compliance for MSPs: Roadmap, Benefits & Cybersecurity Trends (2025 Guide)
GRC Kompas
 
apidays Helsinki & North 2025 - Agentic AI: A Friend or Foe?, Merja Kajava (A...
apidays
 
apidays Helsinki & North 2025 - Monetizing AI APIs: The New API Economy, Alla...
apidays
 
BinarySearchTree in datastructures in detail
kichokuttu
 
A GraphRAG approach for Energy Efficiency Q&A
Marco Brambilla
 
apidays Helsinki & North 2025 - Vero APIs - Experiences of API development in...
apidays
 
Using AI/ML for Space Biology Research
VICTOR MAESTRE RAMIREZ
 
apidays Helsinki & North 2025 - Running a Successful API Program: Best Practi...
apidays
 
Research Methodology Overview Introduction
ayeshagul29594
 
Listify-Intelligent-Voice-to-Catalog-Agent.pptx
nareshkottees
 
ER_Model_with_Diagrams_Presentation.pptx
dharaadhvaryu1992
 
OPPOTUS - Malaysias on Malaysia 1Q2025.pdf
Oppotus
 
apidays Singapore 2025 - Trustworthy Generative AI: The Role of Observability...
apidays
 
apidays Singapore 2025 - Surviving an interconnected world with API governanc...
apidays
 
apidays Singapore 2025 - Designing for Change, Julie Schiller (Google)
apidays
 
apidays Helsinki & North 2025 - How (not) to run a Graphql Stewardship Group,...
apidays
 
Simplifying Document Processing with Docling for AI Applications.pdf
Tamanna36
 
apidays Helsinki & North 2025 - API-Powered Journeys: Mobility in an API-Driv...
apidays
 
Ad

Data structure and algorithm.

  • 1. Course Title: Data Structures and Algorithms Course code: CS211 Credit Hours: 4(3+1) Prerequisite: Introduction to Computer Programming Course objectives: Detailed study of the basic notions of the design of algorithms and the underlying data structures. Several measures of complexity are introduced. Emphasis on the Algorithms development, Testing complexity, and efficiency of algorithms. Learning outcomes: Fundamental algorithms design principles such as Divide and Conquer, Different sorting and searching algorithms are introduced and explored through case studies to demonstrate the design of efficient solutions to computational problems. On completion of the module a student should be able to:
  • 2.  Identify the fundamental strategies in algorithmic design  Distinguish which strategy is appropriate to solve a given problem  Classify different algorithmic strategies  Analyze a given algorithm and assess its efficiency  Apply techniques of proof by induction to verify certain properties of algorithms.  Coursework will enhance a student's algorithm design and program implementation skills. Course Outlines : Introduction to Data Structures and Algorithms; Complexity Analysis; Arrays; Sorting Algorithms: Insertion Sort, Selection Sort, Bubble Sort, Shell Sort, Heap Sort, Quick Sort, Merge Sort, Radix Sort, Bucket Sort; Linked Lists: Singly Linked Lists, Doubly Linked Lists, Circular List; Stacks, Queues, and Priority Queue; Recursion: Function call and Recursion Implementation, Tail Recursion, Non-tail Recursion, Indirect Recursion, Nested Recursion,
  • 3. Backtracking. Trees: Binary Trees, Binary Heap, Binary Search. Tree Traversal, Insertion, Deletion, and Balancing a Tree; Heap; B-Tree; Spanning Tree, Splay Trees; Graphs: Representation, Traversal, Shortest Path, and Cycle Detection; Isomorphic Graphs; Graph Traversal Algorithms; Hashing; Memory Management and Garbage Collection. LAB Work: 1 Write a program to search an element in a two dimensional array 2 Using iteration and recursion concepts write programs for finding the element in the array using the Binary search method. 3 Write a program to perform following operations on tables by user defined functions: Addition, Subtraction, Multiplication, and Transpose. 4 Write a program using iteration and recursion concepts for quick sort. 5 Write a program to implement various operations on strings. 6 Write a program for swapping two numbers using call by value and call by reference strategies. 7 Write a program to implement Binary search tree.
  • 4. 8 Write a program to create a Linked List and perform operations such as insert, delete, update and reverse. 9 Write a program to simulate various sorting and searching algorithms. 10 Write a program to simulate various Graph traversing techniques. 11 Write a program to simulate various tree traversal techniques. 12 Implement data structures in C++ Reference Materials (Latest Editions of the following books): 1. Data Structures and Algorithm Analysis, Mark Allen Weiss, Florida International University, Addison-Wesley 2. Algorithms, Robert Sedgewick, Princeton University Publisher: Addison- Wesley Professional 3. Data Structures: Abstraction and Design Using Java, Koffman and Wolfgang, Wiley; 4. Data Structures and Algorithms in C++, Adam Drozdek, Course Technology;
  • 5. Grading Policy: 1.Assignment /Presentation/ Quizzes /Group Discussions 15% 2.Attendance (Minimum class attendance 75% of the total lectures ) 05% 3.Mid Term Examination 30% 4.Final Examination 50% Data Structure: Data : • The collection of raw material or facts and figures about any object is called data. • e.g
  • 6. • Student data • Name, Age, Rno, Class, Section, Gender , Address, Phone no, Blood group, Data Structure • Organization of data into computer memory is called data structure. • Data structure deals with “How data is organized in memory”. • Data may be organized in many different ways: • The logical or mathematical model of particular organization of data is called a data structure. • The choice of a particular data model depends on two considerations. • First it must be rich enough in structure to mirror the actual relationships of the data in real world. • The structure should be simple enough that one .can effectively process the data when necessary. Classification of Data Structure
  • 7. • Linear • Non linear Linear • The organization of data in which each node except first and last have only one proceeding and succeeding node is called linear data structure • • Arrays • Stacks • Queues • Lists, etc Non Linear • When the data is stored in the computer memory in hierarchical way or in non linear form, then it is called non linear data structure • e.g • Trees
  • 8. • Files • Graphs Algorithms: • An algorithm is any well-defined computational procedure that takes some values, or set of values, as input and produces some value, or set of values, as output. An algorithm is thus a sequence of computational steps that transform the input into output. • It can be described in a natural language, pseudocode, a flow- chart, or even a programming language.
  • 9. Criteria/Characteristics of Algorithms • Finiteness • Definiteness - no ambiguity /unambiguous • Input • Output • Effectiveness • Efficient • Concise and Compact • Sequence No. • Correctness Finiteness • An algorithm must terminate after a finite number of steps and further each step must be executable in finite amount of time. • In order to establish a sequence of steps as an algorithm, it should be established that it terminates (in finite number of steps) on all allowed inputs.
  • 10. Definiteness (no ambiguity , clarity ) • Each steps of an algorithm must be precisely defined and it should be unambiguous. Inputs • An algorithm must have input values from a specified set. • An algorithm has zero or more but only finite number of inputs. Output: • An algorithm must produce some values called output. • An algorithm has one or more outputs. • The requirement of at least one output is obviously essential, because, otherwise we cannot know the answer/ solution provided by the algorithm. • The outputs have specific relation to the inputs, where the relation is defined by the algorithm.
  • 11. Effectiveness • Effectiveness is the capability of producing a desired result. • An algorithm should be effective/useful. Efficient • An algorithm should not use unnecessary memory locations. Concise and compact • An algorithm should be concise and compact. Sequence No. • Each step of algorithm must have a sequence number. Correctness • Each step of an algorithm must be correctly defined.
  • 12. Need for Data Structures  Data structures organize data  more efficient programs.  More powerful computers  more complex applications.  More complex applications demand more calculations. Efficiency  A solution is said to be efficient if it solves the problem within its resource constraints. – Space – Time – The cost of a solution is the amount of resources that the solution consumes.
  • 13. Selecting a Data Structure Select a data structure as follows: 1.Analyze the problem to determine the resource constraints a solution must meet. 2.Determine the basic operations that must be supported. Quantify the resource constraints for each operation. 3.Select the data structure that best meets these requirements Some Questions to Ask • Are all data inserted into the data structure at the beginning, or are insertions interspersed with other operations? • Can data be deleted? • Are all data processed in some well-defined order, or is random access allowed?
  • 14. Data Structure Philosophy  Each data structure has costs and benefits.  Rarely is one data structure better than another in all situations.  A data structure requires: – space for each data item it stores, – time to perform each basic operation, – programming effort.
  • 15. Arrays:  Elementary data structure that exists as built-in in most programming languages. An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array). main( int argc, char** argv ) { int x[6]; int j; for(j=0; j < 6; j++) x[j] = 2*j; }
  • 16.  Array declaration: int x[8];  An array is collection of cells of the same type.  The collection has the name ‘x’.  The cells are numbered with consecutive integers.  To access a cell, use the array name and an index: x[0], x[1], x[2], x[3], x[4], x[5] Array Layout Array cells are contiguous in computer memory. The memory can be thought of as an array. X[0] X[1] X[2] X[3]
  • 17. X[4] X[5] X[6] X[7] What is Array Name?  ‘x’ is an array name but there is no variable x. ‘x’ is not an lvalue.  For example, if we have the code int a, b; then we can write b = 2; a = b; a = 5;
  • 18.  But we cannot write 2 = a;  ‘x’ is not an lvalue int x[6]; int n; x[0] = 5; x[1] = 2; x = 3; // not allowed x = a + b; // not allowed x = &n; // not allowed
  • 19. Dynamic Arrays  You would like to use an array data structure but you do not know the size of the array at compile time.  You find out when the program executes that you need an integer array of size n=20.  Allocate an array using the new operator: int* y = new int[20]; // or int* y = new int[n] y[0] = 10; y[1] = 15; // use is the same
  • 20.  ‘y’ is a lvalue; it is a pointer that holds the address of 20 consecutive cells in memory.  It can be assigned a value. The new operator returns as address that is stored in y.  We can write: y = &x[0]; y = x; // x can appear on the right // y gets the address of the // first cell of the x array  We must free the memory we got using the new operator once we are done with the y array. delete[ ] y; 
  • 21.  We would not do this to the x array because we did not use new to create it. There is a substantial difference between declaring a normal array and allocating dynamic memory for a block of memory using new. The most important difference is that the size of a regular array needs to be a constant expression, and thus its size has to be determined at the moment of designing the program, before it is run, whereas the dynamic memory allocation performed by new allows to assign memory during runtime using any variable value as size.
  • 22. The LIST Data Structure  The List is among the most generic of data structures.  Real life: a.shopping list, b.groceries list, c.list of people to invite to dinner d.List of presents to get  A list is collection of items that are all of the same type (grocery items, integers, names)  The items, or elements of the list, are stored in some particular order  It is possible to insert new elements into various positions in the list and remove any element of the list  List is a set of elements in a linear order. For example, data values a1, a2, a3, a4 can be arranged in a list: (a3, a1, a2, a4)
  • 23. In this list, a3, is the first element, a1 is the second element, and so on  The order is important here; this is not just a random collection of elements, it is an ordered collection List Operations Useful operations • createList(): create a new list (presumably empty) • copy(): set one list to be a copy of another • clear(); clear a list (remove all elments) • insert(X, ?): Insert element X at a particular position in the list • remove(?): Remove element at some position in the list • get(?): Get element at a given position • update(X, ?): replace the element at a given position with X • find(X): determine if the element X is in the list • length(): return the length of the list. List Operations
  • 24.  We need to decide what is meant by “particular position”; we have used “?” for this. There are two possibilities: 1.Use the actual index of element: insert after element 3, get element number 6. This approach is taken by arrays 2.Use a “current” marker or pointer to refer to a particular position in the list. List Operations  If we use the “current” marker, the following four methods would be useful:  start(): moves to “current” pointer to the very first element.  tail(): moves to “current” pointer to the very last element.  next(): move the current position forward one element  back(): move the current position backward one element