program abstraction
Post on 31-Dec-2015
21 Views
Preview:
DESCRIPTION
TRANSCRIPT
Program Abstraction
• The solution to a given problem should not be based on a
particular computer language.
• Otherwise, we spend more time in thinking about how to fit the
solution to language syntax rather than thinking about the logic
of solution to the problem.
• We should think about the solution ignoring the language
constructs such as input and output statements, data types,
syntax of iteration and selection structures etc.,
• These decisions can be made when we are certain that the solution
correctly solves the problem.
• Developing a solution to a problem by ignoring the context of
programming language is called 'program abstraction'.
• Such an abstract solution is called an 'algorithm'.
• Once developed, the algorithmic solution must be documented for
later use.
• There are two notations to document algorithms, namely, pseudo-code
and flow-chart.
Problems, Algorithms and Programs
• An Algorithm is the method one has to use to solve a
Problem.
• A Program is the precise expression of an algorithm in a
language that could be followed by a computer.
Software Development Process
• Understand the Problem
• Come up with an Algorithm to solve it
• Flesh out the Algorithm in a program
What is an Algorithm?
• An algorithm is a methodical approach to solving a
problem
• Examples:
– An algorithm to multiply X and Y is to add X to itself Y times
– An algorithm to compute the average of N numbers is to add
them up and then divide by N
The Makeup of an Algorithm
• An algorithm is made up of a set of “basic steps” that are
followed sequentially.
• Each algorithm must have a single “entry” point from which
the algorithm is started.
• Each algorithm must have one or more “exit” points that
indicate when the algorithm is finished.
What Constitutes a Basic Algorithmic Step?
• Input a value into a “memory variable”
• Output a value from a “memory variable”
• Assigning and reassigning a value to a variable
• Performing a math or some other operation on a number of
memory variables
• Deciding the next step based on the outcome of a yes/no question
– Decision/choice
Tracing of an Algorithm Tracing an algorithm means checking or verifying the algorithm or
flowchart whether is working properly or not.
It is one of the way to find out errors in an algorithm or flowchart.
Through trace table one can easily detect error in their flowchart.
Trace table is also known as verification table.
Through algorithm we can draw flowchart and from flowchart we can
draw the Trace Table.
In Trace Table we take Steps, Input variable, Intermediate Variable and
Output.
Examples:1) To perform addition of two numbers:• Algorithm:-1. Start2. Read Values a and b3. Compute a + b and store result in sum.4. Print value of Sum5. Stop
• Pseudo Code:- Begin input a, b sum = a+ b Print sum End
Start
Read a & B
Sum = a + b
Print Sum
Stop
Tracing of algorithm with a=10 and b=20
Input output
steps a b Sum
1 10 20 ---
2 10 20 30
3 10 20 30
1) To perform addition of two numbers and square of sum:• Algorithm:-1. Start2. Read Values a and b3. Compute a + b and store result in sum.4. Compute sum * sum and store in square.5. Print value of Square6. Stop
• Pseudo Code:- Begin input a, b sum = a+ b square = sum * sum Print square End
Start
Read a & B
Sum = a + b
Print Square
Stop
Square= sum * sum
Tracing of an algorithm with a=3 and b=2
Input
Intermediate
output
steps a b Sum Square
1 3 2 ---
2 3 2 5 ---
3 3 2 5 25
4 3 2 5 25
3) To find maximum of 2 numbers:• Algorithm:-1.Start2.Input two numbers a and b3. If a > b then goto step 4 else goto step 54.Output a5.Output b6.Stop
Start
Input a,b
If a > b
Output bOutput a
Stop
No Yes
Input
Intermediate
output
steps
a
b --- ---
1 5 10 --- ---
2 5 10 --- ---
3 5 10 --- 10
Tracing with a=5 and b=10
Implementation Of Algorithm
• Algorithms must be designed in such a fashion that they follows the pure top
down approach.
• This ensures the straight line execution path.
• Following points should be kept in mind while writing algorithms:
1)Use of procedures:
We write program referring the algorithm.
It should emphasis on development of implementation & readability.
Modularizing programs will accomplish both of these needs.
This allows us to implement independent separate procedures to perform
specific task.
The main program can have references to different procedures.
2) Choice of variable Names: By choosing appropriate variable names & constants we can make
programs more meaningful and easier to understand.
Eg: If we wish to add the two numbers and store the result; we can use
variables as Num1, Num2 and Sum instead of a,b,c.
This makes the program self explanatory & self documented.
One should ensure that each variable should have only one role in a
given program.
3)Documentation of Programs: This is a useful practice to have in programs.
Brief and accurate comments should be written at the beginning of each
procedure/function.
Program documentation is a good practice because the program can be
used easily by other people unfamiliar with the working and input
requirements of the program.
This indicates that the program must specify during execution exactly
what responses require from the user.
4)Debugging the program: It is necessary to carry out number of tests during implementation of algorithm ,
to ensure that the program is behaving correctly according to its specifications.
Program may have some logical errors which may not be detected during
compilation.
To detect such type of errors one may include “print” statements to print
information at strategic points in computations.
There are no complete methods for debugging.
However following steps can be taken to ease the burden of process:
1) Always work the program through by hand before ever attempting execute it.
2) Print out the strategic information.
5) Program Testing: The program should be tested against all possible inputs.
Some of the things we might check are whether the program solves the
smallest possible problem, whether it handles all the cases for varying
input data.
Whenever possible programs should be accompanied by input and
output sections.
From Algorithm to a Program• Flowcharts are aimed at “humans”; they help programmers “visualize”
the algorithm to be followed to solve a problem.
• Flowcharts are not suitable for computers to execute because they are
not “detailed” and “specific” enough.
• To represent an algorithm to be followed by a computer, we have to
express that algorithm as a program.
Representation of an Algorithm
• An algorithm can be represented as:1) Flow Charts2) Pseudo code3) Programs
Flow Charts
• The flowchart is a means of visually presenting the flow of data through
an information processing systems, the operations performed within
the system and the sequence in which they are performed.
• A flowchart is a diagrammatic representation that illustrates the
sequence of operations to be performed to get the solution of a
problem.
• Flowcharts are generally drawn in the early stages of formulating
computer solutions.
• Flowcharts facilitate communication between programmers and
business people.
• Flowcharts 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.
• Hence, it is correct to say that a flowchart is a must for the better
documentation of a complex program.
Symbols Start/Stop
Assignment Statements, Expressions
Input/Output
Decision Making
Connector
Flow Indicators
ADVANTAGES OF USING FLOWCHARTS The benefits of flowcharts are as follows: 1. Communication: Flowcharts are better way of communicating the logic
of a system to all concerned.
2. Effective analysis: With the help of flowchart, problem can be analyzed in
more effective way.
3. Proper documentation: Program flowcharts serve as a good program
documentation, which is needed for various purposes.
4. Efficient Coding: The flowcharts act as a guide or blueprint during the
systems analysis and program development phase.
5.Proper Debugging: The flowchart helps in debugging process.
6.Efficient Program Maintenance: The maintenance of operating program becomes easy with
the help of flowchart. It helps the programmer to put efforts more efficiently on that part
LIMITATIONS OF USING FLOWCHARTS
1.Complex logic: Sometimes, the program logic is quite complicated. In that case,
flowchart becomes complex and clumsy.
2. Alterations and Modifications: If alterations are required the flowchart may require re-drawing
completely.
3.Reproduction: As the flowchart symbols cannot be typed, reproduction of flowchart
becomes a problem.
4.The essentials of what is done can easily be lost in the technical details of how it is done.
Pseudo code
• Pseudo-Code is simply a numbered list of instructions to perform some
task.
• It consists of short, English phrases used to explain specific tasks within
a program’s algorithm.
• It is also called as Structured English.
• It requires an algorithm to be specified with the same precision as that
of the programming language in which the program is eventually
written.
• By moving from diagrams to text , pseudo code brings the design
process a step closer to the coding process and makes the ultimate
translation of the algorithm into a programming language somewhat
easier.
Why is pseudo code necessary?• Programming process is a complicated task.
• Firstly it is necessary to understand the program specifications.
• Then it is necessary to organize thoughts and create the program.
• This is a difficult task when program is big and complex.
• A main task is broken down into smaller ones, in order to be able
to write fully developed code.
• Writing Pseudo Code will save time later during the construction
and testing phase of a program’s development.
How to write pseudo code?
• First make a list of the main tasks that must be accomplished on a
piece of scratch paper.
• Then, focus on each of those tasks.
• Try to break each main task down into very small tasks that can
each be explained with a short phrase.
• There may eventually be a one-to-one correlation between the
lines of pseudo code and the lines of the code that you write after
you have finished pseudo coding.
• Functions and procedures can be shown within pseudo code.
• Purpose of pseudo code is to help the programmer efficiently
write code. Therefore, you must honestly attempt to add enough
detail and analysis to the pseudo code.
• In the professional programming world, workers who write
pseudo code are often not the same people that write the actual
code for a program. In fact, sometimes the person who writes the
pseudo code does not know beforehand what programming
language will be used to eventually write the program.
Pseudo code Language Constructions
• Input/Output: Get “variable”,”variable”,…. Display “variable”,”variable”,….
• Computation/Assignment: Set the value of “variable” to “arithmetic expression”
or“variable” = “expression”
• Conditional: 6. If “condition” 6.1 statement 1 6.2 ………… 7. Else 7.1 statement 2 7.2 ………….• Iterative : 9. While “condition” 9.1 statement 1 9.2 …………………
Algorithm, Pseudo code, Flowchart to add two numbers
• Algorithm:-1. Start2. Read Values a and b3. Compute a + b and store result in sum.4. Print value of Sum5. Stop
• Pseudo Code:- Begin input a, b sum = a+ b Print sum End
Start
Read a & B
Sum = a + b
Print Sum
Stop
SEARCHING
• Information retrieval is one of the most important applications of
computers.
• Suppose you want to identify the name of student, whose roll no is 150.
• Is it sufficient for you to get the information which you need.? No since
there may be many classes.
• We may now add another attribute to our query- “second year”. Even
now it may fail, as there may be branches. So another attribute branch
name.
• Thus we can now formulate the query as:
• Find the name of student having Roll. No. 150, studying in second year
computer science.
• Every time we are given with a piece of information (e.g. Roll no)
and we are asked to find the record that contains associated
information for the given piece of information.
• The initial piece of information is called “Key”.
• Using Key, we are retrieving the information from the database.
This is called searching.
• Hence searching is a technique where we try to obtain certain
information, given a key information.
Sequential Search
The simplest way to do a search is to begin at one end of the
information, scan down, until the desired key is found or the end is
reached.
This method is called sequential or linear search.
More precisely , we can define sequential search as:
A=<a1,a2,a3,….an> be the list of ‘n’ records, each record has a key element ai.
Given a key value ‘v’ we want to find the information or retrieve the
record from the list, such that v= ai, i.e. we are finding the target record,
by its key value.
Algorithm:
Let A be an array of structure, having two fields Rollno and
name. The algorithm for making the sequential search to
search for a student having Rollno provided as a key is given
as follows:
Step 1: Read the information of ‘n’ students
in the array A. (i.e. rollno and name).
Step 2: Read the Rollno, which you want to
search, say key.
Step 3: Initialize Counter I to 1.
Step 4: Repeat steps 5 & 6 until I < n,
Step 5: If (A[i].rollno = key) then
Print ‘Key Found’,
Print Rollno, name and goto step 8.
Step 6: i=i+1 i.e advance to next entry in the list and goto step 5.
Step 7: Else
Print ‘No such entry in list’.
Steo 8: Stop.
Flowchart: start
Read how many nos say n
Read the no. to be searched say x
Read the array A of n nos.
i=1
Is a[i]=x
Print “no. found”
Stop
i=i+1
Is i<= n
Print “no. not found”
Yes
No
No
Yes
Advantages:
1. It is very easy to implement. It requires no additional data
structure.
Disadvantage:
2. Its performance degrades when the list grows.
Suppose you are searching for a telephone number of Mr.Xavier and
that you are searching it sequentially from the beginning. You may
read unnecessarily many entries, which could have been avoided.
Binary Search
Binary search is an improvement in searching method to reduce
the amount of work that is made.
If the list is organized in order (just like in telephone directory,
where the names are arranged alphabetically) one of the best
approach is to , first compare the required target key with the
center of the list, and then restrict ourselves to only the first or
second half of the list, depending on whether the required item
comes before or after the center one.
In this way at each step we reduce the length of the list to be
searched by half, hence the name binary search.
For example:
we are searching for telephone number of Mr.Xavier. The current
page which we have opened of the directory is containing names
starting with letter say ‘k’. Since ‘x’ > ‘k’ (X of Xavier) we continue
to search the upper (next) pages, leaving the earlier pages. This
method, can locate a name from a list of million names, within
only 20 comparisons.
Binary Search method is applicable only when the elements are
arranged in order.
Algorithm:
Step1: Read the ‘n’ elements in an array, say A.
Step 2: Read the element to be searched say X.
Step 3: Low = 1, High =n (initialize the lower and upper limit of the list to
be searched)
Step 4: Find the middle element,
mid-position = (Low + High)/2
Y=A [Mid-position]
Step 5: If (X =Y) then
Print “element found and its location is ‘mid
position’”
Goto Step 9.
Step 6: If (X<Y) (i.e. element to be searched is
less than the middle element) then
search to be continued in lower half;
high = mid-position – 1
goto step 8.
Step 7: Otherwise the element is in upper half,
low = mid-position + 1.
Step 8: If (High >= Low) then goto step 4 else
print “Element not in the list”
Step 9: Stop.
Example 1:
Let n=5, and array A have elements:
10, 20, 30, 40, 50
Now we will run the algorithm for searching the element 20.
Following table shows run steps for searching x=20.
Sr.No Step no Executed
Action/Remarks.
1 3 Low = 1, High =5
2 4 Mid-position= (1+5)/2 =3; Y=30
3 6 (X <Y ), High = Mid-Position -1 = 3 -1 = 2
4 8 High>=Low (2>1)
5 4 Mid-Position = (1+2)/2 =1 ; Y=10
6 7 (X>Y) (20>10), Low = 1 + 1= 2
7 8 High >= Low (2>=2)
8 4 Mid-Position = (2+2)/ 2=2 ; Y=20
9 5 X=Y, Element Found
10 9 Stop
Following table shows run steps for searching x=45.
Sr.No Step no Executed
Action/Remarks.
1 3 Low = 1, High =5
2 4 Mid-position= (1+5)/2 =3; Y=30
3 (X >Y ), (45 > 30) Low = Mid-Position +1 = 3 +1 = 4
4 8 High>=Low (5>4)
5 4 Mid-Position = (5+4)/2 =4 ; Y=40
6 7 (X>Y )(45>40), Low = Mid-Position + 1 = 4+1 =5
7 8 High >= Low (5=5)
8 4 Mid-Position = (5+5)/ 2=5 ; Y=50
9 6 X<y (45<50) High= 5-1=4
333310 8 High < Low (4<5) element not found
Example: Consider the following set of numbers: 10 20 30 40 50 60 70 80With which numbers of comparison is being made while
searching for the number:1) 30 Low Up Mid Remarks 1 8 4 30 < 40 up = mid – 1 1 3 2 30 > 20 Low = mid +1 3 3 3 Found
1) 50 Low Up Mid Remarks 1 8 4 50 > 40 Low = mid + 1 5 8 6 50 < 60 Up = mid -1 5 5 5 Found
Start
Read how many nos. say n
Read array A of n nos
Read the no. to be searched say x
Low =1 , Up = n
Compute mid = (low + up)/2
Is a[mid]=x
Print “No. Found”
Stop
YesNo
Is a[mid]<x No
Up =mid-1
Yes
Low = mid + 1
Is up >= low
Print “No. not found”
Stop
NoYes
SORTING ALGORITHMS
Sorting Sorting is a process of organizing data in a specific order.
Sorting is of two types:
1) Internal Sort:
• Internal sorting methods are used when the file to be sorted is small
enough so that the entire sorting can be done in main memory.
2) External Sort:
• External sorting methods are used when the file to be sorted is large,
so that entire sorting can’t be done in main memory.
• So we need to swap the records between main memory and
secondary storage, frequently, which results in more time for sorting.
Importance of Sorting:
1) Sorting is the basic building block around which many other
algorithms are built. By understanding sorting, we obtain an
amazing amount of power to solve other problems.
2) Sorting is the most thoroughly studied problem in computer
science.
3) Most of the interesting ideas used in the design of algorithms
appear in the context of sorting, such as divide – and – conquer,
data structures and randomized algorithms.
Applications of Sorting:
An important key to algorithm design is to use sorting as a basic building
block, because once a set of items is sorted, many other problems
become easy.
Consider following applications:
1)Searching:
Binary search enables you to test whether an item is in a dictionary ,
once the keys are sorted.
2) Closest Pair:
Given a set of n numbers, how do you find the pair of numbers that have
the smallest difference between them? After the numbers are sorted,
the closest pair of numbers will lie next to each other somewhere in
sorted order.
3) Element Uniqueness:
Are there any duplicates in a given set of n items? The most
efficient algorithm is to sort them and do a linear scan through
them checking all adjacent pairs.
4) Frequency Distribution:
Given a set of n items, which element occurs the largest number
of times in the set? If the items are sorted, we can sweep from left
to right and count them, since all identical items will be lumped
together during sorting.
Bubble Sort This is the most simple sorting algorithm but least efficient
methods among other methods. In computer programming, it is also one of the easiest to
implement. Although it is a very slow algorithm when used with a large value
set, it can be easily described by a few instructions. The basic idea here is that, the largest number will be pushed
down the list, so that it will be placed in its order. (if we are arranging the numbers in increasing order).
Continue this process till all the numbers are placed in the proper order.
Each number is compared with its next (successor) number, and swapped (interchanged) if the first number is larger than the second number.
This way, when we reach the end of the list, the largest number will be placed in its proper position.
But we need to repeat these steps for many times, so as to get the list sorted.
Pseudocode and FlowchartBubbleSort( int a[], int n)Begin for i = 1 to n-1 for j = 0 to n-1-i if a[j] > a[j+1] temp = a[j] a[j] = a[j+1] a[j+1] = temp end for if sorted break from i loopend forEnd
• Bubble Sort Example i = 1 j 0 1 2 3 4 5 6 7 0 5 3 1 9 8 2 4 7 1 3 5 1 9 8 2 4 7 2 3 1 5 9 8 2 4 7 3 3 1 5 9 8 2 4 7 4 3 1 5 8 9 2 4 7 5 3 1 5 8 2 9 4 7 6 3 1 5 8 2 4 9 7
i = 2 0 3 1 5 8 2 4 7 9 1 1 3 5 8 2 4 7 2 1 3 5 8 2 4 7 3 1 3 5 8 2 4 7 4 1 3 5 2 8 4 7 5 1 3 5 2 4 8 7
i = 3 j 0 1 2 3 4 5 6 7 0 1 3 5 2 4 7 8 1 1 3 5 2 4 7 2 1 3 5 2 4 7 3 1 3 2 5 4 7 4 1 3 2 4 5 7
i = 4 0 1 3 2 4 5 7 1 1 3 2 4 5 2 1 2 3 4 5 3 1 2 3 4 5
i = 5 j 0 1 2 3 4 5 6 7 0 1 2 3 4 5 1 1 2 3 4 2 1 2 3 4
i = 6 0 1 2 3 4 1 1 2 3
i = 7 0 1 2 3 1 2
• Note for array of size 8, outside i loop repeats 7 times.
Insertion Sort
• Based on the technique used by card players to arrange a
hand of cards
– Player keeps the cards that have been picked up
so far in sorted order
– When the player picks up a new card, he makes
room for the new card and then inserts it in its
proper place
Insertion Sort 8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
2 3 4 8 9 6
2 3 4 6 8 9
Insertion Sort Algorithm
• For each array element from the second to the last (nextPos = 1)– Insert the element at nextPos where it belongs in
the array, increasing the length of the sorted subarray by 1
The idea: Consider one element at a time, inserting it in its proper place among already sorted elements.
The algorithm: Input: An array A storing N itemsOutput: A sorted in ascending order
Algorithm Insertion_Sort (A, N):
for i:= 2 to N do { current := A[i] j := i while A[j-1] > current A[j] := A[j-1], j := j-1 A[j] = current}
Source Code: void insertion( int a[], int n ) { /* Pre-condition: a contains n items to be sorted */ int i, j, v; /* Initially, the first item is considered 'sorted' */ /* i divides a into a sorted region, x<i, and an unsorted one, x >= i */ for(i=1;i<n;i++) { /* Select the item at the beginning of the as yet unsorted section */ v = a[i]; /* Work backwards through the array, finding where v should go */ j = i; /* If this element is greater than v, move it up one */ while ( a[j-1] > v ) { a[j] = a[j-1]; j = j-1; if ( j <= 0 ) break; } /* Stopped when a[j-1] <= v, so put v at position j */ a[j] = v; } }
Selection Sort Selection sort is probably the simplest sorting algorithm to implement so it
can be blazingly fast on short arrays. The idea is to find minimum of the array and swap it with the first element of
the array. Then repeat the process with the rest of array, shrinking the unsorted part of
array by one with each selection. Implementation consists of two loops:
1)External loop index determines the boundary of the sorted area
• 2)The inner loop is the classic algorithms of finding a minimum of the array. The selection sort algorithm is in many ways similar to the Simple Sort and
Bubble Sort algorithms. Rather than swapping neighbors continuously as the algorithm traverses the (sub) array to be sorted, as done in the Bubble Sort case, this algorithm finds the MINIMUM element of the (sub) array and swaps it with the pivot (or "anchor") element.
As an example, begin with the following array.
The selection sort marks the first element (7). It then goes through the remaining data to find the smallest number (1). It swaps with the first element (7) and the smallest element (1) which is placed in its correct position.
It then marks the second element (4) and looks through the remaining data for the next
smallest number (2). These two numbers are then swapped. Marking the third element (7) and looking through the remaining data for the next smallest
number (4). These two numbers are now swapped.
Lastly it marks the fourth element (9) and looks through the remaining data for the next smallest number (7). These two numbers are then swapped.
If we were not finished at this point this sort would continue until n-1 passes have been made.
Pseudocode:Begin for i = 0 to n-1 minpos = i for j = i+1 to n-1 if a[j] < a[minpos] minpos = j temp = a[i] a[i] = a[minpos] a[minpos] = tempEnd
top related