An algorithm is a process or a set of rules required to perform calculations or some other problem-solving operations especially by a computer. It is not complete program or code, it is just a solution (logic) of a problem, which can be represented either as an informal descnption using a flowchart or pseudosode.
characteristics of an algorithm:-
Input:- An algorithm has some input values. We can pass '0' or some input value to an algorithm. Output:- we will get more output at end of an algorithm. unambiguity :- An algorithm should be unambigous.which means that instruction in an algorithm should be clear and simple.. Finiteness :- An algonthm should have finiteness. means limited number of instructions. Effectiveness : - An algorithm should have finite as each instruction in an algorithm affects the overall process.
Approches in Algorithm
Brute force Algorithm :-
The general logic structure is applied to design an algorithm. It is also known as exhaustive search algorithm that searches all possible to provide required solution.
such algorithms have two types :-
1). Optimizing:-finding all solutions of a problem and then take out the best solution is known then it will terminate.if the best solution is known.
2). Sacrificing :-As soon as the best solution is found, then it will stop.
Divide and conquer :- This breaks down the algorithm to solve the problem in different methods. It allows you to break down problem into different methods, and valid output is produced for the valid imput. This valid output is passed to some other function.
Greedy algorithm :- It is an algorithm paradigm that makes an optimal choice on each iteration with the hope of getting best solution. It is easy to implement and has faster execution time. But there are very rare cases in which it provides the optimal solution.
The major categories of algorithms are given below:-
Sort :- Algorithm developed for sorting the items in a certain order.
search :-Algorithm developed for searching the items inside a data Structure.
Delete :-Algorithm developed for deleting the existing element from the data structure.
Insert :-Algorithm developed for inserting an item inside a data structure.
Update :-Algorithm developed for updating the existing element inside a datel structure.
Algorithm Analysis
The algorithm can be analyzed in two levels :-
The first is before creating the algoritom, and Second is after creating the algorithm.
There are two analysis of an algorithm:-
Prior Analysis :-Here, prior analysis is the theoretical analysis of an algorithm which is done before implementing the algorithm.
Posterion Analysis :- Here, posterior analysis is a practical analysis of an algorithm. The practical analysis is achieved by implementing algorithm using any programming language.
Algorithm complexity:-
The performance of the algorithm can be measured in two factors:
Time Complexity:-The time complexity of an algorithm is the amount of time required to complete the execution. The time complexity of an algorithm is denoted by the big o notation.
Here big o notation is the asymptotic notation to represent time complexity. The time complexity is mainly calculated by counting the number of steps to finish execution.
Example:-
Sum = 0;
// suppose we have to calculate the sum of n numbers.
for i=1 to n.
Sum = sum + i;
// when the loop ends then sum holds the sum of n numbers.
return sum;
In above code, the time complexity of the loop statement will be atleast n, and if value of n increases, then time complexity also increases.
We generally consider the worst-time. as it is maximum time taken for any given input sixe..
Space complexity:-
An algorithm's space complexity is the amount of space required to solve a problem and produce an output. similar to the time
complexity, space complexity is also expressed in big o notation.
Space complexity = Auxiliary Space + Input size.
Types of Algorithms
Search Algorithm :-
on each day, we search for something in our day to day life.
similarly, with the case of computer, huge data is stored in a computer that whenever user asks for any data then the computer searches for that data in the memory and provides that data to the user.
There are mainly two techniques available to search data in an array:
• Linear search. • Binary search.
sorting Algorithms :- sorting algorithms are used to rearrange elements in an array or a given data structure. either in an ascending or descending order. The comparison operator decides the new order of the elements: