Adsa
Adsa
Informal Definition:
An Algorithm is any well-defined computational procedure that takes some value or set of
values as Input and produces a set of values or some value as output. Thus algorithm is a
sequence of computational steps that transforms the input into the output.
Formal Definition:
An Algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In
addition, all algorithms should satisfy the following criteria.
INPUT Zero or more quantities are externally supplied.
OUTPUT At least one quantity is produced.
DEFINITENESS Each instruction is clear and unambiguous.
FINITENESS If we trace out the instructions of an algorithm, then for all cases, the
algorithm terminates after a finite number of steps.
EFFECTIVENESS Every instruction must very basic so that it can be carried out, in
principle, by a person using only pencil & paper.
Issues or study of Algorithm:
1. How to device or design an algorithm creating and algorithm.
2. How to express an algorithm definiteness.
3. How to analysis an algorithm time and space complexity.
4. How to validate an algorithm fitness.
5. Testing the algorithm checking for error.
Algorithm Specification:
Algorithm can be described in three ways.
1. Natural language like English:
When this way is choused care should be taken, we should ensure that
each & every statement is definite.
2. Graphic representation called flowchart:
This method will work well when the algorithm is small& simple.
3. Pseudo-code Method:
3. {
4. Result := A[1];
5. for I:= 2 to n do
6. if A[I] > Result then
7. Result :=A[I];
8. return Result;
9. }
In this algorithm (named Max), A & n are procedure parameters. Result & I are Local
variables.
Next we present 2 examples to illustrate the process of translation problem into an
algorithm.
Selection Sort:
Suppose we Must devise an algorithm that sorts a collection of n>=1 elements of
arbitrary type.
A Simple solution given by the following.
( From those elements that are currently unsorted ,find the smallest & place it
next in the sorted list.)
Algorithm:
1. For i:= 1 to n do
2. {
3. Examine a[I] to a[n] and suppose the smallest element is at a[j];
4. Interchange a[I] and a[j];
5. }
Finding the smallest element (sat a[j]) and interchanging it with a[ i ]
We can solve the latter problem using the code,
t := a[i];
a[i]:=a[j];
a[j]:=t;
The first subtask can be solved by assuming the minimum is a[ I ];checking a[I]
with a[I+1],a[I+2]…….,and whenever a smaller element is found, regarding it as the
new minimum. a[n] is compared with the current minimum.
Putting all these observations together, we get the algorithm Selection sort.
Theorem: Algorithm selection sort(a,n) correctly sorts a set of n>=1 elements
.The result remains is a a[1:n] such that a[1] <= a[2] ….<=a[n].
Selection Sort:
Selection Sort begins by finding the least element in the list. This element is
moved to the front. Then the least element among the remaining element is
found out and put into second position. This procedure is repeated till the
entire list has been studied.
Example: List L = 3,5,4,1,2
1 is selected , 1,5,4,3,2
2 is selected, 1,2,4,3,5
3 is selected, 1,2,3,4,5
4 is selected, 1,2,3,4,5
Proof:
We first note that any I, say I=q, following the execution of lines 6 to 9,it is the
case that a[q] Þ a[r],q<r<=n.
Also observe that when ‘i’ becomes greater than q, a[1:q] is unchanged. Hence,
following the last execution of these lines (i.e. I=n).We have a[1] <= a[2] <=……a[n].
We observe this point that the upper limit of the for loop in the line 4 can be
changed to n-1 without damaging the correctness of the algorithm.
Algorithm:
1. Algorithm selection sort (a,n)
2. // Sort the array a[1:n] into non-decreasing order.
3.{
4. for I:=1 to n do
5. {
6. j:=I;
7. for k:=i+1 to n do
8. if (a[k]<a[j])
9. t:=a[I];
10. a[I]:=a[j];
11. a[j]:=t;
12. }
13. }
1.3. PERFORMANCE ANALYSIS:
1. Space Complexity:
The space complexity of an algorithm is the amount of money it needs to run to
compilation.
2. Time Complexity:
The time complexity of an algorithm is the amount of computer time it needs to run
to compilation.
Space Complexity:
Space Complexity Example:
Algorithm abc(a,b,c)
{
return a+b++*c+(a+b-c)/(a+b) +4.0;
}
The Space needed by each of these algorithms is seen to be the sum of the
following component.
1.A fixed part that is independent of the characteristics (eg:number,size)of the
inputs and outputs.
The part typically includes the instruction space (ie. Space for the code),
space for simple variable and fixed-size component variables (also called
aggregate) space for constants, and so on.
2.A variable part that consists of the space needed by component variables
whose size is dependent on the particular problem instance being solved, the
space needed by referenced variables (to the extent that is depends on instance
characteristics), and the recursion stack space.
a. The space requirement s(p) of any algorithm p may therefore be written as,