Upload
lee-robinson
View
215
Download
3
Embed Size (px)
Citation preview
1
Data Structures
Data Structures
Reference
http://www.algolist.net
2
Algorithm :- Outline the essence of a computational procedure , step by step instructions .
Program :- an implementation of an algorithm in some programming language
Data Structure :- Organization of data needed to solve the problem .
3
What is an Algorithm?
Algorithm: Finite set of instructions that, if
followed, accomplishes a particular task.
Describe: Representation of algorithm can
written By :-
in natural language ( English ) / pseudo-
code / diagrams / etc.
4
Any computing algorithm will have AT MOST
five kinds of components:
• Data structures to hold data
• Instructions change data values
• Conditional expressions to make
decisions
• Control structures to act on decisions
• Modules to make the algorithm
manageable by abstraction, i.e., grouping
related components
5
6
Relations between Problems, Algorithms and Programs
Problem
Algorithm Algorithm
Program ProgramProgram Program
. . . .
. . . . . . . .
What is a Good Algorithm?
Efficient :-
Running Time Space Used
Efficiency as function of input size :-
The number of bits in an input number Number of data elements ( numbers , points )
7
8
WORST- / AVERAGE- / BEST-CASE
Worst-case running time of an algorithm
The longest running time for any input of size n An upper bound on the running time for any input
guarantee that the algorithm will never take longer Example: Sort a set of numbers in increasing order; and the
data is in decreasing order The worst case can occur fairly often
E.g. in searching a database for a particular piece of information
Best-case running timesort a set of numbers in increasing order; and the data is already in increasing order
Average-case running timeAverage number of steps taken on any instance of size n
9
Pseudo- code -: A mixture of natural language and high –
level programming concepts that describes the main ideas behind a generic implementation of a data structure or algorithm
Eg:- Algorithm arrayMax ( A, n )
input : An array A sorting n integers Output : The Maximum element in A
currentMax A[0]for i 1 to n-1 do if currentMax < A[i] then currentMax
A[i]return currentMax
Pseudo – Code
It is more structured than usual language but less formal than a programming language .
Expressions:
Use standard mathematical symbols to
describe numeric and Boolean expression .
Use for assignment ( “=“ in java )
Use = for the equality relationship( “==“ in
java ) 10
Pseudo- Code
Programming Constructs :- decision structures :- if …. Then … [ else ]
while – loops : while .. Do repeat – loops : repeat … until .. for – loop : for .. Do array indexing : A[i] , A[I,j]
Methods : Calls : object method ( args ) returns : return value 11
Simple Algorithm
Find sum n numbers between any given range numbers
1 -Start
2 -Read N
3 -Sum =0
4 -For I= 1 to N
4.1 sum = sum + I
4.1 next I
5 -print Sum
6 -End 12
Basic Structure of C++ Language
13
Documentation Section
Link Section
Definition Section
Global Declaration Section
Subprogram SectionFunction 1…..Function n
main(){Declaration SectionExecutable Section }
14
Language
Built-in User-defined
Data
OperatorsAssignment) = (
Arithmetic)%,/,*,-,+( Relational)=!,==,=> ,> ,=< ,<(
Logical)|| , ( &&Unary)-- ,++(
Data Operators
Atomicint (2 bytes)
char (1 byte)float (4 bytes)
double (8 bytes)
Tokens
The smallest element in the C language is the token. Tokens can be:
Numeric constants : 123, 98.6, 1000000 Character constants : ‘A’, ‘a’, ‘$’, ‘4’ String constants : “UMBC”, “I like ice cream.”,
“123”, “CAR”, “car” Keywords : int , while, for Names (identifiers) Punctuation : ; : , ‘ “ [ ] { } ( ) Operators: =, +, -, *, /, <>, <, >, >=, !=, &&, ||, +
+, --
Simple C++Program
/*---- Hello World C Example ("hello.c") -------------------------------------*/
/* ANSI C Headers */
#include <iostream.h>
/* Main Program starts here */
void main( )
{
int i = 0;
/* End of declarations ... */
for ( i = 0; i < 10; i++ )
{
cout<<“Hello World”<<endl<< i ); /* print to standard
output */
}
}
15
Include headers if you need to use functions in these files
Comment :/* .. */
Declare all your variables first
Control Statement
Selection Iteration Jump
If For Break
Switch While Continue
………… Do- while Return
16
Eg:- to calculate the average of n number
/*Calculate the average of n numbers*/
#include<iostream.h>
void main(){
int n, count=1;
float x, average,sum=0;
cout<<"Enter the limit”<<endl; /*initialize and read in a value for
n*/
cin>>n;
while(count<=n) /*Read in the numbers*/
{
cout<<"x= “;
cin>>x;
sum+=x;
++count;
}
/*calculate the average and display result*/
average=sum/n;
cout<<“The average is ”<<average;
getch(); }
17
While
Syntax -:
While (Expression ) { Statement (s )
Increment counter }
Do – while
Syntax-:
Do {
statement(s)Counter}While(expression ) ;
18
Functions
A function is a self contained program segment that carries
out some specific,well-defined task.
C regards main() as function
Functions should be defined separately before or after main.
A function will process information that is passed to it from the
calling program and return a single value
Each function contains:
A function heading (function name), followed by optional
list of arguments enclosed in parentheses.
A list of argument declarations
A compound statement enclosed within a pair of braces
{ }19
Functions Cont..
Function Definition: returntype fn_name(type1 arg1,type2 arg2, …..type
n,arg n)
{ localvariables
functioncode }
Example: int maximum(int x, int y)
{ int z;
z=(x>=y) ? x:y;
return(z);}
Calling A function:
function-name(arg1,arg2,….arg n);
Function Prototype: returntype fn_name(type1 arg1,type2 arg2, …..type
n,arg n);
C ++ Program illustrating Function
/*Program to calculate the area of a circle*/
#include <iostream.h>
const float PI= 3.14;
float areacircle( float radius) ; /* function prototype*/
main() {
float radius, area;
cout<<"Enter value of radius“<<endl;
cin>>radius;
area=areacircle(radius); /* call function */
cout<<“Area= “<<area ;
getch();
}
float areacircle(float r) { /* definition of function */
a= PI*r*r;
return(a);
}20
Assignment -1-
Write algorithm for the following
sum of n numbers between given range ( Using while )
sum of n numbers between given range ( Using Do…
while )
count even& odd numbers in given range( Using For )
count even & odd numbers in given range( Using while )
count even & odd numbers in given range(Using Do..
while)
21