Lecture 2. Algorithms and Algorithm Convention 1.

Slides:



Advertisements
Similar presentations
Chapter 1: INTRODUCTION TO DATA STRUCTURE
Advertisements

Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
MATH 224 – Discrete Mathematics
One Dimensional Arrays
1 CSE1301 Computer Programming: Lecture 15 Flowcharts and Debugging.
Data Structures & Algorithms What The Course Is About s Data structures is concerned with the representation and manipulation of data. s All programs.
Course Overview CS221 – Advanced Programming Fall 2007 : Ray S. Babcock Computer Science Department Montana State University.
Basic Definitions Data Structures: Data Structures: A data structure is a systematic way of organizing and accessing data. Or, It’s the logical relationship.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
1 CSE1301 Computer Programming: Lecture 15 Flowcharts, Testing and Debugging.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
The Design and Analysis of Algorithms
DATA STRUCTURE Subject Code -14B11CI211.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
1. Reference  2  Algorithm :- Outline the essence of a computational procedure, step by step instructions.  Program :- an.
Design and Analysis of Algorithms CSC201 Shahid Hussain 1.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Lecture 4. RAM Model, Space and Time Complexity
Instructor Information: Dr. Radwa El Shawi Room: Week # 1: Overview & Review.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
DATA STRUCTURES (CS212D) Week # 1: Overview & Review.
Course Teacher: Moona Kanwal
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter
Prepared By Ms.R.K.Dharme Head Computer Department.
 DATA STRUCTURE DATA STRUCTURE  DATA STRUCTURE OPERATIONS DATA STRUCTURE OPERATIONS  BIG-O NOTATION BIG-O NOTATION  TYPES OF DATA STRUCTURE TYPES.
Software Life Cycle What Requirements Gathering, Problem definition
How to start Visual Studio 2008 or 2010 (command-line program)
Data Structure Introduction.
Lecture -3 on Data structures Array. Prepared by, Jesmin Akhter, Lecturer, IIT, JU Array Data structures are classified as either linear or nonlinear.
Lecture by: Prof. Pooja Vaishnav.  Language Processor implementations are highly influenced by the kind of storage structure used for program variables.
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
Elementary Data Organization. Outline  Data, Entity and Information  Primitive data types  Non primitive data Types  Data structure  Definition 
Algorithms Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin,
Review Sorting algorithms Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort.
Data Structures & Algorithms CHAPTER 1 Introduction Ms. Manal Al-Asmari.
DATA STRUCTURES (CS212D) Overview & Review Instructor Information 2  Instructor Information:  Dr. Radwa El Shawi  Room: 
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
COMP Loop Statements Yi Hong May 21, 2015.
Data Structure and Algorithms
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To introduce the basic concepts of linked lists ❏ To introduce the basic concepts.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
1 CSE1301 Computer Programming: Lecture 16 Flow Diagrams and Debugging.
1 CSC103: Introduction to Computer and Programming Lecture No 9.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 6: Stepwise refinement revisited, Midterm review.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Introduction toData structures and Algorithms
Introduction to Algorithms
© 2016 Pearson Education, Ltd. All rights reserved.
The Design and Analysis of Algorithms
Lectures linked lists Chapter 6 of textbook
Operation performed by Linear Structure
Data Structures and Algorithms
GC211Data Structure Lecture2 Sara Alhajjam.
Lecture – 2 on Data structures
Recap RAM model is used to measure the run time of an algorithm by counting the number of steps. Space complexity of an algorithm refer to the amount of.
3.1 Algorithms (and real programming examples).
Data Structures (CS212D) Overview & Review.
Algorithm.
Analysis Algorithms.
Structured Program
A finite sequence of operations that solves a given task
Week # 1: Overview & Review
Data Structures (CS212D) Overview & Review.
Visit for more Learning Resources
Revision of C++.
Presentation transcript:

Lecture 2. Algorithms and Algorithm Convention 1

Recap Data structure is way for temporary storing and manipulation of data Data structures are classified as linear and non linear on the base data representation and logical and physical on the base of memory representation. Common operation for all data structures are Insert, Delete, Merge, Sort and search. Examples of linear and sequential data structures are Array, Stack and Queues. Examples of linear and pointer based data structure is Linked List. Examples of non linear data structures are Tree and graphs. 2

Today Topics What is an algorithm Features of Algorithms Algorithm’s Notations 3

At the End of Lecture At the end of lecturer you will be able To understand the features of algorithm To understand the use of algorithm’s notation How to design algorithms and Pseudo codes 4

What is an algorithm An algorithm is a step by step procedure to solve a problem – Plate form independent – Can be implement in any procedural langauge Simplest form (for experts) is Pseudo code Pseudo code can be written in any language syntax, but here in this course we will used C/C++ syntax Two important factors – Space Tradeoff (Refer to less use of memory i.e. RAM) – Time Tradeoff (Refer to less time of Micro processor for execution) 5

Features of algorithm Complete Finite At least one Output 0,1 or more inputs Correct Clarity 6

An Algorithm (Example) Algorithm SUM (No1, No2, Res) { This algorithm is used to read two numbers and print its sum } Step-1 [ Read numbers No1 and No2] Read(No1,No2) Step-2 [Calculate the sum] Res=No1+No2 Step-3 [Display the Result ] Write(Res) Step-4 {Finish] Exit 7 C Language Program: void main() { int no1, no2, res; scanf(“%d%d”, &a, &b); res = a+b; printf(“Sum=%d”, res); }

