algorithms 1

24
PRESENTED TO :- 1 Madam Huma

Upload: muhammad-uzair-rasheed

Post on 22-Apr-2015

438 views

Category:

Education


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Algorithms 1

PRESENTED TO :-

1

Madam Huma

Page 2: Algorithms 1

Presented By:-

Muhammad Uzair Rasheed

2009-CPE-03

UCE&T BZU MULTAN

2

Page 3: Algorithms 1

3

The Objective of Today’s Presentation

To become familiar with the concept of algorithms:

– What they are?

– What is their use?

– What do they consist of?

– What are the techniques used for representing them?

– How do we can analyze them?

Page 4: Algorithms 1

4

al.go.rithmSequence of steps

that can be taken to solve a given problemstepssequence

Page 5: Algorithms 1

5

Solving Problems (1)

When faced with a problem:

1. We first clearly define the problem

2. Think of possible solutions

3. Select the one that we think is the best under the prevailing circumstances

4. And then apply that solution

5. If the solution woks as desired, fine; else we go back to step 2

Page 6: Algorithms 1

6

Solving Problems (2)

• It is quite common to first solve a problem for a particular case

• Then for another

• And, possibly another

• And watch for patterns and trends that emerge

• And to use the knowledge form those patterns and trends in coming up with a general solution

Page 7: Algorithms 1

7

Solving Problems (3)

• It helps if we have experienced that problem or similar ones before

• Generally, there are many ways of solving a given problem; the best problem-solvers come-up with the most appropriate solution more often than not!

• The process that can be used to solve a problem is termed as the “algorithm”

Page 8: Algorithms 1

8

Examples

• Addition

• Conversion from decimal to binary

• The process of boiling an egg

• The process of mailing a letter

• Sorting

• Searching

Page 9: Algorithms 1

9

Let us write down the algorithm for a problem that is familiar to us

Converting a decimal number into binary

Page 10: Algorithms 1

10

Convert 75 to Binary75237 1218 129 024 122 021 020 1

1001011

remainder

Page 11: Algorithms 1

11

Algorithm for Decimal-to-Binary Conversion

1. Write the decimal number

2. Divide by 2; write quotient and remainder

3. Repeat step 2 on the quotient; keep on repeating until the quotient becomes zero

4. Write all remainder digits in the reverse order (last remainder first) to form the final result

Page 12: Algorithms 1

12

Three Requirements:

1. Sequence is:a. Precise

b. Consists of a limited number of steps

2. Each step is:a. Unambiguous

b. Executable

3. The sequence of steps terminates in the form of a solution

Page 13: Algorithms 1

13

Why Algorithms are Useful?

• Once we find an algorithm for solving a problem, we do not need to re-discover it the next time we are faced with that problem

• Once an algorithm is known, the task of solving the problem reduces to following (almost blindly and without thinking) the instructions precisely

• All the knowledge required for solving the problem is present in the algorithm

Page 14: Algorithms 1

14

Why Write an Algorithm Down?

• For your own use in the future, so that you don’t have spend the time for rethinking it

• Written form is easier to modify and improve

• Makes it easy when explaining the process to others

Page 15: Algorithms 1

15

Analysis of Algorithms• Analysis in the context of algorithms is concerned with

predicting the resources that re requires:– Computational time– Memory– Bandwidth– Logic functions

• However, Time – generally measured in terms of the number of steps required to execute an algorithm - is the resource of most interest

• By analyzing several candidate algorithms, the most efficient one(s) can be identified

Page 16: Algorithms 1

16

A Selection of Algorithmic Application Areas

• Search

• Sort

• Cryptography

• Numeric

• Graphical

• Quantum computing

Page 17: Algorithms 1

17

We’ll now talk about the various ways of representing algorithms.

But, before we do that please allow me to say a few words about …

Page 18: Algorithms 1

18

Syntax & Semantics

An algo. is “correct” if its:– Semantics are correct– Syntax is correct

Semantics:The concept embedded in an algorithm (the soul!)

Syntax:The actual representation of an algorithm (the body!)

WARNINGS:

1. An algo. can be syntactically correct, yet semantically incorrect – very dangerous situation!

2. Syntactic correctness is easier to check as compared with semantic

Page 19: Algorithms 1

19

Now onto Algorithm Representation

• We have said enough about algorithms – their definition, their types, etc.

• But, how do we actually represent them?

• Generally, SW developers represent them in one of three forms:– Pseudo code– Flowcharts– Actual code

Page 20: Algorithms 1

HOW TO WRITE AN ALGORITHM

• Part-1:-• NAME OF ALGORITHM(NAME OF

VARIABLES USED)• BRIEF DISCRIPTION ABOUT VARIABLES

USED IN SIMPLE ENGLISH• Part-2:-• Series of steps to solve a problem

20

Page 21: Algorithms 1

OPERATIONS THAT CAN BE PERFORMED

• TRAVERSING

• INSERTION

• DELETION

• SORTING

• MERGING21

Page 22: Algorithms 1

Types of sorting

• Bubble sorting.

• Selection sorting.

• Insertion sorting.

• Merge sorting.22

Page 23: Algorithms 1

Method of Bubble sorting

• To sort data in an array of n elements, n-1 iterations are required.

• There are following steps explain sorting of data in ascending order using bubble sort method:

23

Page 24: Algorithms 1

• In first Iteration, the largest value moves to the last position in the array.

• In the second iteration the above process is repeated and the second largest value moves to the second last position and so on.

• In n-1 iteration, the data is arranged in ascending order.

24