Successfully reported this slideshow.
Upcoming SlideShare
×

# Lect 3-4 Zaheer Abbas

2,471 views

Published on

Published in: Technology
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

### Lect 3-4 Zaheer Abbas

1. 1. Created By Zaheer Abbas Aghani
2. 2. Data Structure (ITC-322) Lecture 2 Algorithms
3. 3. Algorithms (Definition) <ul><li>An Algorithm (pronounced AL-go-rith-um) is a se quence of instructions specifying the steps required to accomplish some task. </li></ul><ul><li>OR </li></ul><ul><li>An algorithm is a finite step by step list of well defined instruction for solving a particular problem. </li></ul>
4. 4. Algorithm -- Examples <ul><li>A cooking recipe. </li></ul><ul><li>The rules of how to play a game. </li></ul><ul><li>VCR instructions. </li></ul><ul><li>Directions for driving from A to B. </li></ul><ul><li>A car repair manual. </li></ul>
5. 5. Computer Algorithms <ul><li>A computer program is another example of an algorithm. </li></ul><ul><li>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. </li></ul><ul><li>The computer then executes the program following each step to accomplish the end goal. </li></ul><ul><li>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. </li></ul>
6. 6. From Algorithms to Programs Problem C Program Algorithm : A sequence of instructions describing how to do a task (or process)
7. 7. Examples: <ul><li>Suppose Add two number a & b and store in c. </li></ul><ul><li>Steps: </li></ul><ul><ul><li>Step1: Initialize variable a and b [a=1, b=1] </li></ul></ul><ul><ul><li>Step2: Declare variable c. </li></ul></ul><ul><ul><li>Steps3: Add variables and store in other variable [ c= a+b]. </li></ul></ul><ul><ul><li>Steps4:Print output [c]. </li></ul></ul>
8. 8. Implement of Algorithm in C <ul><li>Void main() </li></ul><ul><li>{ </li></ul><ul><li>int a,b,c; //declaration of variables </li></ul><ul><li>a=1,b=1; //initialize variable a and b </li></ul><ul><li>c=a+b; // add a and b and store result in c </li></ul><ul><li>printf(“%d”,&c); // print output </li></ul><ul><li>} </li></ul>
9. 9. Example:- Algorithm: Drive_To_Uni <ul><li>Steps </li></ul><ul><li>{ </li></ul><ul><li>1. find car keys </li></ul><ul><li>2. disable car alarm </li></ul><ul><li>3. open car door </li></ul><ul><li>4. get in car </li></ul><ul><li>5. shut car door </li></ul><ul><li>6. put keys in ignition </li></ul><ul><li>7. start car </li></ul><ul><li>8. back car out of driveway </li></ul><ul><li>9. drive to end of street </li></ul><ul><li>10. turn right </li></ul><ul><li>11. drive to end of street </li></ul><ul><li>12. turn left </li></ul><ul><li>...etc...etc...etc </li></ul>...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 }
10. 10. Controls Structures <ul><li>A control structure or logic structure is a structure that controls the logical sequences in which programs instructions are executed. </li></ul><ul><li>OR </li></ul><ul><li>Control flow or Control structures refers to the order in which the individual statements, instructions, are executed. </li></ul><ul><li>Algorithms and their equivalent computer programs are more easily understood if they mainly use these types of control structures. </li></ul><ul><li>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. </li></ul><ul><li>In algorithm design structure, three types of control structure are used to form the logic of program. </li></ul>
11. 11. Control Structures Cont…. <ul><li>Sequential Logic or sequential Flow. </li></ul><ul><li>Selection Logic or Conditional Flow. </li></ul><ul><li>Iteration Logic or Repetitive Flow. </li></ul><ul><li>Sequential Logic : In sequence control structure, one program statement following another logical order. </li></ul><ul><li>OR </li></ul><ul><li>Steps are executed one after one </li></ul><ul><li>i.e Step1  Step2  Step3  Step4  So on… </li></ul>
12. 12. 2. Selection Logic <ul><li>Selection logic employs a number of conditions which leads to a selection of one out of several alternatives conditions. </li></ul><ul><li>The selection control structure also known as IF-THEN-ELSE structure. </li></ul><ul><li>Its offers two paths to follow when a decision must be made by a program. </li></ul><ul><li>The selection control structure fall into 3 types: </li></ul>
13. 13. Selection control structure types: <ul><li>Single Alterative : (One Condition): </li></ul><ul><li>This structure has the form: </li></ul><ul><li>IF condition, then </li></ul><ul><li>[Module A ] </li></ul><ul><li>End of IF structure. </li></ul><ul><li>------------------------------------- </li></ul><ul><li>Algorithm: </li></ul><ul><li>step1: start </li></ul><ul><li>step2: if today is wed” then goto step3. </li></ul><ul><li>Step3: print “ today is wed” </li></ul><ul><li>Step4: End/Exit </li></ul>
14. 14. Double Alterative: <ul><li>This structure has the form </li></ul><ul><li>IF condition, then </li></ul><ul><li>[Module A] </li></ul><ul><li>Else </li></ul><ul><li>[Module B] </li></ul><ul><li>End of IF Structure </li></ul><ul><li>------------------------------ </li></ul><ul><li>Algorithm: </li></ul><ul><li>Step1: start </li></ul><ul><li>Step2: if a=1 then goto step3 </li></ul><ul><li>Step3: Print “a=1” </li></ul><ul><li>Else </li></ul><ul><li>Step4: print “a!=1” </li></ul><ul><li>Step5: End/Exit. </li></ul>
15. 15. Multiple Alterative: <ul><li>This structure has the form. </li></ul><ul><li>IF condition1, then </li></ul><ul><li>[Module A] Else </li></ul><ul><li>IF condition2, then </li></ul><ul><li>[Module B] Else </li></ul><ul><li>IF condition3, then </li></ul><ul><li>[Module C] </li></ul><ul><li>Else </li></ul><ul><li>[Module D] </li></ul>Algorithm: Step1: start Step2: if a=1 then goto step3 Step3: print a=1. else Step4: if a=2 then goto step5 Step5: print a=2. Else Step6: if a=3 then goto step7 Step7: print a=3. Else Step8: print a!=1,2,3 Step9: End/Exit.
16. 16. 3. Iteration Logic OR Repetitive Flow <ul><li>In this iteration or loop control structure a process may be repeat as long as the condition remain true. </li></ul><ul><li>In this control structure, we use either two types of structure involving loops. </li></ul><ul><ul><li>For Loop </li></ul></ul><ul><ul><li>While Loop </li></ul></ul><ul><ul><li>For loop is used to repeat a process until a fix number of times </li></ul></ul><ul><ul><li>While loop is used to repeat a process or instructions until our condition is true. </li></ul></ul>
17. 17. Iteration logic Example: <ul><li>Algorithm: </li></ul><ul><li>Step1: start </li></ul><ul><li>Step2: declare variable x as interger </li></ul><ul><li>for{ </li></ul><ul><li>Step3: assign value 1 to x variable. </li></ul><ul><li>Step4: if x<=5 then goto step5 </li></ul><ul><li>Step5: print value of x. </li></ul><ul><li>Step6: add 1 to x (x++) </li></ul><ul><li>Step7: goto step4 </li></ul><ul><li>} </li></ul><ul><li>Else </li></ul><ul><li>Step8: End </li></ul>
18. 18. Program (For Loop) <ul><li>void main() </li></ul><ul><li>{ int x; for(x=1;x<=5;x++) </li></ul><ul><li>{ </li></ul><ul><li>printf(“n x=%d”,x); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>Output: </li></ul><ul><li>x=1 </li></ul><ul><li>x=2 </li></ul><ul><li>x=3 </li></ul><ul><li>x=4 </li></ul><ul><li>x=5 </li></ul>Format string Variable declaration Assignment of a value . check condition (value of x) Increament statement ( add 1 to x)
19. 19. Program (while loop) <ul><li>void main () </li></ul><ul><li>{ </li></ul><ul><li>int x=3; </li></ul><ul><li>while(x!=1) </li></ul><ul><li>{ </li></ul><ul><li>printf(“nData Structure”); </li></ul><ul><li>x--; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>OUTPUT: </li></ul><ul><li>Data Structure </li></ul><ul><li>Data Structure </li></ul>same as x = x- 1;
20. 20. Complexity of an Algorithm: <ul><li>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. </li></ul><ul><li>Efficiency of an algorithm depends on two major criteria. </li></ul><ul><ul><ul><li>First one is run time of that algorithm. </li></ul></ul></ul><ul><ul><ul><li>Second is space. </li></ul></ul></ul>
21. 21. <ul><li>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. </li></ul><ul><li>Lets suppose space is fixed of an algorithm then only run time will be considered for obtaining the complexity. </li></ul><ul><li>We take three cases for complexity of algorithms. </li></ul><ul><ul><li>Best Case </li></ul></ul><ul><ul><li>Worst Case </li></ul></ul><ul><ul><li>Average Case </li></ul></ul>
22. 22. <ul><li>Best Case: In this case, the algorithm takes less time to execute or algorithm searches the element in first time. </li></ul><ul><li>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. </li></ul><ul><li>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. </li></ul>
23. 23. FLOWCHART
24. 24. FLOWCHART <ul><li>A program flowchart is a graphical representation of how process works. </li></ul><ul><li>OR </li></ul><ul><li>A program flowchart is a chart that graphical present the detail series of steps (algorithm or logical flow) needed to solve a programming problem. </li></ul><ul><li>The program flowchart can be likened to the blueprint of a building. </li></ul>
25. 25. Meaning of a flowchart: <ul><li>A flowchart is a diagrammatic representation that illustrates the sequence of operation to be performed to get the solution of a problem. </li></ul><ul><li>Flowcharts facilitate communication between the programmers and business people. </li></ul><ul><li>Flowchart play a vital role in the programming of a problem and are quite helpful in understanding the logic of complicated and lengthy problems. </li></ul><ul><li>Once the flowchart is drawn, it becomes easy to write the program in any high level language. </li></ul>
26. 26. Design the Flowchart <ul><li>The flowchart is drawn according to define rules and using standard flowchart symbols. </li></ul>
27. 27. Symbols of FlowChart <ul><li>Terminator: An oval flowchart indicating the start and end of process. </li></ul><ul><li>Process: A rectangular shape indicating a normal process flow step or calculation or assigning of values to variables. </li></ul><ul><li>Data: A parallelogram shape indicating any statement that causes data to be input to a program or output from the program. </li></ul>
28. 28. Symbols of Flowchart: <ul><li>Decision: A diamond shape indicating conditional steps. </li></ul><ul><li>Diamond shape have two paths (True/False or Yes/No) </li></ul><ul><li>Connector: A small, labelled circular shape used to indicate a jump in the process flow. </li></ul><ul><li>Arrows: Arrows are used to connect symbols and indicates the sequence of operations. </li></ul>
29. 29. Example: <ul><li>Program: </li></ul><ul><li>Void main() </li></ul><ul><li>{ </li></ul><ul><li>int a=1; </li></ul><ul><li>if(a=1) </li></ul><ul><li>{ </li></ul><ul><li> printf(“a is equal to 1”); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Start Int a=1 If a=1 Print a=1 End NO YES