100% found this document useful (1 vote)
640 views31 pages

Data Structure and Algorithm: Lecture No 1 Prepared by Muhammad Farhan

This document provides an overview of data structures and algorithms. It defines key terms like data, data structure, primitive and non-primitive data structures. It also discusses different types of data structures like linear (arrays, linked lists, stacks, queues) and non-linear (trees, graphs). Additionally, it covers algorithm development, including writing steps, comments, variables, and different control structures like selection, looping and their examples.

Uploaded by

Muhammad Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
640 views31 pages

Data Structure and Algorithm: Lecture No 1 Prepared by Muhammad Farhan

This document provides an overview of data structures and algorithms. It defines key terms like data, data structure, primitive and non-primitive data structures. It also discusses different types of data structures like linear (arrays, linked lists, stacks, queues) and non-linear (trees, graphs). Additionally, it covers algorithm development, including writing steps, comments, variables, and different control structures like selection, looping and their examples.

Uploaded by

Muhammad Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

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 Information
• A set of values have no • Meaningful processed data
meaning
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 Pointer


Classification of Data Structure
Non-Primitive DS

Linear Non-Linear

Array Queue Graph Trees

Link List Stack


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
Algorithm-Sum
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
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
Algorithm-Greater number
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
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
Algorithm-Natural Numbers

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
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 For Example
Algorithm-Natural Numbers

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

You might also like