Algorithms Notations Algorithm Name – Should be in capital form – Meaningful Parameters – Should be enclosed in parenthesis ( ) – Variable name comprises on more than one characters – Scripting or looping variable are of single character Introductory Comment – Description and purpose of an algorithm Steps – Finite steps 8

Algorithms Notations (Cont !!!) Comments – Each step start with a comment – Enclose in [ ] – Define the purpose of step Input Statements – Read – Scanf (if using C/C++) Output statement – Write – Printf (if using C/C++) 9

Algorithms Notations (Cont !!!) Selection statement – If –Then –End If – If – then ---Else --- End If – Nested If – Example If ( a>b ) then write ( a+”Is Large”) Else write ( b+”Is Large”) End if 10

Algorithms Notations (Cont !!!) Loops ( For, While, Until ) – Example-1 Repeat Step 2 For i=1, N, 1 – Example-2 Repeat Step 2 to Step 4 For i=1, N, 1 – Example-3 Repeat Step 2 while i<=10 – Example-4 Repeat Step 2 Until i>10 11 Note:- Purpose of all examples is same i.e. to perform loop 10 times

Algorithms Notations (Cont !!!) Finish – Exit (Used in main algorithm) – Return (Used in sub algorithm) 12

Example-1. Write an algorithms or Pseudo code to read two number and display the largest number. 13

Algorithm Example-1. Algorithm LARGE(No1, No2, lar) { This algorithm is used to read two numbers and print the largest} Step-1 [ Read numbers No1 and No2] Read(No1,No2) Step-2 [Find the largest] if (No1 > No2) then lar = No1 else lar = No2 end if 14

Algorithm Example-1 (Cont !!!) Step-3 [Display the Result ] Write(lar) Step-4 {Finish] Exit 15

Algorithm Example-1 (Cont !!!) Write the algorithms convention which are use in Example-1. Algorithm Name (Large ) Parameters (No1, No2 and lar) Input and Output Statement(Read, Write) Selection Statement (if-else) Comments (enclosed in [ ] before start of each step) Introductory comments (enclosed in { } ) Finish (exit) 16

Pseudo code Example-1 (Cont !!!) Large (a,b, lar) { // Find the largest number if (a>b) lar=a; else lar=b; } 17

Example-2. Write an algorithms or Pseudo code to read an array of N integer values, calculate its sum and then print the sum 18

Algorithm Example-2. Algorithm SUM(Ary[ ], I, total, N) { This algorithm is used to read an array Ary of size N and display its sum } Step-1 [ Perform a loop to read the values of array] Repeat step 2 for i=1,N,1 Step-2 [Read value ] Read(Ary[i]) Step-3 [Initialize variable I and total] a. I =1 b. total = 0 Step-4 [ Perform a loop to traverse the all elements of array ary and add] Repeat step 5 while i<=10 19

Algorithm Example-2 (Cont !!!) Step-5 [Add the values] total = total + ary [ I ] Step-6 {Display the sum of values] Write (total) Step-7 {Finish] Exit 20

Algorithm Example-2 (Cont !!!) Write the algorithms convention which are use in Example-2. Algorithm Name (SUM) Parameters (ary[], I, N, total) Input and Output Statement (Read, Write) Loop Statement (for, while) Assignment Comments (enclosed in [ ] before start of each step) Introductory comments (enclosed in { } ) Finish (exit) 21

Pseudo code Example-2(Cont !!!) Large (Ary, N, I, Total) { for(i=0;i<=N-1;i++) scanf(“%d”, Ary[i]); i=0; total=0; while(i<=N-1) total=total+ Ary[i]; printf(“%d”, total); } 22

Example-3. Write an algorithms or Pseudo code to find the factorial of number N using sub algorithm approach. 23

Algorithm Example-3. Algorithm FACTORIAL( No, Res) { This algorithm is used to read a number No and print its factorial } Step-1 [ Read the number No] Read (No) Step-2 [Call Sub algorithm FACT] Call Res = FACT(No) Step-3 [ Display factorial ] Write (Res) Step-4 [ Finish ] Exit 24 SubAlgorithm FACT( NewNo, I, f) { This Sub algorithm is used to receive an element from main algorithm FACORIAL and return the result } Step 1. [ Initialize variable ] f =1 Step2. [ Perform loop to calculate factorial] Repeat step 3 for i=1, NewNo, 1 Step 3. [Calculate f ] f = f * I Step 4 [ Finish ] return ( f)

Algorithm Example-3 (Cont !!!) Write the algorithms convention which are use in Example-3. Algorithm Name (FACTORIAL) Sub Algorithm Name (FACT) Parameters of FACTORIAL (No, Res) Parameters of FACT (NewNo, F, I) Input and Output Statement (Read, Write) Loop Statement (for, while) Assignment Comments (enclosed in [ ] before start of each step) Introductory comments (enclosed in { } ) Finish (exit, return) 25

Pseudo code Example-3(Cont !!!) Facorial(No, Res) { Res= Fact (No) Printf(“%d”, Res) } 26 Fact(NewNo) { for(i=1;i<=N;i++) F = F * I Return(f) }

Summary An algorithm is a step by step process to solve a problem. An algorithm is platform Independent and you can make a computer program in any language. No of Inputs, outputs, completeness, accuracy, correctness and finite are the main features of algorithms Algorithms notation are used to design an algorithm Pseudo code are used by experts 27

Home Work Write an algorithm which read three numbers and print the smallest number. Also write a C language program. Write an algorithm which read an array of 10 integers and count the even numbers. Also write a C language program.\ Write an algorithm which read two values and find its product using a sub algorithm. Also write a C language program. 28