Lect 3-4 Zaheer Abbas
-
Upload
information-technology-center -
Category
Technology
-
view
2.072 -
download
6
description
Transcript of Lect 3-4 Zaheer Abbas
Created ByZaheer Abbas Aghani
Data Structure (ITC-322)
Lecture 2
Algorithms
Algorithms (Definition)
An Algorithm (pronounced AL-go-rith-um) is a sequence of instructions specifying the steps required to accomplish some task.
OR
An algorithm is a finite step by step list of well defined instruction for solving a particular problem.
Algorithm -- Examples
A cooking recipe. The rules of how to play a game. VCR instructions. Directions for driving from A to B. A car repair manual.
Computer Algorithms
A computer program is another example of an algorithm.
To make a computer do anything, you have to write a computer program. To write a computer program you have to tell the computer step by step, exactly what you want to do.
The computer then executes the program following each step to accomplish the end goal.
When you telling the computer what to do, you also get to choose how it’s going to do it. That’s where computer algorithms come in.
From Algorithms to Programs
Problem
C ProgramC Program
AlgorithmAlgorithm: A sequence of instructions describing how to do a task (or process)
Examples:
Suppose Add two number a & b and store in c.
Steps: Step1: Initialize variable a and b [a=1,
b=1] Step2: Declare variable c. Steps3: Add variables and store in other
variable [ c= a+b]. Steps4:Print output [c].
Implement of Algorithm in C
Void main(){
int a,b,c; //declaration of variablesa=1,b=1; //initialize variable a and b
c=a+b; // add a and b and store result in cprintf(“%d”,&c); // print output
}
Example:- Algorithm: Drive_To_Uni
Steps { 1. find car keys 2. disable car alarm 3. open car door 4. get in car 5. shut car door 6. put keys in ignition 7. start car 8. back car out of
driveway 9. drive to end of street 10. turn right 11. drive to end of street 12. turn left ...etc...etc...etc
...etc...etc...etc...
52. find parking space
53. pull into parking space
54. turn off engine
55. remove keys from ignition
56. open car door
57. get out
58. shut car door
59. lock car door
60. enable alarm
}
Controls Structures
A control structure or logic structure is a structure that controls the logical sequences in which programs instructions are executed.
OR Control flow or Control structures refers to the order in which
the individual statements, instructions, are executed. Algorithms and their equivalent computer programs are
more easily understood if they mainly use these types of control structures.
An algorithm or computer program is usually not limited to a linear sequence of instructions. During its process it may bifurcate, repeat code or take decisions.
In algorithm design structure, three types of control structure are used to form the logic of program.
Control Structures Cont….
i. Sequential Logic or sequential Flow.ii. Selection Logic or Conditional Flow.iii. Iteration Logic or Repetitive Flow.
I. Sequential Logic: In sequence control structure, one program statement following another logical order.
OR Steps are executed one after one i.e Step1 Step2Step3Step4So on…
2. Selection Logic
Selection logic employs a number of conditions which leads to a selection of one out of several alternatives conditions.
The selection control structure also known as IF-THEN-ELSE structure.
Its offers two paths to follow when a decision must be made by a program.
The selection control structure fall into 3 types:
Selection control structure types:
Single Alterative: (One Condition):
This structure has the form:IF condition, then
[Module A ]End of IF structure.
-------------------------------------Algorithm:step1: startstep2: if today is wed” then goto step3.Step3: print “ today is wed” Step4: End/Exit
Double Alterative:
This structure has the formIF condition, then
[Module A]Else
[Module B]End of IF Structure
------------------------------Algorithm:Step1: startStep2: if a=1 then goto step3 Step3: Print “a=1”ElseStep4: print “a!=1”Step5: End/Exit.
Multiple Alterative:
This structure has the form.IF condition1, then[Module A] ElseIF condition2, then[Module B] ElseIF condition3, then[Module C] Else[Module D]
Algorithm:Step1: startStep2: if a=1 then goto step3Step3: print a=1.elseStep4: if a=2 then goto step5Step5: print a=2.ElseStep6: if a=3 then goto step7Step7: print a=3.ElseStep8: print a!=1,2,3Step9: End/Exit.
3. Iteration Logic OR Repetitive Flow
In this iteration or loop control structure a process may be repeat as long as the condition remain true.
In this control structure, we use either two types of structure involving loops. For Loop While LoopFor loop is used to repeat a process until a
fix number of timesWhile loop is used to repeat a process or
instructions until our condition is true.
Iteration logic Example:
Algorithm:Step1: startStep2: declare variable x as intergerfor{Step3: assign value 1 to x variable.Step4: if x<=5 then goto step5Step5: print value of x.Step6: add 1 to x (x++)Step7: goto step4}ElseStep8: End
Program (For Loop)
void main() {
int x;
for(x=1;x<=5;x++){
printf(“\n x=%d”,x);}
}
Output:
x=1x=2x=3x=4x=5
Format string
Variable declarationAssignment
of a value.
check condition (value of x)
Increament statement ( add 1 to x)
Program (while loop)
void main (){
int x=3;
while(x!=1){
printf(“\nData Structure”);
x--;}
}
OUTPUT:
Data StructureData Structure
same as
x = x- 1;
Complexity of an Algorithm:
The analysis of algorithm is a major task in computer science. In order to compare algorithm, we must have some criteria to measure the efficiency or complexity of an algorithm.
Efficiency of an algorithm depends on two major criteria.
First one is run time of that algorithm. Second is space.
We implement different data structures, some data structure takes more space but improves the run time and some take less space but its run time is slow.
Lets suppose space is fixed of an algorithm then only run time will be considered for obtaining the complexity.
We take three cases for complexity of algorithms. Best Case Worst Case Average Case
Best Case: In this case, the algorithm takes
less time to execute or algorithm searches the element in first time.
Worst Case: In this case, algorithm takes more time to execute or its fail to achieve its goal or in this case, algorithm finds the element at end or searching of element is fail.
Average Case: Here we take probability with list of data. Average case algorithm should be average number of steps but since data can be at any place, so finding exact behavior of algorithm is difficult.
FLOWCHART
FLOWCHART
A program flowchart is a graphical representation of how process works.
ORA program flowchart is a chart that graphical
present the detail series of steps (algorithm or logical flow) needed to solve a programming problem.
The program flowchart can be likened to the
blueprint of a building.
Meaning of a flowchart:
A flowchart is a diagrammatic representation that illustrates the sequence of operation to be performed to get the solution of a problem.
Flowcharts facilitate communication between the programmers and business people.
Flowchart play a vital role in the programming of a problem and are quite helpful in understanding the logic of complicated and lengthy problems.
Once the flowchart is drawn, it becomes easy to write the program in any high level language.
Design the Flowchart
The flowchart is drawn according to define rules and using standard flowchart symbols.
Symbols of FlowChart
Terminator: An oval flowchart indicating the start and end of process.
Process: A rectangular shape indicating a normal process flow step or calculation or assigning of values to variables.
Data: A parallelogram shape indicating any statement that causes data to be input to a program or output from the program.
Symbols of Flowchart:
Decision: A diamond shape indicating conditional steps.Diamond shape have two paths (True/False or Yes/No)
Connector: A small, labelled circular shape used to indicate a jump in the process flow.
Arrows: Arrows are used to connect symbols and indicates the sequence of operations.
Example:
Program:
Void main()
{
int a=1;
if(a=1)
{
printf(“a is equal to 1”);
}
}
Start
Int a=1
If a=1
Print a=1
End
NO
YES